I have an autohosted app that has a tenant FullControl permission. I have the following code running out of a remote event receiver that executes when some item in a list is modified.
//var token = TokenHelper.GetAppOnlyAccessToken(SHAREPOINT_PID, parentSite.Authority, null).AccessToken;
var token = TokenHelper.GetAppOnlyAccessToken(SHAREPOINT_PID, parentSite.Authority, null).AccessToken;
// bool subsiteSuccesfullyCreated = false;
using (var clientContextOSC = TokenHelper.GetClientContextWithAccessToken(parentSite.ToString(), token))
{
//we use the default team site template
String webTemplateName = "STS#0";
//TO DO:haal templatelijst op van clienttemplate web
//
Web web = clientContextOSC.Web;
clientContextOSC.Load(web);
clientContextOSC.ExecuteQuery();
////1033 english
WebTemplateCollection templates = web.GetAvailableWebTemplates(1043, true);
clientContextOSC.Load(templates);
clientContextOSC.ExecuteQuery();
foreach (WebTemplate wt in templates)
{
if (wt.Title == contentTypeString)
{
webTemplateName = wt.Name;
break;
}
}
WebCreationInformation wcInfo = new WebCreationInformation();
wcInfo.Title = naamSubsite;
wcInfo.Description = naamSubsite;
wcInfo.Url = naamSubsite;
wcInfo.Language = 1043;
wcInfo.UseSamePermissionsAsParentSite = true;
wcInfo.WebTemplate = webTemplateName;
Web newWeb = clientContextOSC.Web.Webs.Add(wcInfo);
clientContextOSC.Load(newWeb, w => w.Title);
clientContextOSC.ExecuteQuery();
parentSite points to another site collection in the same O365 tenant: I can load & access the website (web) itself (even modify the title) but I'm unable to load the web templates. When debugging this part of the code I get an UnauthorizedAccessException
Access denied. You are not authorized to perform this action or access this resource.
when executing the getwebtemplates code.
When I grey out the gettemplates & run the site creation part of the code I get another exception:
ServerException was unhandled by user code. Value does not fall within the expected range
When I execute this code out of a console app with a clientcontext created using the same parentsite and using SharePointOnlineCredentials (as admin) this code runs flawlessly
The realm is filled in in the web.config file and responds to the office 365 tenant's realm so I don't think this is the problem.
Anyone any ideas as to what I'm doing wrong/what I'm not seeing? Any hints or suggestions towards fixing this?