更新多 select 查找列 REST

Updating a multi-select lookup column REST

短版: 我需要帮助或更新多 select 查找列 REST 的示例。我一直无法添加或更新 multi-select 查找列。


详情

我想做什么: 使用 SPA 中更新的多个查找 ID 更新列表项。我想了解我应该寻找什么以及如何应用它。而且我假设添加一行是相同的。

我施加的限制: 这必须通过客户端代码来完成。我无法部署 wsp/binary 代码。 :( SP2010 我陷入了 jQuery 1.8.3 的黑暗时代,在 IE11 上以文档模式 IE=8 运行 呈现站点。

什么有效: 所有不涉及多重查找的 CRUD。使用 ...ListName(ID)/MultiLookupCol 形状在单独的调用中使用多重查找进行读取。

我尝试或研究过的: 我相信我得到的最接近的是这个。但是, 错误请求错误仍然失败:处理请求流时出错。负载必须代表集合的有效数组格式。

所以我认为我的 multi 属性构建与 Charles Manson 一样不稳定。假设那是我的罪魁祸首,我不知道该如何解决。

使用

var itemProperties = {
    "__metadata": { "type": 'Microsoft.SharePoint.DataService.GroupsOfNumbersItem' },

停止了我得到的类型名称错误。更近了,但还没有。

var aIDs = [1, 3];
var itemProperties = {
    "__metadata": { "type": 'Microsoft.SharePoint.DataService.MyNumbersItem' },
    "ReferenceLookup": { "results": aIDs }
};
updateListItem(myWebUrl(), 
               "GroupsOfNumbers", 
               1, 
               itemProperties,
    function () {
        console.log('Success');
    },
    function (error) {
        console.log(JSON.stringify(error));
    }
);

我已阅读并尝试应用来自多个页面的信息,最相关的似乎是以下内容。

https://sharepoint.stackexchange.com/questions/84560/updating-lookup-values-with-the-rest-api

https://sharepoint.stackexchange.com/questions/69983/updating-a-multiselect-field-via-rest-api-2010

https://sharepoint.stackexchange.com/questions/115950/sharepoint-2013-rest-api-to-update-list-item-uncaught-referenceerror-etag-is-n

https://blogs.msdn.microsoft.com/uksharepoint/2013/02/22/manipulating-list-items-in-sharepoint-hosted-apps-using-the-rest-api/

我已经包含了来自浏览器 window 的以下调用的结果,希望对您有所帮助。

来自 joedev/mftt/_vti_bin/ListData 的结果。svc/GroupsOfNumbers

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://joedev/mftt/_vti_bin/ListData.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">GroupsOfNumbers</title>
  <id>http://joedev/mftt/_vti_bin/ListData.svc/GroupsOfNumbers/</id>
  <updated>2016-07-09T02:00:17Z</updated>
  <link rel="self" title="GroupsOfNumbers" href="GroupsOfNumbers" />
  <entry m:etag="W/&quot;1&quot;">
    <id>http://joedev/mftt/_vti_bin/ListData.svc/GroupsOfNumbers(1)</id>
    <title type="text">My First Test</title>
    <updated>2016-07-08T21:01:04-04:00</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="GroupsOfNumbersItem" href="GroupsOfNumbers(1)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/NumberLookUp" type="application/atom+xml;type=feed" title="NumberLookUp" href="GroupsOfNumbers(1)/NumberLookUp" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="GroupsOfNumbers(1)/CreatedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="GroupsOfNumbers(1)/ModifiedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="GroupsOfNumbers(1)/Attachments" />
    <category term="Microsoft.SharePoint.DataService.GroupsOfNumbersItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:ContentTypeID>0x0100F35C84ED82881449AC47D1675F099E2C</d:ContentTypeID>
        <d:Title>My First Test</d:Title>
        <d:Id m:type="Edm.Int32">1</d:Id>
        <d:ContentType>Item</d:ContentType>
        <d:Modified m:type="Edm.DateTime">2016-07-08T21:01:04</d:Modified>
        <d:Created m:type="Edm.DateTime">2016-07-08T21:01:04</d:Created>
        <d:CreatedById m:type="Edm.Int32">1</d:CreatedById>
        <d:ModifiedById m:type="Edm.Int32">1</d:ModifiedById>
        <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
        <d:Version>1.0</d:Version>
        <d:Path>/mftt/Lists/GroupsOfNumbers</d:Path>
      </m:properties>
    </content>
  </entry>
</feed>

来自 joedev/mftt/_vti_bin/ListData.svc/GroupsOfNumbers(1)

的结果
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="http://joedev/mftt/_vti_bin/ListData.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:etag="W/&quot;1&quot;" xmlns="http://www.w3.org/2005/Atom">
  <id>http://joedev/mftt/_vti_bin/ListData.svc/GroupsOfNumbers(1)</id>
  <title type="text">My First Test</title>
  <updated>2016-07-08T21:01:04-04:00</updated>
  <author>
    <name />
  </author>
  <link rel="edit" title="GroupsOfNumbersItem" href="GroupsOfNumbers(1)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/NumberLookUp" type="application/atom+xml;type=feed" title="NumberLookUp" href="GroupsOfNumbers(1)/NumberLookUp" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="GroupsOfNumbers(1)/CreatedBy" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="GroupsOfNumbers(1)/ModifiedBy" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="GroupsOfNumbers(1)/Attachments" />
  <category term="Microsoft.SharePoint.DataService.GroupsOfNumbersItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:ContentTypeID>0x0100F35C84ED82881449AC47D1675F099E2C</d:ContentTypeID>
      <d:Title>My First Test</d:Title>
      <d:Id m:type="Edm.Int32">1</d:Id>
      <d:ContentType>Item</d:ContentType>
      <d:Modified m:type="Edm.DateTime">2016-07-08T21:01:04</d:Modified>
      <d:Created m:type="Edm.DateTime">2016-07-08T21:01:04</d:Created>
      <d:CreatedById m:type="Edm.Int32">1</d:CreatedById>
      <d:ModifiedById m:type="Edm.Int32">1</d:ModifiedById>
      <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
      <d:Version>1.0</d:Version>
      <d:Path>/mftt/Lists/GroupsOfNumbers</d:Path>
    </m:properties>
  </content>
</entry>

结果来自 joedev/mftt/_vti_bin/ListData.svc/GroupsOfNumbers(1)/NumberLookUp

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://joedev/mftt/_vti_bin/ListData.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">NumberLookUp</title>
  <id>http://joedev/mftt/_vti_bin/ListData.svc/GroupsOfNumbers(1)/NumberLookUp/</id>
  <updated>2016-07-09T02:00:10Z</updated>
  <link rel="self" title="NumberLookUp" href="NumberLookUp" />
  <entry m:etag="W/&quot;1&quot;">
    <id>http://joedev/mftt/_vti_bin/ListData.svc/MyNumbers(4)</id>
    <title type="text"></title>
    <updated>2016-07-08T20:55:47-04:00</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="MyNumbersItem" href="MyNumbers(4)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="MyNumbers(4)/CreatedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="MyNumbers(4)/ModifiedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="MyNumbers(4)/Attachments" />
    <category term="Microsoft.SharePoint.DataService.MyNumbersItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:ContentTypeID>0x0100913060DC06F4DF458B6A06CA950C1149</d:ContentTypeID>
        <d:Title m:null="true" />
        <d:NunberName>Five</d:NunberName>
        <d:Id m:type="Edm.Int32">4</d:Id>
        <d:ContentType>Item</d:ContentType>
        <d:Modified m:type="Edm.DateTime">2016-07-08T20:55:47</d:Modified>
        <d:Created m:type="Edm.DateTime">2016-07-08T20:55:47</d:Created>
        <d:CreatedById m:type="Edm.Int32">1</d:CreatedById>
        <d:ModifiedById m:type="Edm.Int32">1</d:ModifiedById>
        <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
        <d:Version>1.0</d:Version>
        <d:Path>/mftt/Lists/MyNumbers</d:Path>
      </m:properties>
    </content>
  </entry>
  <entry m:etag="W/&quot;1&quot;">
    <id>http://joedev/mftt/_vti_bin/ListData.svc/MyNumbers(5)</id>
    <title type="text"></title>
    <updated>2016-07-08T20:55:54-04:00</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="MyNumbersItem" href="MyNumbers(5)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="MyNumbers(5)/CreatedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="MyNumbers(5)/ModifiedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="MyNumbers(5)/Attachments" />
    <category term="Microsoft.SharePoint.DataService.MyNumbersItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:ContentTypeID>0x0100913060DC06F4DF458B6A06CA950C1149</d:ContentTypeID>
        <d:Title m:null="true" />
        <d:NunberName>Four</d:NunberName>
        <d:Id m:type="Edm.Int32">5</d:Id>
        <d:ContentType>Item</d:ContentType>
        <d:Modified m:type="Edm.DateTime">2016-07-08T20:55:54</d:Modified>
        <d:Created m:type="Edm.DateTime">2016-07-08T20:55:54</d:Created>
        <d:CreatedById m:type="Edm.Int32">1</d:CreatedById>
        <d:ModifiedById m:type="Edm.Int32">1</d:ModifiedById>
        <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
        <d:Version>1.0</d:Version>
        <d:Path>/mftt/Lists/MyNumbers</d:Path>
      </m:properties>
    </content>
  </entry>
  <entry m:etag="W/&quot;1&quot;">
    <id>http://joedev/mftt/_vti_bin/ListData.svc/MyNumbers(6)</id>
    <title type="text"></title>
    <updated>2016-07-08T20:56:13-04:00</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="MyNumbersItem" href="MyNumbers(6)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="MyNumbers(6)/CreatedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="MyNumbers(6)/ModifiedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="MyNumbers(6)/Attachments" />
    <category term="Microsoft.SharePoint.DataService.MyNumbersItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:ContentTypeID>0x0100913060DC06F4DF458B6A06CA950C1149</d:ContentTypeID>
        <d:Title m:null="true" />
        <d:NunberName>One</d:NunberName>
        <d:Id m:type="Edm.Int32">6</d:Id>
        <d:ContentType>Item</d:ContentType>
        <d:Modified m:type="Edm.DateTime">2016-07-08T20:56:13</d:Modified>
        <d:Created m:type="Edm.DateTime">2016-07-08T20:56:13</d:Created>
        <d:CreatedById m:type="Edm.Int32">1</d:CreatedById>
        <d:ModifiedById m:type="Edm.Int32">1</d:ModifiedById>
        <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
        <d:Version>1.0</d:Version>
        <d:Path>/mftt/Lists/MyNumbers</d:Path>
      </m:properties>
    </content>
  </entry>
</feed>

为了完整性,这里是我的更新函数。

function getListItemById(webUrl, listName, itemId, success, failure) {
    var url = webUrl + "/_vti_bin/Listdata.svc/" + listName + "(" + itemId + ") ";

    $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d);
        },
        error: function (data) {
            if (debug == true) {
                console.log("getListItemById ID:" + itemId);
                if (data.responseJSON) {
                    console.dir("Data: " + data);
                }
            }
            if (data.responseJSON) {
                failure(data.responseJSON.error);
            } else {
                console.log("getListItemById No data returned for ID: " + itemId);
            }

        }
    });
}

