gpt4 book ai didi

c# - 通过 REST 在列表中插入项目

转载 作者:太空狗 更新时间:2023-10-30 01:03:56 25 4
gpt4 key购买 nike

我正在创建一个 WCF 服务,我在其中执行共享点列表中的 CRUD 操作。我已经通过 REST 成功地从共享点列表中检索了数据,即通过网络凭据对象传递了凭据(用户名、密码、域),但不幸的是无法插入数据。该错误是指在共享点中插入数据时未经授权的访问。然后我尝试了 Sharepoint 在线凭据对象,但没有成功。我需要一些帮助。

异常(exception)情况:

The remote server returned an error: (401) Unauthorized.

代码如下:

try
{
string userPassword = "password";
WebClient webClient = new WebClient();
SecureString securePassword = new SecureString();
for (int i = 0; i < userPassword.Length; i++)
{
securePassword.AppendChar(userPassword[i]);
}
webClient.Credentials = new SharePointOnlineCredentials(@"username@domain", securePassword);
webClient.Headers.Add("Content-type", "application/json");
webClient.Headers.Add("Accept", "application/json;odata=verbose");
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");

webClient.Encoding = Encoding.UTF8;
string msg = webClient.UploadString("http://abc:9211/sites/api/_api/web/lists/getbytitle('list_name')/Items", "POST", "{__metadata\":{\"type\":\"SP.Data.SubscriptionListItem\"},\"UserID\":\"user\",\"SubscriptionType\":\"Type1\",\"Title\":\"Testing\"}");
}
catch(WebException ex)
{
}

最佳答案

发生此错误是因为要执行创建、更新或删除操作,必须在 SharePoint Online/Office 365 中的请求期间指定 Request Digest 值。

关于请求摘要

X-RequestDigest header 存储一个安全验证 token ,可帮助防止用户在不知情的情况下被诱骗将数据发布到服务器的攻击类型。

如何在 SharePoint 2013/SharePoint Online 中通过 WebClient 创建列表项

using System;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace SPORestClient
{
/// <summary>
/// Client for performing CRUD operations against List resource in SharePoint Online (SPO)
/// </summary>
public class ListsClient : IDisposable
{
public ListsClient(Uri webUri, ICredentials credentials)
{
_client = new WebClient();
_client.Credentials = credentials;
_client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
_client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
_client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
WebUri = webUri;
}

public void InsertListItem(string listTitle, object payload)
{
var formDigestValue = RequestFormDigest();
_client.Headers.Add("X-RequestDigest", formDigestValue);
var endpointUri = new Uri(WebUri, string.Format("/_api/web/lists/getbytitle('{0}')/items", listTitle));
var payloadString = JsonConvert.SerializeObject(payload);
_client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
_client.UploadString(endpointUri, "POST", payloadString);
}

/// <summary>
/// Request Form Digest
/// </summary>
/// <returns></returns>
private string RequestFormDigest()
{
var endpointUri = new Uri(WebUri, "_api/contextinfo");
var result = _client.UploadString(endpointUri, "POST");
JToken t = JToken.Parse(result);
return t["d"]["GetContextWebInformation"]["FormDigestValue"].ToString();
}


public Uri WebUri { get; private set; }

public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}

private readonly WebClient _client;

}
}

要点:[ListsClient.cs][2]

Note: Since SharePoint requires the user to include a request digest value with each create, update and delete operation, an additional request is invoked using RequestFormDigest method to request Context Info entity that contains request digest value.

--

Dependency: [Json.NET library][1]

用法

以下示例演示了如何在Contacts 列表中创建列表项:

using (var client = new ListsClient(webUri, credentials))
{
var contactEntry = new
{
__metadata = new { type = "SP.Data.ContactsListItem" },
Title = "John Doe"
};
client.InsertListItem("Contacts", contactEntry);
}

关于c# - 通过 REST 在列表中插入项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25599509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com