0

i am new to ECMAScript and share point development, i have small requirement i need to create one list using ECMAScript and while creation it has to check whether the list already exists in the site ,if list doesn't exist new list has to create.

2 Answers 2

1

You can use SPServices with "GetListCollection" to find all the lists in a Sharepoint, and then use "AddList" to create it.

Something like:

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

Make sure to read the website correctly, and especially the FAQ. You'll need to include jQuery and then SPServices in your code.

Sign up to request clarification or add additional context in comments.

Comments

1

You could utilize JSOM or SOAP Services for that purpose, below is demonstrated JSOM solution.

How to create a List using JSOM in SharePoint 2010

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

How to determine whether list exists in Web

Unfortunately JSOM API does not contains any "built-in" methods to determine whether list exists or not, but you could use the following approach.

One solution would be to load Web object with lists collection and then iterate through list collection to find a specific list:

context.load(web, 'Lists');

Solution

The following example demonstrates how to determine whether List exist via 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
    );
}

Usage

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

   }   
);

References

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.