Jul 22 2020 04:46 AM
I'm trying to find the right place to ask my question and I'm not having much luck, hopefully someone can help here.
I have set up a SharePoint Online site and I have created a provider hosted app. One of the features of the app is to create subsites and there are times when a subsite needs to be renamed. I can use the CSOM to create the subsite without any problems but when I try to rename I get an access denied error. If I only change the title and description of the subsite there is no problem. If I log into SharePoint Online via the browser and I use the UI to rename the URL then it works without any problem.
I am using a Microsoft 365 Developer subscription to get SharePoint Online so I can develop the provider hosted app. I don't have access to the non-developer SharePoint Online site. Is there something I'm doing wrong? Is there a limitation to renaming a subsite via code? Is there a bug in SharePoint Online that prevents renaming a subsite using CSOM?
The provider hosted website is created from the template in Visual Studio 2019. As mentioned above, I can create the subsite, I can also create list items, upload files to a document library. I just can't rename the subsite even though I can do it via the UI, i.e. https://mysubdomain.sharepoint.com/mysubsite/_layouts/15/settings.aspx.
The exception thrown includes ServerErrorTypeName = "Microsoft.SharePoint.SPException". I can get the correlation id but from what I understand that's of no use in SharePoint Online. The error Message is literally "Access denied."
Here is the code I'm using to rename the subsite:
SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
ClientContext clientContext = newClientContext(spContext.SPHostUrl)
{
Credentials = new SharePointOnlineCredentials("SPUserName", "SPPassword".ToSecureString())
};
var webUrl = "/" + request.OldProjectName.Replace(" ", "_");
var subweb = clientContext.Site.OpenWeb(webUrl);
clientContext.Load(subweb);
clientContext.ExecuteQuery();
subweb.Title = request.ProjectName;
subweb.Description = request.ProjectName;
subweb.ServerRelativeUrl = "/SomeNewName"; // <-- if I skip this line there is no error
subweb.Update();
clientContext.ExecuteQuery();
Feb 23 2021 06:30 AM
Solution
Hi David
I found that you need to set DenyAddAndCustomizePages to false on the specific sitecollection.
Like this:
Set-SPOSite -Identity https://tenant.sharepoint.com/sites/xxxxxx -DenyAddAndCustomizePages $false
Hope this helps you.
Kind regards Lasse
Feb 23 2021 08:52 PM
Feb 23 2021 06:30 AM
Solution
Hi David
I found that you need to set DenyAddAndCustomizePages to false on the specific sitecollection.
Like this:
Set-SPOSite -Identity https://tenant.sharepoint.com/sites/xxxxxx -DenyAddAndCustomizePages $false
Hope this helps you.
Kind regards Lasse