如何在SharePoint中使用REST API将项目添加到列表中的文件夹中?
下面的代码成功地将项目添加到列表中,但我想使用REST API将项目添加到列表中的文件夹中,列表名称为“Designation”,文件夹名称为“Folder1”。要在文件夹中插入项目,我应该做哪些更改如何在SharePoint中使用REST API将项目添加到列表中的文件夹中?,rest,sharepoint,directory,Rest,Sharepoint,Directory,下面的代码成功地将项目添加到列表中,但我想使用REST API将项目添加到列表中的文件夹中,列表名称为“Designation”,文件夹名称为“Folder1”。要在文件夹中插入项目,我应该做哪些更改 $.ajax({ url:"https://brillio446.sharepoint.com/teams/Social2016/work/_api/web/lists/getByTitle('Designation')/items", method:"P
$.ajax({
url:"https://brillio446.sharepoint.com/teams/Social2016/work/_api/web/lists/getByTitle('Designation')/items",
method:"POST",
dataType:"json",
data: JSON.stringify({
'__metadata': {'type': 'SP.Data.DesignationListItem' },
'Title': 'D1',
}),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function(data){
alert("Item added successfully!");
},
error: function(err){
alert("Error while adding item: " + JSON.stringify(err));
}
});
我还发现文件夹路径应该在那里,所以我尝试了这个代码。。。
但我得到的错误是,SP.Data.designationListItem
data: JSON.stringify({
'__metadata': {'type': 'SP.Data.DesignationListItem' },
'Title': 'D1',
'Path': '/ServerRelativeUrl of folder',
}),
在创建列表项时,似乎不支持指定文件夹URL,但您可以考虑以下方法:
- 创建一个
资源ListItem
- 获取相关的
资源并将其移动到文件夹中文件
function executeJson(options)
{
var headers = options.headers || {};
var method = options.method || "GET";
headers["Accept"] = "application/json;odata=verbose";
if(options.method == "POST") {
headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
}
var ajaxOptions =
{
url: options.url,
type: method,
contentType: "application/json;odata=verbose",
headers: headers
};
if("payload" in options) {
ajaxOptions.data = JSON.stringify(options.payload);
}
return $.ajax(ajaxOptions);
}
function createListItem(listTitle,properties,folderUrl){
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
return executeJson({
"url" :url,
"method": 'POST',
"payload": properties})
.then(function(result){
var url = result.d.__metadata.uri + "?$select=FileDirRef,FileRef";
return executeJson({url : url});
})
.then(function(result){
var fileUrl = result.d.FileRef;
var fileDirRef = result.d.FileDirRef;
var moveFileUrl = fileUrl.replace(fileDirRef,folderUrl);
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfilebyserverrelativeurl('" + fileUrl + "')/moveto(newurl='" + moveFileUrl + "',flags=1)";
console.log(url);
return executeJson({
"url" :url,
"method": 'POST',
});
});
}
用法
这是一个老问题,但搜索把我带到了这里,所以为其他人添加了答案。 正如Vadim提到的,
/\u api/web/lists/getbytitle('ListTitle')/items
方法不支持向文件夹中添加项目
相反,您应该使用/\u api/web/lists/GetByTitle('ListTitle')/AddValidateUpdateItemUsingPath
方法
只需确保使用字符串值而不是数字、日期或类似的值,因为它与输入表单时的工作方式相同—解析、验证和保存值
MSDN参考资料:
例子:
上面的代码不起作用。由于“值不在预期范围内”,因此给出了错误信息
var listTitle = "Requests"; //list title
var targetFolderUrl = "/Lists/Requests/Archive"; //folder server relative url
var itemProperties = {
'__metadata': { "type": "SP.Data.RequestsListItem" },
"Title": 'Request 123'
};
createListItem(listTitle,itemProperties,targetFolderUrl)
.done(function(item)
{
console.log('List item has been created');
})
.fail(function(error){
console.log(JSON.stringify(error));
});
$.ajax({
url:"https://brillio446.sharepoint.com/teams/Social2016/work/_api/web/lists/getByTitle('Designation')/AddValidateUpdateItemUsingPath",
method:"POST",
dataType:"json",
data: JSON.stringify({{
"listItemCreateInfo": {
"FolderPath": { "DecodedUrl": "/ServerRelativeUrl of folder" },
"UnderlyingObjectType": 0
},
"formValues": [
{
"FieldName": "Title",
"FieldValue": "D1"
}
],
"bNewDocumentUpdate": false
}),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function(data){
alert("Item added successfully!");
},
error: function(err){
alert("Error while adding item: " + JSON.stringify(err));
}
});