Managing Fields with the MashPoint REST API

In my two prevoius posts, I showed how to manage Webs and Lists. In this post I will show you how to manage Fields of a List. You can download MashPoint REST API for SharePoint here.  If you have read the two other posts it should be obvious to you that to create a new Field we will POST to the Fields collection of a List. So let's add some custom Fields to our DocumentLibrary that we created in the previous post. The URI to my list looks like this; http://jndtvbx64/mashpoint/1/Webs/7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib And to access the Fields collection just add Fields to the end of the URI.

A real client should NOT assume anything of the links but instead read them from the response.

If I browse to the following URI http://jndtvbx64/mashpoint/1/Webs/7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib/Fields  I can see the list of Fields.
Fields
To create a new Field we need to POST to the above URI.
private void CreateField()
{
    string uri = "http://jndtvbx64/mashpoint/1/Webs/" +
        "7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib/Fields";

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;
    NameValueCollection values = new NameValueCollection();
    values["Title"] = "A new field";
    values["Description"] = "A field created from REST";
    values["FieldType"] = Microsoft.SharePoint.SPFieldType.URL.ToString();
    values["Required"] = false.ToString();

    wc.UploadValues(uri, "POST", values);
    string newuri = wc.ResponseHeaders.Get("Location");
    Console.WriteLine(newuri);
}
Refresh the browser to see the new Field. The URI of the newly created field is returned in the Location header. My field is located here; http://jndtvbx64/mashpoint/1/Webs/7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib/Fields/A_x0020_new_x0020_field
AnewField

Now if we want to UPDATE the Field to make it part of the forms we will issue a PUT request to the URI representing our SPField.
props
Remember that the names of the HTML Form fields matches the Property names of the object you are targeting. Below is the code snippet to change some properties of the Field.
private void UpdateField()
{
    string uri = "http://jndtvbx64/mashpoint/1/Webs/" +
        "7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib/Fields/" +
        "A_x0020_new_x0020_field";

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;
    NameValueCollection values = new NameValueCollection();
    values["ShowInDisplayForm"] = true.ToString();
    values["ShowInEditForm"] = true.ToString();
    values["ShowInNewForm"] = true.ToString();
 
    wc.UploadValues(uri, "PUT", values);
}
htmlView
Simple and consistent. We use the exact same interface to manage all resources. GET, POST, PUT and DELETE. So finally to delete the Field we issue a DELETE request on the URI of the Field.
private void DeleteField()
{
    string uri = "http://jndtvbx64/mashpoint/1/Webs/" +
        "7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib/Fields/" +
        "A_x0020_new_x0020_field";

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;
    NameValueCollection values = new NameValueCollection();
 
    wc.UploadValues(uri, "DELETE", values);
}
Trying to access the field in the web browser causes a 404 error to be returned.
404

Site Columns
Site columns are “reusable fields”. You create site columns the same way as you create “list fields”. The only difference is the URI you use to create them. To create a list field your URI target the list. To create a site column your URI targets the site. Instead of “polluting” our meta data by creating list columns let’s take a look at how we create a site column which we then can reuse in ContentTypes.
[Test]
public void CreateSiteColumnField()
{
    string uri = "http://jndtvbx64/mashpoint/1/Webs/" +
        "7ca3778b-12de-471d-b53f-afe6be2c815e/Fields/";

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;
    NameValueCollection values = new NameValueCollection();
    values["Group"] = "Created from MashPoint REST API";
    values["Title"] = "A new field";
    values["Description"] = "A field created using MashPoint REST API";
    values["FieldType"] = Microsoft.SharePoint.SPFieldType.URL.ToString();
    values["Required"] = false.ToString();

    wc.UploadValues(uri, "POST", values);
    string newuri = wc.ResponseHeaders.Get("Location");
    Console.WriteLine(newuri);
}
I hope you appreciate how similar it is to creating a list field. In the next post I will show you how to create Items in the List.

Posted Aug 14 2009, 11:00 AM by Jonas Nilsson

Blogs

    MashPoint - A Breakthrough in SharePoint Data Integration
  • Home

News Flash

Download MashPoint Now!

MashPoint - Data Integration for SharePointDownload the official MashPoint release, available as of November 7th, 2008.

Jonas Nilsson Q&A

Bamboo Nation Almost Everywhere

Bamboo Solutions on Facebook

Bamboo Solutions on Google+

Bamboo Solutions on LinkedIn

Bamboo Solutions on YouTube

Bamboo Solutions Corporation, 2002-2013