Forum Discussion
Joseph Ackerman
Feb 07, 2017Iron Contributor
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...
- 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()); }
paulpascha
Feb 07, 2017Bronze 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
Feb 08, 2017Iron 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!