SOLVED

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

Iron Contributor

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.

2 Replies
best response confirmed by Joseph Ackerman (Iron Contributor)
Solution

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

>>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!

 


 

1 best response

Accepted Solutions
best response confirmed by Joseph Ackerman (Iron Contributor)
Solution

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

View solution in original post