在使用SharePoint 2013 REST API时,我在指定的文件夹中添加ListItem时遇到问题。
使用客户端对象模型,它看起来像这样:
var creationInfo = new SP.ListItemCreationInformation();
creationInfo.FolderUrl = "/Lists/MyList/MyFolder";
var item = list.addItem(creationInfo );
item.update();
ctx.executeQueryAsync(
Function.createDelegate(this, onSuccess),
Function.createDelegate(this, onFail));
但是当我试图通过Rest Service
设置FolderUrl时{ '__metadata' : { 'type': 'SP.Data.MyListListItem' }, 'Title': 'NewItemInFolder', 'FolderUrl': '/sites/example/Lists/MyList/MyFolder' }
我收到400 Bad Request
我还尝试先将ListItem添加到List中,然后更新FolderUrl,但这也无效。
如何使用Rest-Api将ListItem添加到SharePoint中的列表文件夹?
修改
{ '__metadata' : {
'type': 'SP.Data.MyListListItem',
'type': 'SP.Data.ListItemCreationInformation'
},
'Title': 'NewItemInFolder',
'FolderUrl': '/sites/example/Lists/MyList/MyFolder'
}
我现在尝试使用ListItemEntityTypeFullName
我的列表中的实体和ListItemCreationInformation,但我也只得到400错误请求。
当我在与Fiddler查看请求时,我发现SharePoint现在忽略了我的列表实体类型SP.Data.MyListItem
答案 0 :(得分:3)
您不应使用REST api,而应使用listdata.svc
url: /_vti_bin/listdata.svc/[Name of List]
Header:Content-Type: application/json;odata=verbose
Body: {Title: "Title", Path: "/ServerRelativeUrl"}
我遇到了同样的问题,但是使用REST api它将无法使用旧的svc。
答案 1 :(得分:0)
您可以考虑以下解决方案,它包含以下3个步骤:
ListItem
资源File
资源,最后将其移至文件夹示例强>
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(webUrl,listTitle,properties,folderUrl){
var url = webUrl + "/_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 = webUrl + "/_api/web/getfilebyserverrelativeurl('" + fileUrl + "')/moveto(newurl='" + moveFileUrl + "',flags=1)";
console.log(url);
return executeJson({
"url" :url,
"method": 'POST',
});
});
}
用法
var webUrl = _spPageContextInfo.webAbsoluteUrl;
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(webUrl,listTitle,itemProperties,targetFolderUrl)
.done(function(item)
{
console.log('List item has been created');
})
.fail(function(error){
console.log(JSON.stringify(error));
});
答案 2 :(得分:-1)
Microsoft.SharePoint.Client.ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext("URL");
Microsoft.SharePoint.Client.List list = clientContext.Web.Lists.GetByTitle("FolderName");
var folder = list.RootFolder;
clientContext.Load(folder);
clientContext.Credentials = new NetworkCredential(username, password, domain);
clientContext.ExecuteQuery();
folder = folder.Folders.Add("ItemName");
clientContext.Credentials = new NetworkCredential(username, password, domain);
clientContext.ExecuteQuery();
答案 3 :(得分:-1)
creationInfo.set_folderUrl("http://siteURL/Lists/Docs/Folder1");