Managing List Items with the MashPoint REST API

Now it is time to create, update and delete items in a list. I suggest that you read all previous posts before continuing on.
If you have read the posts above it should be pretty clear how to create a new Item. Correct; we POST to the list or to the lists Items collection. Lets work with our previously created List called MyDocLib. Mine is located at this uri; http://jndtvbx64/mashpoint/1/Webs/7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib
As you remember Form fields are mapped to Object properties by name. This is true for List Items too but here the Form field name is mapped to the internal Field name. Since we are adding items to a document library we need to upload files along with form fields. Unfortunately the .NET WebClient class does not allow you to do this in one request. You can either upload files OR send form fields. (I really don't  know why this has not been fixed)
 
So let's look at the code for uploading a file to our document library. It should look very familiar by now. First we POST the file to the List and then we read the location of the newly created Item and we issue a PUT request with the properties we want to update.
public void CreateListItemUsingWebClient()
{
    string uri = "http://jndtvbx64/mashpoint/1/Webs/" +
        "7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib";

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;
    
    wc.UploadFile(uri, "POST", @"foo.txt");
    string newuri = wc.ResponseHeaders.Get("Location");
    Console.WriteLine(newuri);

    NameValueCollection values = new NameValueCollection();

    values["Title"] = "A new Item";
    values["A new field"] = "http://www.google.com";

    wc.UploadValues(newuri, "PUT", values);
}
There are a few classes out "there" that allows us to upload both files and form fields. In this example I'm using a slightly modified version of the wwHTTP class created by Rick Strahl. You can download the modified class here.
public void CreateListItem()
{
    string uri = "http://jndtvbx64/mashpoint/1/Webs/" +
        "7ca3778b-12de-471d-b53f-afe6be2c815e/Lists/MyDocLib";

    HttpUtils.wwHttp wc = new HttpUtils.wwHttp();
    wc.Credentials = CredentialCache.DefaultCredentials;
    wc.AddPostFile("file", @"foo.txt" );
    wc.AddPostKey("Title", "A new Item" );
    wc.AddPostKey("A new field","http://www.google.com");

    wc.GetUrl(uri);
    string newuri = wc.WebResponse.Headers["Location"];

    Console.WriteLine(newuri);
}
As you can see we only need one POST request to upload the file AND set the properties.

Note! You don't have to escape the internal name using XmlConvert.EncodeName since this is done for you. But you can handle the escaping yourself if you want to.

To DELETE the list item we issue a DELETE request to the URI of the item.
delete
So how do we create items in a regular list? In the exact same way, but here files will be added as attachments to the Item. Let's add a comment to our Comments list.
CommentList
One URI to this comment list is (in my example) http://jndtvbx64/mashpoint/1/subweb/Lists/Comments
So let's update our previous example to POST to this URI instead and we will set the Title and Body properties.
public void CreateListItemUsingWebClient()
{
    string uri = "http://jndtvbx64/mashpoint/1/subweb/Lists/Comments";

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;

    wc.UploadFile(uri, "POST", @"gateway.gif");
    string newuri = wc.ResponseHeaders.Get("Location");
    Console.WriteLine(newuri);

    NameValueCollection values = new NameValueCollection();

    values["Title"] = "A new Comment";
    values["Body"] = "This is my comment.";

    wc.UploadValues(newuri, "PUT", values);
}
Running this code should give you one Comment in your list with one attachment.
NewComment
To add an attachment we POST to the Attachments collection of the Item.
public void AddAttachementToExistsingItem()
{
    string uri = "http://jndtvbx64/mashpoint/1/subweb/Lists/Comments/6/Attachments";

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;

    wc.UploadFile(uri, "POST", @"memory.jpg");
}
AddAttachment
To delete an attachment we issue a DELETE request to the URI of the attachment.
public void DeleteAttachementFromExistsingItem()
{
    string uri = "http://jndtvbx64/mashpoint/1/subweb/Lists/"+
        "Comments/2/Attachments/memory.jpg";

    WebClient wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;
    wc.UploadString(uri, "DELETE", @"");
}
I hope you like the consistency of the API. Once again you manage ListItems, Attachments, Lists, Fields and Webs using the same verbs. In the next post we will look at how to create Views.

Posted Aug 17 2009, 11:00 AM by Jonas Nilsson
Filed under: , ,

Blogs

    MashPoint - A Breakthrough in SharePoint Data Integration
  • Home

Bamboo Nation, Media Sponsor of:

SPTechCon

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

Follow Bamboo Nation on:Bamboo Solutions on Facebook

Bamboo Solutions on Google+

Bamboo Solutions on LinkedIn

Bamboo Solutions on Twitter

Bamboo Solutions on YouTube

SharePoint Calendars

SharePoint Calendars

Bamboo Solutions Corporation, 2002-2012