Feb 06 2017 05:56 PM
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.
Feb 06 2017 10:57 PM - edited Feb 06 2017 11:21 PM
SolutionYou 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()); }
Feb 08 2017 03:18 PM
>>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!