更新多选查阅列REST

时间:2016-07-09 02:57:16

标签: javascript jquery rest sharepoint-2010 single-page-application

简短版本: 我需要帮助或更新多选查阅列REST的示例。我无法添加或更新多选查阅列。

详情

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

我施加的限制:这必须使用客户端代码完成。我无法部署wsp /二进制代码。 :( SP2010我在jQuery 1.8.3的黑暗时代陷入困境,在IE11上运行的文档模式IE = 8中的网站渲染。

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

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

所以我相信我的多元化物业建筑与查尔斯曼森一样不稳定。假设这是我的罪魁祸首,我不知道如何解决它。

使用

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/

我已经从浏览器窗口中包含了以下调用的结果,希望它有所帮助。

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

}

1 个答案:

答案 0 :(得分:4)

使用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,并且您要将多查找字段设置为查找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

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

请注意,要从多选查找字段中删除值,您需要使用其他操作。以上操作仅添加到选定的值。

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

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

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

(你能说出那句话吗?)

脚注:没有jQuery的回调函数

我使用XMLHttpRequest作为上述示例,但我不希望这会让你头疼。

如果您决定使用内置的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
}