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...

Hanna Nassar Nassar 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;
                }
            }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,138 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,202 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,655 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 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) 55,041 Reputation points
    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