0

I am writing an API in Web API 2.0 on C# to perform various Actions with SharePoint List items.

I have tried the following code to update the Status Column of a List item:

HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("https://site.sharepoint.com/sites/OverDuePos/_api/web/lists/GetByTitle('Overdue POs List')/items(45)");

endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.Method = "MERGE";
// endpointRequest.Headers.Add("X-HTTP-Method", "MERGE");
endpointRequest.Headers.Add("IF-MATCH", "*");
endpointRequest.Headers.Add("X-RequestDigest", null);

string stringData = "{'__metadata': { 'type': 'SP.Data.TestlistListItem' }, 'PO_x0020_Status': 'whatever'}";
endpointRequest.ContentLength = stringData.Length;

string result = string.Empty;
endpointRequest.Headers.Add("Authorization",
"Bearer " + "token");
StreamWriter writer = new StreamWriter(endpointRequest.GetRequestStream());
writer.Write(stringData);
writer.Flush();

WebResponse wresp = endpointRequest.GetResponse();
using (StreamReader sr = new StreamReader(wresp.GetResponseStream()))
{
    result = sr.ReadToEnd();
}

The code is throwing the following Error:

'The remote server returned an error: (500) Internal Server Error.'

Similarly I have to update various columns in Multiple Lists.

Also, I cannot Hardcode any user and Password in the code.

Can anyone Help me sorting this Out?

1 Answer 1

0

You can refer to the following code to call rest api in c#

string taskListUrlById = "{0}_api/web/lists/GetByTitle('tasks')/items(932)"; //932 is the ID of the item to update
string SiteUrl = "yoursite";
string url = string.Format(taskListUrlById, SiteUrl);
string content = "{ '__metadata': { 'type': 'SP.Data.TasksListItem' }, 'Status': 'Close'}";

var credential = new System.Net.NetworkCredential("username",
                                                  "Password",
                                                   "domain");
                
using (var handler = new HttpClientHandler() { Credentials = credential, UseProxy = false })
    {
      using (var client = new HttpClient(handler))
        {
          client.DefaultRequestHeaders.Accept.Clear();
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
          var request = new HttpRequestMessage(HttpMethod.Post, url);
          request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
          request.Headers.Add("X-RequestDigest", await GetRequestDigest());
          request.Headers.Add("X-HTTP-Method", "MERGE");
          request.Headers.Add("IF-MATCH", "*");
          HttpContent strContent = new StringContent(content);
          strContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
          strContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("odata", "verbose"));
          request.Content = strContent;
          var response = await client.SendAsync(request);
        }
    }
1
  • I am looking forward to update SharePoint list with SharePoint Apis. this is not working for that Jul 25, 2023 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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