SharePoint Online LookUp字段PATCH调用

时间:2018-01-20 18:07:47

标签: ajax rest sharepoint-online sharepoint-api

我遇到的问题是当我对SharePoint LookUp字段进行PATCH调用时。每次我打电话时,所有已经在多值查找列中的Id都会被覆盖。我的临时修复是检索LookUp字段中的所有Id,然后将新Id添加到包含所有现有Id的数组中,然后使用该数组进行PATCH调用。 PATCH调用不应该只是将新的id添加到LookUp字段而不删除字段中已存在的ID吗?这个问题解释起来很棘手。希望我足够清楚。谢谢你的帮助!

代码:

    function linkContract(ajaxData) {

            var def = jQuery.Deferred();
            //Url
            var url = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/lists/getbytitle('Avtal')/items('${window._cache.avtalsInfoId}')`;

            //requestHeaders
            var requestHeaders = {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-HTTP-Method": "PATCH",
                "IF-MATCH": `${window._cache.avtal_eTag}`,
                "X-RequestDigest": jQuery('#__REQUESTDIGEST').val()
            }
          //Array with old Id´s that I add the new Id to and then PATCH it to the column     
 window._cache.relateradeAvtalArray.push(parseInt(ajaxData.RelateradeAvtalId));

            //Data
            var data = {
                __metadata: {
                    "type": "SP.Data.AvtalItem"
                },
                RelateradeAvtalId: {
                    'results': window._cache.relateradeAvtalArray//<--Array with old and new id´s
                },

            };
            //requestBod
            var requestBody = JSON.stringify(data);

            //Post
            var postLookId = jQuery.ajax({
                url: url,
                type: "PATCH",
                headers: requestHeaders,
                data: requestBody
            });

            //Done
            postLookId.done(function (data, textStatus, jqXHR) {
                def.resolve(data);


            });
            //Fail
            postLookId.fail(function (jqXHR, textStatus, errorThrown) {

            })
            return def.promise();
        }

1 个答案:

答案 0 :(得分:0)

PATCH方法允许实体的部分更新,但不允许实体的属性,换句话说,请求有效负载中指定的属性将被替换,而实体的当前状态将被保留。

例如,使用以下请求

Url: /_api/web/lists/getbytitle(Tasks)/items(1)
Method. POST
Headers: {"Accept":"application/json;odata=verbose","Content-Type":"application/json; odata=verbose","X-HTTP-Method":"MERGE","If-Match":"\"36\"","X-RequestDigest":"0x6B24D8B9C10F251812478A2D313EBD36D5373993A11CF9EF1D8414555C5E1CC1D0AD596633E2AEF4D5194CB534CF25006C913BDEC885F6914B4D2803DAB6FC04,27 Jan 2018 13:17:32 -0000"}
Data: {"__metadata":{"type":"SP.Data.TasksListItem"},"PredecessorsId":{"__metadata":{"type":"Collection(Edm.Int32)"},"results":[3]}}

Predecessors字段值将在“任务列表”的现有列表项资源中替换

合并查找字段值,您可以考虑以下解决方案:

  • 提交获取查询以检索现有列表项
  • 通过合并客户端上的现有查找ID和新查找ID来更新字段值
  • 提交更新查询以更新列表项资源