SharePoint 加载项 JSOM 创建列表项错误。 "this" 未定义
SharePoint Add-IN JSOM Create List Item Error. "this" is undefined
我需要让我的 SharePoint 加载项创建一个列表项。我从 OfficeDev Github 站点 here 获得了该功能。当我调用该函数时,它在 "this" 首次出现的行失败。
错误是:
无法设置未定义或空引用的 属性 'oListItem'
// line calling the function
createDELListItem(document.getElementById("resultpanel"), uniqueID, "MTD_On_Demand");
// function
function createDELListItem(resultpanel, extractionID, extractionType) {
var clientContext;
var oWebsite;
var oList;
var itemCreateInfo;
clientContext = new SP.ClientContext.get_current();
oWebsite = clientContext.get_web();
oList = oWebsite.get_lists().getByTitle("Global_Variable_Store");
itemCreateInfo = new SP.ListItemCreationInformation();
// Line throwing the "null or undefined" error
this.oListItem = oList.addItem(itemCreateInfo);
//A guid to send with message that uniquely identifies the list item.
this.oListItem.set_item("ExtractionID", extractionID);
//A brief title (description) of the variable set here in this list item.
this.oListItem.set_item("Extraction Type", extractionType);
//The Variable name
this.oListItem.set_item("StartTime", convertThisDate);
//The process descriptor that set the variable. This is set by the code.
this.oListItem.update();
clientContext.load(this.oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);
function successHandler() {
resultpanel.innerHTML = "Go to the <a href='../Lists/Global_Variable_Store'>list</a> to see your new item.";
}
function errorHandler() {
resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();
}
}
您的情况似乎 this
未定义,我会通过将用于创建列表项的函数替换为:
来避免使用 this
关键字
function addListItem(list,itemProperties,success,error)
{
var ctx = list.get_context();
var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
for(var name in itemProperties) {
listItem.set_item(name, itemProperties[name])
}
listItem.update();
ctx.load(listItem);
ctx.executeQueryAsync(
function() {
success(listItem);
},
error
);
}
用法
var properties = {
"ExtractionID" : "----",
"Extraction Type": "----",
"StartTime": "----"
};
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle("Global_Variable_Store");
addListItem(list,properties,
function(contactItem){
console.log('Item has been created successfully');
},
function(sender,args){
console.log('Error occured while creating item:' + args.get_message());
});
我需要让我的 SharePoint 加载项创建一个列表项。我从 OfficeDev Github 站点 here 获得了该功能。当我调用该函数时,它在 "this" 首次出现的行失败。
错误是: 无法设置未定义或空引用的 属性 'oListItem'
// line calling the function
createDELListItem(document.getElementById("resultpanel"), uniqueID, "MTD_On_Demand");
// function
function createDELListItem(resultpanel, extractionID, extractionType) {
var clientContext;
var oWebsite;
var oList;
var itemCreateInfo;
clientContext = new SP.ClientContext.get_current();
oWebsite = clientContext.get_web();
oList = oWebsite.get_lists().getByTitle("Global_Variable_Store");
itemCreateInfo = new SP.ListItemCreationInformation();
// Line throwing the "null or undefined" error
this.oListItem = oList.addItem(itemCreateInfo);
//A guid to send with message that uniquely identifies the list item.
this.oListItem.set_item("ExtractionID", extractionID);
//A brief title (description) of the variable set here in this list item.
this.oListItem.set_item("Extraction Type", extractionType);
//The Variable name
this.oListItem.set_item("StartTime", convertThisDate);
//The process descriptor that set the variable. This is set by the code.
this.oListItem.update();
clientContext.load(this.oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);
function successHandler() {
resultpanel.innerHTML = "Go to the <a href='../Lists/Global_Variable_Store'>list</a> to see your new item.";
}
function errorHandler() {
resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();
}
}
您的情况似乎 this
未定义,我会通过将用于创建列表项的函数替换为:
this
关键字
function addListItem(list,itemProperties,success,error)
{
var ctx = list.get_context();
var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
for(var name in itemProperties) {
listItem.set_item(name, itemProperties[name])
}
listItem.update();
ctx.load(listItem);
ctx.executeQueryAsync(
function() {
success(listItem);
},
error
);
}
用法
var properties = {
"ExtractionID" : "----",
"Extraction Type": "----",
"StartTime": "----"
};
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle("Global_Variable_Store");
addListItem(list,properties,
function(contactItem){
console.log('Item has been created successfully');
},
function(sender,args){
console.log('Error occured while creating item:' + args.get_message());
});