Updating list items in a document library using SharePoint 2013 REST Api + C#

Hello,

is it possible to update/modify a list item in a document library via REST Api (c#)?

Unfortunately, I could not find any code samples.

Any kind of help will be appreciated.

Thanks,

Dennis

August 6th, 2013 12:07pm

Files and Folders using REST

http://code.msdn.microsoft.com/SharePoint-2013-Perform-ab9c4ae5

Basic data access operations with REST

http://code.msdn.microsoft.com/SharePoint-2013-Perform-335d925b

All other samples

http://msdn.microsoft.com/en-us/library/jj901637.aspx
Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 7:39pm

Hi Dennis,

Yes, it is possible to update list item using REST. Please find a simple code sample below to update the title of an item using REST call  -

    • // Update Operation
    • // listName: The name of the list you want to get items from
    • // siteurl: The url of the site that the list is in. // title: The value of the title field for the new item
    • // itemId: the id of the item to update
    • // success: The function to execute if the call is sucesfull
    • // failure: The function to execute if the call fails
    • function updateListItem(itemId, listName, siteUrl, title, success, failure) {
    •     var itemType = GetItemTypeForListName(listName);
    •  
    •     var item = {
    •         "__metadata": { "type": itemType },
    •         "Title": title
    •     };
    •  
    •     getListItemWithId(itemId, listName, siteUrl, function (data) {
    •         $.ajax({
    •             url: data.__metadata.uri,
    •             type: "POST",
    •             contentType: "application/json;odata=verbose",
    •             data: JSON.stringify(item),
    •             headers: {
    •                 "Accept""application/json;odata=verbose",
    •                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    •                 "X-HTTP-Method""MERGE",
    •                 "If-Match": data.__metadata.etag
    •             },
    •             success: function (data) {
    •                 success(data);
    •             },
    •             error: function (data) {
    •                 failure(data);
    •             }
    •         });
    •     }, function(data){
    •         failure(data);
    •     });
    • }
  1. function GetItemTypeForListName(name) {
    •     return"SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
    • }

    Narahari

    *******If a post answers your question, please click "Mark As Answer" on that post and/or "Vote as Helpful *********

    August 7th, 2013 2:52am

    Thank you both very much for your replies.

    @Narahari: That's exactly what I am looking for, but for C# not JavaScript. Unfortunately, I was not able to transfer the code to C#. Do you have a C# sample as well?

    Thanks,

    Dennis

    Free Windows Admin Tool Kit Click here and download it now
    August 7th, 2013 4:10am

    C# examples are here:

    http://msdn.microsoft.com/en-us/library/jj164022.aspx

    August 7th, 2013 3:56pm

    It's possible, just  leverage HttpWebRequest and HttpResponse object, make a HTTP POST call

    HttpWebRequest endpointRequest =
      (HttpWebRequest)HttpWebRequest.Create(
      "http:// <site url>/_api/contextinfo");
    endpointRequest.Method = "POST";
    endpointRequest.Accept = "application/json;odata=verbose";
    endpointRequest.Headers.Add("Authorization",
      "Bearer " + accessToken);
    HttpWebResponse endpointResponse =
      (HttpWebResponse)endpointRequest.GetResponse();

    Then set the necessary headers in the request

    Free Windows Admin Tool Kit Click here and download it now
    August 7th, 2013 8:04pm

    Hi Dennis,

    Please find below a sample code in C# which updates the list title using REST calls. You can update the code (string data and url) in the sample below to update a list item also.

    Uri uri = new Uri("http://sp2013vm/_api/web/lists/GetByTitle('Tasks')");            
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.ContentType = "application/json;odata=verbose";
        request.Headers["X-RequestDigest"] = getDigestValue();
        request.Headers["X-HTTP-Method"] = "MERGE";
        request.Headers["IF-MATCH"] = "*";
        request.Credentials = CredentialCache.DefaultCredentials;
        request.Accept = "application/json;odata=verbose";
        request.Method = "POST";
    
        string stringData = "{ '__metadata': { 'type': 'SP.List' }, 'Title': 'New title' }";
        request.ContentLength = stringData.Length;
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(stringData);
        writer.Flush();
    
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());

    Narahari

    *******If a post answers your question, please click "Mark As Answer" on that post and/or "Vote as Helpful *********

    August 7th, 2013 10:03pm

    Hi,

    Thanks for the post,

    Actually i need to send a msdocument to office365 document library by using rest api at client side.

    how to attach file to Sharepoint online [office 365] document library by using visual studio 2010 & Rest API services in visual studio?

    Can anybody please tell me its urgent i have been searching for this task since one week,please help me

    atleast u tell me how to connect to office365 online through java script or any csom with rest api only.

    please help me.

    Thanking you in Advance

    Mahesh

    Free Windows Admin Tool Kit Click here and download it now
    November 12th, 2013 5:29am

    This topic is archived. No further replies will be accepted.

    Other recent topics Other recent topics