[英]How to retrieve metadata of document set using JavaScript
In my SharePoint site I have document sets with some metadata (Budget, Client Name, City ..). 在我的SharePoint网站中,我具有一些元数据(预算,客户端名称,城市..)的文档集。 I want to change the default look of doc-set homepage. 我想更改文档集首页的默认外观。 I want to retrieve the metadata of the current document set, show it in a content editor web part. 我想检索当前文档集的元数据,并在内容编辑器Web部件中显示它。
How do I retrieve current doc set metadata using JavaScript and based on the ID of the doc set? 如何使用JavaScript并基于文档集ID检索当前文档集元数据?
First, keep in mind that there is already a web part that automatically displays metadata from the current document set. 首先,请记住,已经有一个Web部件可以自动显示当前文档集中的元数据。 This web part is called "Document Set Properties" and can be found in the "Document Sets" category of web parts. 该Web部件称为“文档集属性”,可以在Web部件的“文档集”类别中找到。
The fields that it displays can be configured by navigating to Library Settings, clicking the name of your Document Set content type under Content Types, and clicking "Document Set Settings." 可以通过导航到“库设置”,单击“内容类型”下的文档集内容类型的名称,然后单击“文档集设置”来配置它显示的字段。
If for some reason that web part is inadequate for your purposes, you can use the REST API or JavaScript client side object model to retrieve metadata about the current document set. 如果由于某种原因Web部件不足以实现您的目的,则可以使用REST API或JavaScript客户端对象模型来检索有关当前文档集的元数据。 You can obtain the ID of the current document set from the "ID" parameter in the query string section of the URL. 您可以从URL的查询字符串部分中的“ ID”参数获取当前文档集的ID。
SharePoint provides a handy GetUrlKeyValue()
method to easily obtain query string parameters. SharePoint提供了一个便捷的GetUrlKeyValue()
方法来轻松获取查询字符串参数。
var itemId = GetUrlKeyValue("ID");
var listGuid = GetUrlKeyValue("List");
var xhr = new XMLHttpRequest();
xhr.open("GET", "/_api/lists('"+listGuid+"')/items("+itemId+")");
xhr.setRequestHeader("accept","application/json;odata=verbose");
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var item = JSON.parse(xhr.responseText).d;
var title = item.Title;
var desc = item.DocumentSetDescription;
// You can retrieve any other properties here as necessary
}else{
alert("error "+xhr.status+": "+xhr.responseText);
}
}
};
xhr.send();
SP.SOD.ExecuteOrDelayUntilScriptLoaded(function(){
var listGuid = GetUrlKeyValue("List");
var itemId = GetUrlKeyValue("ID");
var clientContext = new SP.ClientContext();
var item = clientContext.get_web().get_lists().getById(listGuid).getItemById(itemId);
clientContext.load(item);
clientContext.executeQueryAsync(
function(){
var title = item.get_item("Title");
var desc = item.get_item("DocumentSetDescription");
// You can retrieve any other properties here as necessary
},
function(sender,args){
alert(args.get_message());
}
);
},"sp.js");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.