Saturday, 28 October 2017

How to get Site details in SharePoint Online?

These days, Microsoft is obsoleting code based solution from Office 365.  And these leads us to use JavaScript Object Model / Client Side Object Model to get data and add them in to content editor / script editor webpart to display data.

In this blog we will try to get current site title, web tile and any intermediate web title using CSOM. You can go deep down if you have further requirement for these objects.

Here is the basic function to get site details:

Sample code:

var currSiteTitle; // site collection details
var currWebTitle; // get sub site details
var parentWebTitle; // get any intermediate site details

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

function getWebTitle() {

        var context = new SP.ClientContext.get_current();
        var site = context.get_site();
        var web = context.get_web();
        context.load(site);
        context.load(web);
        var parentInfo = web.get_parentWeb();  
context.load(parentInfo);
        context.executeQueryAsync(function() {

                console.log(site.get_title());
                console.log(site.get_url());
                console.log(web.get_title());
                console.log(web.get_url());
                console.log("Intermediate site value: " + parentInfo.get_title());

                currSiteTitle = site.get_title();
                currWebTitle = web.get_title();
                parentWebTitle = parentInfo.get_title();
            },
            function(sender, args) {
                console.log(args.get_message());
            }
        );
     }


Original Source:
  1. https://msdn.microsoft.com/en-us/library/office/jj246780.aspx
  2. https://msdn.microsoft.com/en-us/library/office/jj246877.aspx
  3. https://sharepoint.stackexchange.com/questions/175217/get-parent-web-of-a-subsite-using-jsom-in-sharepoint-2013

No comments:

Post a Comment