Saturday 6 September 2014

Nav items using CSOM

$(document).ready(function () { SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { pageloaddetails(); }); });

    //This function is called on the page load and it fetches the static information for the Navigation bar
    function pageloaddetails() {
     try {

            var context = new SP.ClientContext.get_current();           
                                      var web = context.get_web();
                                     
            var list = web.get_lists().getByTitle('Navigation Configuration');           
            var query = '';
            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml(query);
            this.listcollection = list.getItems(camlQuery);
            context.load(this.listcollection, 'Include(Title,ItemText)');
            context.executeQueryAsync(Function.createDelegate(this, this.Received), Function.createDelegate(this, this.failed));
            //return false;
        }
        catch (e) {
            alert(e);
        }
    }
    //This method is called on the success of pageload function.
    function Received() {
        var title = "";
        var htmlNav = "";
       
        var ListEnumerator = this.listcollection.getEnumerator();      
        while (ListEnumerator.moveNext()) {
            var currentItem = ListEnumerator.get_current();
            if (currentItem.get_item('Title') != null) {
                if (currentItem.get_item('Title') != "") {
                       if  ( currentItem.get_item('Title') == "Topnav"){
                    htmlNav = currentItem.get_item('ItemText');
                    }                   
                }
            }
          document.getElementById("topNavDiv").innerHTML = htmlNav;
        }
       
    }
    function failed(sender, args) {
        alert('failed. Message:' + args.get_message());

    }

No comments:

Post a Comment