SharePoint 2013 Rest Api:在文件夹中创建列表项 [英] SharePoint 2013 Rest Api: Create list item in folder

查看:74
本文介绍了SharePoint 2013 Rest Api:在文件夹中创建列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SharePoint 2013 REST Api在特定文件夹中创建列表项.

I'm trying to create a list item in a specific folder using SharePoint 2013 REST Api.

此操作的JavaScript代码如下:

The JavaScript Code for this operation looks like that:

var listItemCreationInfo = new SP.ListItemCreationInformation();
listItemCreationInfo.set_folderUrl(_spPageContextInfo.webAbsoluteUrl + "/Lists/ExampleList/Subfolder");
var listItem = list.addItem(listItemCreationInfo);
context.executeQueryAsync(
 ...
);

因此,我需要设置"FolderUrl"通过REST,但我不知道该怎么做.

So, I need to set the "FolderUrl" via REST, but I don't know how to do that.

我的HTTP Post请求符合MSDN文章的准则: http://msdn.microsoft.com/zh-CN/library/jj164022.aspx#ListItems

My HTTP Post request complies with the guidelines of the MSDN article: http://msdn.microsoft.com/en-us/library/jj164022.aspx#ListItems

POST https://url/_api/web/lists/getByTitle('Tasks')/items
Cookie: ...
Content-Type: application/json;odata=verbose
Host: domain.sharepoint.com
X-RequestDigest: ...
Content-Length: ...

{ '__metadata' : { 'type': 'SP.Data.TasksListItem' }, 'Title': 'Example' }

T 他的效果很好,但正如我所说,我不知道如何传递"FolderUrl".

This works fine, but as I said, I don't know how to pass the "FolderUrl".

以下请求正文导致"400错误请求",因为列表项没有字段"FolderUrl":

The following request body leads to a "400 Bad Request", because the list item has no field "FolderUrl":

{ '__metadata' : { 'type': 'SP.Data.TasksListItem' }, 'Title': 'Example', 'FolderUrl': '/sites/example/Lists/Tasks/Subfolder' }

我也尝试过两次操作.首先,我将列表项添加到列表中.然后,我更新列表项并设置"FolderUrl",但这也不起作用.

I have also tried to do that in two operations. First, I add the list item to the list. Then, I update the list item and set the "FolderUrl", but this does not work, too.

非常感谢您的帮助!

谢谢.

最诚挚的问候,

达斯汀

推荐答案

我知道这可能需要一年半的时间才能帮助原始海报,但我浪费了相当多的时间这么多的时间,所以我认为我至少会尽力帮助别人避免浪费他们的时间.

I know this is probably a year and a half too late to help the original poster, but I wasted a considerable amount of time on this, so I figured I'd at least try to help someone else avoid wasting theirs.

注意:我只是确认在SharePoint Online网站上是这种情况,因此,在本地工作的人可能想测试一下,看看我是否得到相同的结果.

Note: I've only confirmed this to be the case on SharePoint Online sites, so someone working on-premise might want to test and see if I get the same results.

原始海报的JavaScript代码段中的API使用 SP.List JSOM API中的.addItem(SP.ListItemCreationInformation)方法,因此要使用正确的API 使用本来是

The API in the original poster's JavaScript snippet uses the SP.List.addItem(SP.ListItemCreationInformation) method from the JSOM API, so the correct API to use would've been:

POST https://domain.sharepoint.com/_api/web/lists/getByTitle('Tasks')/addItem
Cookie: ...
Content-Type: application/json;odata=verbose
Host: domain.sharepoint.com
X-RequestDigest: ...
Content-Length: ...

{
	"parameters": {
		"__metadata": {
			"type": "SP.ListItemCreationInformation"
		},
		"FolderUrl": "https://domain.sharepoint.com/Lists/Tasks/Subfolder",
		"LeafName": null,
		"UnderlyingObjectType": 0
	}
}

但是,与此有关的问题是REST实现似乎已损坏.执行上述请求后,您将遇到以下错误:

The problem with this, however, is that the REST implementation appears to be broken. After executing the above request, you'll end up with the following error:

{
    "odata.error": {
        "code": "-2146233086, System.ArgumentOutOfRangeException",
        "message": {
            "lang": "en-US",
            "value": "Specified argument was out of the range of valid values."
        }
    }
}

通过回顾 2010年的文档,我们可以看到SP.List.addItem应该只为无效的UnderlyingObjectType抛出System.ArgumentOutOfRangeException 价值观.尽管如此,您仍将持续收到有关中所有值的错误 使用名称在字符串值(例如"File")中使用.将导致以下错误:

By looking back at 2010's documentation, we can see that SP.List.addItem should only throw a System.ArgumentOutOfRangeException for invalid UnderlyingObjectType values. Despite that, you will consistently receive the error for all of the values in FileSystemObjectType. Using the name in a string value like "File" will result in the following error:

{
    "odata.error": {
        "code": "-1, Microsoft.Data.OData.ODataException",
        "message": {
            "lang": "en-US",
            "value": "Cannot convert a primitive value to the expected type 'Edm.Int32'. See the inner exception for more details."
        }
    }
}

无论如何,我尝试了每种可以想象的格式使之起作用,包括:

Anyways, I tried every imaginable format to make it work, including:

https://domain.sharepoint.com/_api/web/lists/getByTitle('Tasks')/addItem(parameters=@v1)?@v1={"parameters":{"__metadata":{"type":"SP.ListItemCreationInformation"},"FolderUrl":"https://domain.sharepoint.com/Lists/Tasks/Subfolder","UnderlyingObjectType":0}}

没有运气.对于另一个添加项目的端点:https : //url/_api/web/lists/getByTitle('Tasks')/items

No luck. As for the other endpoint for adding items: https://url/_api/web/lists/getByTitle('Tasks')/items

我尝试了我能想到的所有显而易见的字段(FileDirRef,Folder,FolderUrl,具有对象值的Folder),但从未成功.我还注意到OData的文件夹"列表项上的属性返回null,因此我不确定 它支持该功能.

I tried every obvious field I could think of, (FileDirRef, Folder, FolderUrl, Folder with an object value) and never turned up any success. I also noticed that the OData "Folder" property on list items comes back null, so I'm not really sure that it supports the functionality.

是的,据我所知,无法使用新的OData API在文件夹中创建列表项.

So yeah, as far as I can tell, there's no way to create list items in folders using the new OData API.




这篇关于SharePoint 2013 Rest Api:在文件夹中创建列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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