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;


                ////1033 english
                WebTemplateCollection templates = web.GetAvailableWebTemplates(1043, true);

                foreach (WebTemplate wt in templates)
                    if (wt.Title == contentTypeString)
                        webTemplateName = wt.Name;

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

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?

  • Did you try to perform the same operation from a user's context? Probably a user who is a tenant admin. If you can do it successfully then we can narrow down the issue to the App Only Policy. Nov 8, 2013 at 19:32
  • I'll try that and report back here. Thx Nov 11, 2013 at 11:15

1 Answer 1


Appears I wasn't doing anything wrong. My code went back to normal execution this afternoon. Something was altered behind the scenes at microsoft apparently.

( Also found some other people experiencing the same issues, see comments at: http://blogs.msdn.com/b/richard_dizeregas_blog/archive/2013/04/04/self-service-site-provisioning-using-apps-for-sharepoint-2013.aspx )

Now working as intended so I guess that solves it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.