Neel Kamal
Neel Kamal

Reputation: 1076

C# code to do bulk Update/Insert SharePoint list Item using Rest API call

I'm trying to do bulk update in sharepoint list by looping through item ids as in the code below. However it just update successfully for the first time. Second update gives error as "Response status code does not indicate success: 403".

I have followed this url C# code to Update SharePoint list Item using Rest API call

Uri uri = new Uri(ConfigurationManager.AppSettings["SiteUrl"]);
            using (var client = new SPHttpClient(uri, ConfigurationManager.AppSettings["userName"], ConfigurationManager.AppSettings["password"]))
            {
                var listTitle = "sp_sample";
                var itemId = 1;
                var itemPayload = new { __metadata = new { type = "SP.Data.sampleListItem" }, Title = "Phani", Place = "Hyderabad" };
                for (int i = 1; i <= 3; i++)
                {
                    itemId = i;
                    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");

                }

Please do share if we have any inbuilt function for bulk update/insert. Or any code to do bulk update or insert.

Any guide would be helpful.

Upvotes: 0

Views: 1593

Answers (1)

Lee
Lee

Reputation: 5493

Did test and find the issue caused by duplicate request header, try update method below.

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");
                    var Digest = RequestFormDigest();
                    DefaultRequestHeaders.Clear();
                    DefaultRequestHeaders.Add("X-RequestDigest", Digest);
                    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);
        }

Upvotes: 1

Related Questions