function updateListItem(webUrl, listName, itemId, itemProperties, success, failure) {
    getListItemById(webUrl, listName, itemId, function (item) {

        $.ajax({
            type: 'POST',
            url: item.__metadata.uri,
            contentType: 'application/json',
            processData: false,
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-HTTP-Method": "MERGE",
                "If-Match": item.__metadata.etag
            },
            data: Sys.Serialization.JavaScriptSerializer.serialize(itemProperties),
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });

    },
    function (error) {
        failure(error);
    });

}

使用 REST 将选择添加到多值查找字段

这是您的数据负载的必要格式:

var data = JSON.stringify({
    MultiLookupColumnName:[
        { 
            __metadata: { 
                uri: "http://yoursiteurl/_vti_bin/ListData.svc/LookupListName(1)"
            }
        },
         { 
            __metadata: { 
                uri: "http://yoursiteurl/_vti_bin/ListData.svc/LookupListName(2)"
            }
        }
    ]
});

此示例假定您的查阅列名为 MultiLookupColumnName,您的查阅列所查找的列表标题为 LookupListName,并且您要设置 multi-lookup 字段以查找 ID 为 1 和 2 的项目。

在您的代码中,您将使用 data 变量代替 itemProperties 变量。

为了完整起见,这里有一个没有 jQuery 的完整示例:

