使用ecmascript创建Sharepoint 2010列表
我不熟悉ECMAScript和共享点开发,我有一个小要求,我需要使用ECMAScript创建一个列表,创建时必须检查该列表是否已存在于网站中,如果列表不存在,则必须创建新列表。您可以使用“GetListCollection”在Sharepoint中查找所有列表,然后使用“AddList”来创建它 比如:使用ecmascript创建Sharepoint 2010列表,sharepoint,sharepoint-2010,sharepoint-designer,ecmascript-5,client-side,Sharepoint,Sharepoint 2010,Sharepoint Designer,Ecmascript 5,Client Side,我不熟悉ECMAScript和共享点开发,我有一个小要求,我需要使用ECMAScript创建一个列表,创建时必须检查该列表是否已存在于网站中,如果列表不存在,则必须创建新列表。您可以使用“GetListCollection”在Sharepoint中查找所有列表,然后使用“AddList”来创建它 比如: var myList="My List Test"; // the name of your list var listExists=false; $().SPServices({ oper
var myList="My List Test"; // the name of your list
var listExists=false;
$().SPServices({
operation: "GetListCollection",
async: true,
webURL:"http://my.share.point/my/dir/",
completefunc: function (xData, Status) {
// go through the result
$(xData.responseXML).find('List').each(function() {
if ($(this).attr("Title") == myList) { listExists=true; return false }
})
// if the list doesn't exist
if (!listExists) {
// see the MSDN documentation available from the SPService website
$().SPServices({
operation: "AddList",
async: true,
webURL:"http://my.share.point/my/dir/",
listName:myList,
description:"My description",
templateID:100
})
}
}
});
请确保正确阅读网站,尤其是。您需要在代码中包含jQuery和SPSServices。您可以利用或为此目的,下面演示了JSOM
解决方案
如何在SharePoint 2010中使用JSOM创建列表
如何确定Web中是否存在列表
不幸的是,JSOMAPI不包含任何“内置”方法来确定列表是否存在,但您可以使用以下方法
一种解决方案是使用列表集合加载Web对象,然后遍历列表集合以查找特定列表:
context.load(web, 'Lists');
解决方案
以下示例演示如何通过JSOM确定列表是否存在:
function listExists(siteUrl,listTitle,success,error) {
var context = new SP.ClientContext(siteUrl);
var web = context.get_web();
context.load(web,'Lists');
context.load(web);
context.executeQueryAsync(
function(){
var lists = web.get_lists();
var listExists = false;
var e = lists.getEnumerator();
while (e.moveNext()) {
var list = e.get_current();
if(list.get_title() == listTitle) {
listExists = true;
break;
}
}
success(listExists);
},
error
);
}
用法
工具书类
function listExists(siteUrl,listTitle,success,error) {
var context = new SP.ClientContext(siteUrl);
var web = context.get_web();
context.load(web,'Lists');
context.load(web);
context.executeQueryAsync(
function(){
var lists = web.get_lists();
var listExists = false;
var e = lists.getEnumerator();
while (e.moveNext()) {
var list = e.get_current();
if(list.get_title() == listTitle) {
listExists = true;
break;
}
}
success(listExists);
},
error
);
}
var webUrl = 'http://contoso.intarnet.com';
var listTitle = 'Toolbox Links';
listExists(webUrl,listTitle,
function(listFound) {
if(!listFound){
createList(webUrl,listTitle,SP.ListTemplateType.links,
function(list){
console.log('List ' + list.get_title() + ' has been created succesfully');
},
function(sender, args) {
console.log('Error:' + args.get_message());
}
);
}
else {
console.log('List with title ' + listTitle + ' already exists');
}
}
);