Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中使用SharePoint Online 2013 Rest API删除列表项#_C#_Rest_Sharepoint 2013 - Fatal编程技术网

C# 如何在C中使用SharePoint Online 2013 Rest API删除列表项#

C# 如何在C中使用SharePoint Online 2013 Rest API删除列表项#,c#,rest,sharepoint-2013,C#,Rest,Sharepoint 2013,我正在尝试使用REST API和C#托管代码从SharePoint 2013 Online中删除列表项 以下是我的代码的精髓: using (var client = new WebClient()) { client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); client.Credentials = mySPCreds; client.Headers.Add(HttpRequestHeader.ContentTy

我正在尝试使用REST API和C#托管代码从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网站管理员


因此,我想知道我是否只是语法/方法有误。如果我没有权限问题,有人能验证我上面的代码“应该”工作吗?

好的,我找到了解决方案:

客户端标题需要包括表单摘要:

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