使用REST更新库项目元数据 [英] Use REST to update library item meta data

查看:102
本文介绍了使用REST更新库项目元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个记录中心网站,其中包括图书馆"Drop Off Library"。


我创建了多个网站内容类型并将其分配给"Drop Off Library" ;。


我能够成功地将文档上传到"Drop Off Library"。使用REST。


这是我遇到麻烦的地方。


我需要:


1:将上传项目的内容类型更改为"文档"以外的内容类型,这是该库的默认内容类型。


2:更新项目的元数据内容类型已更改。


我没有成功#1,所以我转到#2,并尝试设置"标题"。价值,我也没有成功。


以下是我在Fiddler看到的内容:


我得到了400响应错误:  参数  __元数据在方法GetById中不存在。


这是我在Fiddler中使用的代码:


URI:

 https://Mysharepoint.com/sites/rms/_api/web/lists/getbytitle('Drop%20Off%20Library')// items(29)

请求:


请注意,我调用/ _api / contextinfo来获取X-RequestDigest的值。

 User-Agent:Fiddler 
主持人:Mysharepoint.com
内容类型:application / json; odata = verbose
accept:application / json; odata =详细
内容长度:110
X-RequestDigest:0x936E127517E4C06B8845257F7AD05C179C18B1F4ECFEA89089CB1B68D76163EC7B70AEB0A64DE8B36FF75CA242640063615A5F8409C0EAE088316ABE833D5FC3,21 Jun 2017 14:11:20 -0000
X-HTTP-Method:Merge


请求正文:

 { " __metadata":
{
" type":" SP.Data.DropOffLibraryItem"
}
," Title":" whatever"
}


有人可以协助吗?





Bill Behning

解决方案

嗨WRBehning,


请使用下面的代码更新库项目的元数据(用您的ListName和ListItemId替换):

< script src = QUOT; HTTP://code.jquery.com/jquery-latest.js"类型= QUOT;文本/ JavaScript的">< /脚本> 
< script type =" text / javascript">
函数UpdateListItem(){
var listName =" doc1" ;;
var listItemId = 1;
var itemType = GetItemTypeForListName(listName);
console.log(itemType);
var item = {
" __ metadata":{" type":itemType},
" Title":" value change"
};


.ajax({
url:_spPageContextInfo.siteAbsoluteUrl +" / _ api / web / lists / getbytitle('" + listName +" ')/ items(" + listItemId +")",
type:" POST",
contentType:" application / json; odata = verbose",
data: JSON.stringify(item),
header:{
" Accept":" application / json; odata = verbose",
" X-RequestDigest":


(" #__ REQUESTDIGEST")。val(),
" X-HTTP-Method":" MERGE",
" If-Match":" * "
},
成功:函数(数据){
alert('Success');
window.location.href = window.location.href;
},
错误:函数(数据){
alert(" Error");
}
});
}

//获取列表项类型元数据
函数GetItemTypeForListName(name){
return" SP.Data。" + name.charAt(0).toUpperCase()+ name.split("")。join("")。s​​lice(1)+" Item" ;;
}
< / script>
< input type =" button"的onclick = QUOT; UpdateListItem()"值= [更新" />

以下是结果捕获:




谢谢


最好的问候


I have a Records Center site which includes the library "Drop Off Library".

I have created several site content types and assigned them to "Drop Off Library".

I am able to successfully upload a document to "Drop Off Library" using REST.

This is where I am having trouble.

I need to:

1: Change the content type of the uploaded item to a content type other than "Document", which is the default content type for the library.

2: Update the meta data of the item after the content type has been changed.

I was unsuccessful with #1, so I moved on to #2, and tried to set the "Title" value, which I have been unsuccessful as well.

Here is what I am seeing in Fiddler:

I get a 400 response with the error: The parameter  __metadata does not exist in method GetById.

Here is my code used in Fiddler:

URI:

https://Mysharepoint.com/sites/rms/_api/web/lists/getbytitle('Drop%20Off%20Library')//items(29)

Request:

Note that I make a call to /_api/contextinfo to get the value for X-RequestDigest.

User-Agent: Fiddler
Host: Mysharepoint.com
Content-Type: application/json;odata=verbose
accept: application/json;odata=verbose
Content-Length: 110
X-RequestDigest: 0x936E127517E4C06B8845257F7AD05C179C18B1F4ECFEA89089CB1B68D76163EC7B70AEB0A64DE8B36FF75CA242640063615A5F8409C0EAE088316ABE833D5FC3,21 Jun 2017 14:11:20 -0000
X-HTTP-Method: Merge

Request Body:

 {" __metadata": 
    { 
        "type": "SP.Data.DropOffLibraryItem"
     }
    ,"Title": "whatever"
}

Can someone please assist?


Bill Behning

解决方案

Hi WRBehning,

Please use the code below to update metadata for library item (replace the ListName and ListItemId with yours) :

<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript">
function UpdateListItem(){
    var listName="doc1";
    var listItemId= 1;
    var itemType = GetItemTypeForListName(listName);
    console.log(itemType);
    var item = {
        "__metadata": { "type": itemType },
        "Title": "value change"    
    };


.ajax({ url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items("+listItemId+")", type: "POST", contentType: "application/json;odata=verbose", data: JSON.stringify(item), headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest":


("#__REQUESTDIGEST").val(), "X-HTTP-Method": "MERGE", "If-Match": "*" }, success: function (data) { alert('Success'); window.location.href=window.location.href; }, error: function (data) { alert("Error"); } }); } // Get List Item Type metadata function GetItemTypeForListName(name) { return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "Item"; } </script> <input type="button" onclick="UpdateListItem()" value="Update"/>

Here is the result capture:

Thanks

Best Regards


这篇关于使用REST更新库项目元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