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

  • Question

  • 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

    Tuesday, August 6, 2013 4:06 PM

Answers

  • 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 *********

    Wednesday, August 7, 2013 7:03 PM
  • Wednesday, August 7, 2013 12:56 PM
  • 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


    Please mark the replies as answers if they help or unmark if not.

    Wednesday, August 7, 2013 5:04 PM

All replies

  • 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

    val it: unit=()

    Tuesday, August 6, 2013 11:37 PM
  • 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 *********

      Wednesday, August 7, 2013 6:51 AM
    2. 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

      Wednesday, August 7, 2013 8:08 AM
    3. Wednesday, August 7, 2013 12:56 PM
    4. 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


      Please mark the replies as answers if they help or unmark if not.

      Wednesday, August 7, 2013 5:04 PM
    5. 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 *********

      Wednesday, August 7, 2013 7:03 PM
    6. 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

      Tuesday, November 12, 2013 10:28 AM
    7. Missing the code for function getDigestValue()
      Friday, January 24, 2014 4:17 PM