Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:


Recycle Bin | MMS | ManagedVsCrawledProperties |

Thursday, January 17, 2013

Check whether the list exists or not using ECMAScript

In SharePoint 2013 when you create any SharePoint Hosted App it will create separate web for the app you created. You can find it easily by verifying the URL at the browser.
1) SPHostUrl 2) SPAppWebUrl

Below code helps you to check whether the lists exists or not from the SPAppWebUrl.
please refer the link if you want to get the SPHostUrl.
$(document).ready(function () {
  context = SP.ClientContext.get_current();
    web = context.get_web();
    GetLists();
});

//To get the all the lists in a web
function GetLists()
{
    this.lists = web.get_lists();
    context.load(lists);
    context.executeQueryAsync(Function.createDelegate(this, this.IsListsExists), Function.createDelegate(this, this.onQueryFailed));
}

//Check whether the lists already exists or not?
function IsListsExists() {
    //alert('IsListExists');
    var isListAvail= false;
    var listEnumerator = lists.getEnumerator();
    while (listEnumerator.moveNext()) {
        list = listEnumerator.get_current();
        if (list.get_title() == 'MyListName') {
            isListAvail = true;
        }
    }
  //If list not exists, create the list
    if (!isListAvail) {
        CreateList();
    }
}
function onQueryFailed(sender, args) {
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}

No comments:

Post a Comment