I am trying to update a list item in sharepoint. I am using the code below it returns Bad Request. any idea Thanks in advance...

15489348 1 Reputation point
2021-05-05T12:20:10.537+00:00
string cont = "{ '__metadata': { 'type': 'SP.IssuesListItem' }, 'Status': 'Close'}";
var content = new StringContent(cont, Encoding.UTF8, "application/json");
string url = "https://mywebsite.com/_api/web/lists/GetByTitle('tasks')/items?$filter=ID eq 932"

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.Add("X-HTTP-Method", "MERGE");
                    var mediaType = new MediaTypeWithQualityHeaderValue("application/json");
                    mediaType.Parameters.Add(new NameValueHeaderValue("odata", "nometadata"));
                    client.DefaultRequestHeaders.Accept.Add(mediaType);
                    client.DefaultRequestHeaders.Add("X-RequestDigest", await GetRequestDigest());
                    client.DefaultRequestHeaders.Add("binaryStringRequestBody", "true");
                    client.DefaultRequestHeaders.Add("IF-MATCH", "*");

                    HttpResponseMessage response = await client.PostAsync(url, content);
                    response.EnsureSuccessStatusCode();
                    return true;
                }
            }
Developer technologies | ASP.NET | ASP.NET Core
Microsoft 365 and Office | SharePoint | Development
Developer technologies | C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2021-05-05T16:01:53.95+00:00

    Hi,
    did you convert your password to SecureString?

     string pwString = "password";
     System.Security.SecureString pwSecure = new System.Security.SecureString();
     foreach (var c in pwString) pwSecure.AppendChar(c);
    
     var credential = new System.Net.NetworkCredential(
                     "username",
                     pwSecure,
                     "domain");
    
    0 comments No comments

  2. Michal Tech 1 Reputation point
    2022-08-08T18:22:59.61+00:00

    To simply write to one blank NFC tag, make sure all of these options are unchecked. Finally, select “Write” at the bottom of the screen and tap the back of your phone write my paper help to a blank NFC tag as if you were going to read it. This writes the URL to the tag. Congratulations, you've just encoded your first NFC tag!

    0 comments No comments

  3. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2022-08-08T18:58:14.417+00:00

    you are using the lookup url instead of the update url:

    POST https://{site_url}/_api/web/lists(guid'{list_guid}')   
    Authorization: "Bearer " + accessToken   
    Accept: "application/json;odata=verbose"   
    Content-Type: "application/json"   
    Content-Length: {length of request body as integer}   
    If-Match: "{etag or *}"   
    X-HTTP-Method: "MERGE"   
    X-RequestDigest: "{form_digest_value}"   
       
    {   
      "__metadata": {   
        "type": "SP.List"   
      },   
      "Title": "New title"   
    }   
    

    the lookup should give you the guid. also it looks like you are using basic authentication. be sure this is supported. if its onprem or hosted vm you are probably good. but online Sharepoint will not support basic

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.