使用ecmascript创建Sharepoint 2010列表

时间:2013-07-25 04:33:57

标签: sharepoint sharepoint-2010 sharepoint-designer ecmascript-5 client-scripting

我是ECMAScript和共享点开发的新手,我有一个小要求我需要使用ECMAScript创建一个列表,而创建它必须检查列表是否已经存在于该站点中,如果列表不存在新列表已有创造。

2 个答案:

答案 0 :(得分:1)

您可以将SPServices与“GetListCollection”一起使用来查找Sharepoint中的所有列表,然后使用“AddList”创建它。

类似的东西:

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
      })
    }
  }
});

请务必正确阅读网站,尤其是FAQ。您需要在代码中包含jQuery和SPServices。

答案 1 :(得分:1)

您可以将JSOMSOAP Services用于此目的,下面将演示JSOM解决方案。

如何在SharePoint 2010中使用JSOM创建List

function createList(siteUrl,listTitle,listTemplateType,success,error) {
    var context = new SP.ClientContext(siteUrl);
    var web = context.get_web();

    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(listTitle);
    listCreationInfo.set_templateType(listTemplateType);

    var list = web.get_lists().add(listCreationInfo);
    context.load(list);
    context.executeQueryAsync(
        function(){
            success(list);
        }, 
        error
    );
}

如何确定Web中是否存在列表

不幸的是,JSOM API不包含任何"内置"确定列表是否存在的方法,但您可以使用以下方法。

一种解决方案是使用列表集合加载Web对象,然后遍历列表集合以查找特定列表:

context.load(web, 'Lists');

解决方案

以下示例演示如何通过JSOM确定List是否存在:

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'); 
     }

   }   
);

参考

How to: Complete basic operations using JavaScript library code in SharePoint 2013

相关问题