4

I want to know how to update title of Sharepoint web using REST. I have used REST to view the title but not sure how to update the title.

I can view the title by exploring http://urlofsite/_api/web/title also.

MSDN has given the code in js but not in c#.

Thanks

1

I have figured it out by myself, this is how it will work. This is code is updating title of list, title of web can also be updated by changing the uri and post data.

    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());
1
  • in a similar way, how can i get a listitem that matches with a item name ? like if the listitem["title"] == "Employee" , i want to do some operations . – userAZLogicApps Jan 23 '17 at 10:32
0

When I tried to use the Ahmed`s solution, I got the following error:

Exception calling "Flush" with "0" argument(s): "Bytes to be written to the stream exceed the Content-Length bytes size specified." At C:\Scripts\UpdateItem.ps1:69 char:18
+     $writer.Flush <<<< ();
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I resolve this error by replacing this command

request.ContentLength = stringData.Length;

To this command

encoder = new UTF8Encoding(false)
request.ContentLength = encoder.GetByteCount(stringData)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.