使用 Rest API 调用更新 SharePoint 列表项的 C# 代码 [英] C# code to Update SharePoint list Item using Rest API call

查看:53
本文介绍了使用 Rest API 调用更新 SharePoint 列表项的 C# 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过 Rest Api 调用更新 sharepoint 中的列表项,

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

要求:

SharePoint 类型:在线

SharePoint Type: Online

更新代码:C# Rest API 调用

Code for Update: C# Rest API call

异常:System.dll 中发生类型为System.Net.WebException"的未处理异常

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

附加信息:远程服务器返回错误:(403) Forbidden.

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();
        }


    }
}

}

如何克服上述异常,是否有任何代码可用于使用 Rest API 调用更新 SharePoint 中的列表项,请分享.

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.

推荐答案

您应该传递有效的 SharePoint Online 用户名和密码以获取表单摘要值,并在请求标头中使用此摘要值,而不是在代码段中使用 null.以下是供您参考的代码示例:

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 和 SPHttpClientHandler.cs:

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, "username@tanantname.onmicrosoft.com", "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");
            }
        }
    }
}

参考:

使用 SharePoint Online REST使用 .NET 的服务

这篇关于使用 Rest API 调用更新 SharePoint 列表项的 C# 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