如何在 C# 中使用 SharePoint Online 2013 Rest API 删除列表项
How to delete list item using SharePoint Online 2013 Rest API in C#
我正在尝试使用带有 C# 托管代码的 REST API 从 SharePoint 2013 Online 中删除列表项。
这是我的代码的本质:
using (var client = new WebClient())
{
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Credentials = mySPCreds;
client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
client.Headers.Add("X-HTTP-Method", "DELETE");
client.Headers.Add("IF-MATCH", "*");
var requestUri = new Uri("https://mysharepointsite.../_api/web/lists/getbytitle('MyList')/items(123)");
client.UploadString(requestUri, String.Empty);
}
我收到 403 权限被拒绝,但使用类似的模式我能够创建列表项。我将我的凭据用于 WebClient,它是一个 SharePointOnlineCredentials 对象。我是 SharePoint 站点管理员。
所以,我想知道我是否只是 syntax/approach 错了。如果我没有权限问题,有人可以验证我上面的代码 "should" 是否有效吗?
好的,我找到了解决方案:
客户端headers需要包含表格摘要:
client.Headers.Add("X-RequestDigest", GetFormDigest());
要获取表单摘要,您可以使用此代码:
private string GetFormDigest()
{
var webClient = new WebClient();
webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
webClient.Credentials = this.Credentials;
webClient.Headers.Add(HttpRequestHeader.ContentType, String.Format("application/json;odata=nometadata"));
webClient.Headers.Add(HttpRequestHeader.Accept, String.Format("application/json;odata=nometadata"));
var uri = new Uri(this.SharepointBaseURL);
var endpointUri = new Uri(uri, "_api/contextinfo");
var result = webClient.UploadString(endpointUri, "POST");
JToken t = JToken.Parse(result);
//Use this if odata = nometadata
return t["FormDigestValue"].ToString();
//Use this if odata = verbose
//return t["d"]["GetContextWebInformation"]["FormDigestValue"].ToString();
}
注意:凭据也很棘手,这是相关代码:
var securePassword = new SecureString();
foreach (var c in myPassword)
{
securePassword.AppendChar(c);
}
this.Credentials = new SharePointOnlineCredentials(myUserName, securePassword);
我正在尝试使用带有 C# 托管代码的 REST API 从 SharePoint 2013 Online 中删除列表项。
这是我的代码的本质:
using (var client = new WebClient())
{
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Credentials = mySPCreds;
client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
client.Headers.Add("X-HTTP-Method", "DELETE");
client.Headers.Add("IF-MATCH", "*");
var requestUri = new Uri("https://mysharepointsite.../_api/web/lists/getbytitle('MyList')/items(123)");
client.UploadString(requestUri, String.Empty);
}
我收到 403 权限被拒绝,但使用类似的模式我能够创建列表项。我将我的凭据用于 WebClient,它是一个 SharePointOnlineCredentials 对象。我是 SharePoint 站点管理员。
所以,我想知道我是否只是 syntax/approach 错了。如果我没有权限问题,有人可以验证我上面的代码 "should" 是否有效吗?
好的,我找到了解决方案:
客户端headers需要包含表格摘要:
client.Headers.Add("X-RequestDigest", GetFormDigest());
要获取表单摘要,您可以使用此代码:
private string GetFormDigest()
{
var webClient = new WebClient();
webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
webClient.Credentials = this.Credentials;
webClient.Headers.Add(HttpRequestHeader.ContentType, String.Format("application/json;odata=nometadata"));
webClient.Headers.Add(HttpRequestHeader.Accept, String.Format("application/json;odata=nometadata"));
var uri = new Uri(this.SharepointBaseURL);
var endpointUri = new Uri(uri, "_api/contextinfo");
var result = webClient.UploadString(endpointUri, "POST");
JToken t = JToken.Parse(result);
//Use this if odata = nometadata
return t["FormDigestValue"].ToString();
//Use this if odata = verbose
//return t["d"]["GetContextWebInformation"]["FormDigestValue"].ToString();
}
注意:凭据也很棘手,这是相关代码:
var securePassword = new SecureString();
foreach (var c in myPassword)
{
securePassword.AppendChar(c);
}
this.Credentials = new SharePointOnlineCredentials(myUserName, securePassword);