Forum Discussion

Joseph Ackerman's avatar
Joseph Ackerman
Iron Contributor
Feb 07, 2017
Solved

Can add-in get context for host web's site collection root web?

I know that I can access my host web from my add-in web using a SharePoint hosted add-in, but is there any way to get the context for the root site of site collection containing the host web?     T...
  • paulpascha's avatar
    Feb 07, 2017

    You can as long as your app has been granted the required site collection scoped permissions. 

     

    Here's some demo code I wrote that shows how to build your context

     

    var rootWeb;
    
        function getSiteCollectionTitle() {
            var scServerRelativeUrl = _spPageContextInfo.siteServerRelativeUrl;
            var clientContext = new SP.ClientContext(scServerRelativeUrl);
            rootWeb = clientContext.get_web();
    
            clientContext.load(rootWeb);
            clientContext.executeQueryAsync(onSuccess, onFail);
        }
        function onSuccess() {
            alert('Title of Root Web:' + rootWeb.get_title());
        }
        function onFail(sender, args) {
            alert('Error: ' + args.get_message());
        }

Resources