var idOfItemToUpdate = 1;
var url = "/serverRelativeUrl/_vti_bin/ListData.svc/YourListName("+idOfItemToUpdate+")"
var data = JSON.stringify({
    MultiLookupColumnName:[
        {__metadata:{uri:"http://yoursiteurl/_vti_bin/ListData.svc/LookupListName(1)"}},
        {__metadata:{uri:"http://yoursiteurl/_vti_bin/ListData.svc/LookupListName(2)"}}
    ]
});
var xhr = new XMLHttpRequest();
xhr.open("POST",url,true);
xhr.setRequestHeader("X-HTTP-Method", "MERGE");
xhr.setRequestHeader("If-Match", "*");
xhr.setRequestHeader("Content-Type","application/json");
xhr.send(data);

使用 REST 从多值查找字段中删除选择

请注意,要从 multi-selection 查找字段中删除值,您需要使用不同的操作。上面的操作只增加了选择的值。

要从字段中删除选定的值,请使用 DELETE 操作,如下所示:

var xhr = new XMLHttpRequest();
xhr.open("DELETE",
    "/serverRelativeUrl/_vti_bin/ListData.svc/YourListName(1)/$links/MultiLookupColumnName(2)",
    true);
xhr.send();

该代码将从名为 YourListName 的列表中 ID 为 1 的项目的名为 MultiLookupColumnName 的字段中删除项目 ID 为 2 的查找值。

(你能画出那句话吗?)

脚注:没有 jQuery

的回调函数

我在上面的示例中使用了 XMLHttpRequest,但我不希望这给您带来任何麻烦。

如果您决定使用 built-in XMLHttpRequest 而不是 jQuery/AJAX 包装器,您可以使用其 onreadystatechange 事件并向其传递回调函数,如下所示:

xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){
        myCallbackFunction(xhr.status, xhr.responseText);
    }
};

function myCallbackFunction(status, text){
    // decide what to do based on the status
}