使用剩余部分动态创建现有SharePoint列表中的新项目,而无需对字段名进行硬编码?

问题描述 投票:0回答:1

我正在尝试使用REST API在SharePoint列表中创建一个新列表项。我正在对要通过数组传递的字段名称进行硬编码。该数组将通过另一个函数进行更新。我正在尝试重用普通代码,但遇到了障碍。这段代码根本没有运行,我尝试在数组中循环并添加数组值的每个元素的第二个元数据值。数组中的每个元素都是我的字段名称。

var favorite=[];
   //favorite is an array of fields title list is updated dynamically with another function
   //I want to dynamically add that dynamically when I create a new list item

var item = {"__metadata": {"type": "SP.Data.TestCatalogListItem"},for(int i=0; i<favourite.length;i++){favorite[i]:cells[i]}};

_createListItem(item);

function _createListItem( listItems,listname, success, failure) {
			$.ajax({
			url: "https://site/_api/web/Lists/getbytitle('MYLIST')/items",
			type: "POST",
			contentType: "application/json;odata=verbose",
			data: JSON.stringify(listItems),
			headers: {
			"Accept": "application/json;odata=verbose",
			"X-RequestDigest": $("#__REQUESTDIGEST").val()
			},
			success: function (data) {
			//success(data);
			},
			error: function (data) {
			//failure(data);
			}
			});
}
javascript rest sharepoint sharepoint-2013 sharepoint-designer
1个回答
0
投票

您是否尝试过将for循环移到_createListItem函数中。

我以前没有使用过ajax方法,我只是使用了JSOM并内置了SP函数,但是对我来说,我传入了一个最多包含7个键的对象:listNamefolderName字段successFunctionCallbacksuccesFunctionParamsfailedFunctionCallbackfailedFunctionParams。字段值只是字段名称和值的数组,就像您拥有的一样。在create函数中,我遍历字段并设置特定值。对于您来说,您只是做同样的事情,而构建一个data变量。

© www.soinside.com 2019 - 2024. All rights reserved.