SharePoint Rest Api-尝试更新列表项时为403

时间:2019-02-19 15:50:38

标签: rest sharepoint sharepoint-2013

从C#库中,我需要使用SP rest Api将文档上载到文档库,然后为其设置属性。

通过执行以下顺序的呼叫,我几乎可以正常工作:

  1. 发布到/ contextinfo以获取FormDigestValue
  2. 在标题属性X-RequestDigest中将包含摘要的二进制文件发布到/ Files / Add
  3. 在标题属性X-RequestDigest中获取/ ListItemAllFields摘要

下一个调用是_api / web / lists / GetByTitle('library name')/ Items(157)的帖子

标头属性X-RequestDigest中包含相同的摘要值,如下所示:

POST 

https://xxx.uk/_api/web/lists/GetByTitle('AssetMgmtEfilesDemo')/Items(157) HTTP/1.1
Accept: application/json, application/xml; odata=verbose
X-HTTP-Method: MERGE
IF-MATCH: *
X-RequestDigest: 0x01426A9818F7145E12BC2E99246C7E00AC1A3905D27204C783107FDDE806D2629171FAD8DCC61008E109DD9948BEB4208DC62107B2336B1228ABA143A2D5B3C6,19 Feb 2019 15:20:44 -0000
Content-Type: application/json; charset=utf-8
Host: xxx.uk
Content-Length: 359
Expect: 100-continue

{  
   __metadata":{
                    "type":"SP.Data.AssetMgmtEfilesDemoItem"
   },
   "UPRN":"U1",
   "KeystoneDocType":"Document"
}

我的问题是,对于此请求,我收到“ 403禁止访问”响应。

任何人都可以看到我出了错吗?

2 个答案:

答案 0 :(得分:0)

您是否在致电之前尝试获取新的摘要摘要值?表单摘要值确实会在一段时间后过期(默认为30分钟)。

您还可以在特定列表上检查您的权限。

答案 1 :(得分:0)

供参考的示例代码,以C#代码调用Rest API,参考System.Web.Extensions dll:

using System.Net;
using System.IO;
using System.Web.Script.Serialization;
static void Main(string[] args)
        {
            UpdateListItem();
        }
        public static string GetFormDigest()
        {
            string formDigest = null;

            string resourceUrl = "http://sp/sites/dev/_api/contextinfo";
            HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
            wreq.UseDefaultCredentials = true;
            wreq.Method = "POST";
            wreq.Accept = "application/json;odata=verbose";
            wreq.ContentLength = 0;
            wreq.ContentType = "application/json";
            string result;
            WebResponse wresp = wreq.GetResponse();

            using (StreamReader sr = new StreamReader(wresp.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }

            var jss = new JavaScriptSerializer();
            var val = jss.Deserialize<Dictionary<string, object>>(result);
            var d = val["d"] as Dictionary<string, object>;
            var wi = d["GetContextWebInformation"] as Dictionary<string, object>;
            formDigest = wi["FormDigestValue"].ToString();

            return formDigest;

        }

        public static void UpdateListItem()
        {
            string result = string.Empty;
            Uri uri = new Uri("http://sp/sites/dev/_api/web/lists/getbytitle('AssetMgmtEfilesDemo')/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", GetFormDigest());

            string stringData = "{'__metadata': { 'type': 'SP.Data.AssetMgmtEfilesDemoItem' }, 'Title': 'UpdatedViaRest','UPRN':'U1','KeystoneDocType':'Image'}";
            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();
            }


        }

enter image description here