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?  

 

This add-in may be installed in multiple sub-sites of the same site collection and I would like all of the installations to share some of the resources by storing them in the root web of the site collection (so I don't have multiple copies of this floating around in every site that uses the add-in).

 

I've been unable to locate any disucssion of this anywhere.  Thanks.

  • 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());
        }

2 Replies

  • paulpascha's avatar
    paulpascha
    Bronze Contributor

    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());
        }
    • Joseph Ackerman's avatar
      Joseph Ackerman
      Iron Contributor

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

       

      That makes sense.  I should be okay in this instance because the Add-in will only be available to install and run by Site Admins with Full Control of the site collection.

       

      Thanks very much for this -- looks like this will be very helpful for me!

       


       

Resources