Tuesday, 27 September 2016

Get All Lists Display and Internal Names and bind to Dropdown from Office 365 (SharePoint ) website using JSOM

Problem Statement- Need to Load all the Lists from current web in a drop down

Solution: Find below the snippet to get all List and bind to drop down.

Drop down option will have Text as Display name and Value as Internal Name of the list. I am aware for some people this is cakewalk but for first timer might be helpful as below snippet take care of A sync call as well.

code :
// Load dropdown function 
loadDropDown:function(){

//Call getAllLists method and then pass the ListCollection to Bind to dropdown
   getAllLists().then(function (listCollection) {
            bindListDropDown(listCollection);
        }
}

  /*********************************************************************************************************
* Name: getAllLists
* Description: Get all List from current web
* Parameters: None
*********************************************************************************************************/
    getAllLists: function () {
        try {
            var context = new SP.ClientContext.get_current(),
                 web = context.get_web(),
                 dfd = new $.Deferred(),
                 relatedInformationList,
                 listCollection;
            listCollection = web.get_lists();
            context.load(listCollection, "Include(Title, DefaultViewUrl,BaseTemplate)");
            context.executeQueryAsync(
                                      function () {
                                          return (dfd.resolve(listCollection));
                                      },
                                      function () {

                                          return dfd.reject();
                                      });
            return dfd.promise();
        }
        catch (error) {
          //error log
        }
    },
    /*********************************************************************************************************
* Name: bindListDropDown
* Description: Bind al the Lists to the DropDown
* Parameters: listCollection
*********************************************************************************************************/
    bindListDropDown: function (listCollection) {
        var listCollectionEnumerator = listCollection.getEnumerator(),
            listName = "",
            listInternalName = "",
            option = $("
      
        $("#ddlListName").append(option);
        while (listCollectionEnumerator.moveNext()) {
            {
                var baseTemplate = listCollectionEnumerator.get_current().get_baseTemplate();
                var defaultViewUrl = listCollectionEnumerator.get_current().get_defaultViewUrl();
                defaultViewUrl = defaultViewUrl.replace(_spPageContextInfo.webServerRelativeUrl, "");
                defaultViewUrl = defaultViewUrl.split("/");

                if (baseTemplate == 100) // get only custom list to be displayed if you want Document //Library only use 101
{
                    listInternalName = defaultViewUrl[2];
                    $("#ddlListName").append($("").val(listCollectionEnumerator.get_current().get_title()).html(listCollectionEnumerator.get_current().get_title()));
                }
            }
        }

    },

Let me know if you have any query.





No comments:

Post a Comment