0
votes

I have to update the list item in sharepoint through Rest Api call,

Requirement:

SharePoint Type: Online

Code for Update: C# Rest API call

Exception: An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: (403) Forbidden.

public class Class1
{
    public static void Main()
    {

        string result = string.Empty;
        Uri uri = new Uri("http://serviceportal.xxx.com/_api/lists/getbytitle('List name')/items(1)");
        HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(uri);
        wreq.Credentials = CredentialCache.DefaultNetworkCredentials;

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

        string stringData = "{'__metadata': { 'type': 'SP.Data.TestlistListItem' }, 'Title': 'whatever'}";
        wreq.ContentLength = stringData.Length;
        StreamWriter writer = new StreamWriter(wreq.GetRequestStream());
        writer.Write(stringData);
        writer.Flush();

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


    }
}

}

How to over come the above exception and is there any code available for update list item in SharePoint using Rest API call kindly share.

1
404 defines an authorization issue..u might wanna test these calls on postman first : getpostman.comBhawna Jain
I have tested in postman also, throws like "401 unauthorized"THARMA KS

1 Answers

1
votes

You should pass valid SharePoint Online username and password to get the form digest value and use this digest value in your request header instead of null in your code snippet. Here is a code sample for your reference:

SPHttpClient.cs and SPHttpClientHandler.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;

    namespace SPORestConsole
    {
        public class SPHttpClient: HttpClient
        {
            public SPHttpClient(Uri webUri, string userName, string password) : base(new SPHttpClientHandler(webUri, userName, password))
            {
                BaseAddress = webUri;
            }
            /// <summary>
            /// Execure request method
            /// </summary>
            /// <param name="requestUri"></param>
            /// <param name="method"></param>
            /// <param name="headers"></param>
            /// <param name="payload"></param>
            /// <returns></returns>
            public JObject ExecuteJson<T>(string requestUri, HttpMethod method, IDictionary<string, string> headers, T payload)
            {
                HttpResponseMessage response;
                switch (method.Method)
                {
                    case "POST":
                        var requestContent = new StringContent(JsonConvert.SerializeObject(payload));
                        requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");
                        DefaultRequestHeaders.Add("X-RequestDigest", RequestFormDigest());
                        if (headers != null)
                        {
                            foreach (var header in headers)
                            {
                                DefaultRequestHeaders.Add(header.Key, header.Value);
                            }
                        }
                        response = PostAsync(requestUri, requestContent).Result;
                        break;
                    case "GET":
                        response = GetAsync(requestUri).Result;
                        break;
                    default:
                        throw new NotSupportedException(string.Format("Method {0} is not supported", method.Method));
                }

                response.EnsureSuccessStatusCode();
                var responseContent = response.Content.ReadAsStringAsync().Result;
                return String.IsNullOrEmpty(responseContent) ? new JObject() : JObject.Parse(responseContent);
            }

            public JObject ExecuteJson<T>(string requestUri, HttpMethod method, T payload)
            {
                return ExecuteJson(requestUri, method, null, payload);
            }

            public JObject ExecuteJson(string requestUri)
            {
                return ExecuteJson(requestUri, HttpMethod.Get, null, default(string));
            }


            /// <summary>
            /// Request Form Digest
            /// </summary>
            /// <returns></returns>
            public string RequestFormDigest()
            {
                var endpointUrl = string.Format("{0}/_api/contextinfo", BaseAddress);
                var result = this.PostAsync(endpointUrl, new StringContent(string.Empty)).Result;
                result.EnsureSuccessStatusCode();
                var content = result.Content.ReadAsStringAsync().Result;
                var contentJson = JObject.Parse(content);
                return contentJson["d"]["GetContextWebInformation"]["FormDigestValue"].ToString();
            }
        }
    }


    using System;
    using System.Net;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Security;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.SharePoint.Client;

    namespace SPORestConsole
    {
        public class SPHttpClientHandler : HttpClientHandler
        {
            public SPHttpClientHandler(Uri webUri, string userName, string password)
            {
                CookieContainer = GetAuthCookies(webUri, userName, password);
                FormatType = FormatType.JsonVerbose;
            }


            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
                if (FormatType == FormatType.JsonVerbose)
                {
                    //request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json;odata=verbose"));
                    request.Headers.Add("Accept", "application/json;odata=verbose");
                }
                return base.SendAsync(request, cancellationToken);
            }


            /// <summary>
            /// Retrieve SPO Auth Cookies 
            /// </summary>
            /// <param name="webUri"></param>
            /// <param name="userName"></param>
            /// <param name="password"></param>
            /// <returns></returns>
            private static CookieContainer GetAuthCookies(Uri webUri, string userName, string password)
            {
                var securePassword = new SecureString();
                foreach (var c in password) { securePassword.AppendChar(c); }
                var credentials = new SharePointOnlineCredentials(userName, securePassword);
                var authCookie = credentials.GetAuthenticationCookie(webUri);
                var cookieContainer = new CookieContainer();
                cookieContainer.SetCookies(webUri, authCookie);
                return cookieContainer;
            }


            public FormatType FormatType { get; set; }
        }

        public enum FormatType
        {
            JsonVerbose,
            Xml
        }
    }

call update list item action like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace SPORestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri ("https://tenantname.sharepoint.com/sites/dev/");
            using (var client = new SPHttpClient(uri, "[email protected]", "yourpassword"))
            {
                var listTitle = "MyList8";
                var itemId = 4;
                var itemPayload = new { __metadata = new { type = "SP.Data.MyList8ListItem" }, Title = "updateviaRest" };
                var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items({2})", uri, listTitle, itemId);
                var headers = new Dictionary<string, string>();
                headers["IF-MATCH"] = "*";
                headers["X-HTTP-Method"] = "MERGE";
                client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, itemPayload);
                Console.WriteLine("Task item has been updated");
            }
        }
    }
}

enter image description here

Reference:

Consume SharePoint Online REST service using .NET