SOLVED

Unable to rename SharePoint Online subsite URL via code

Copper Contributor

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

 

2 Replies
best response confirmed by davidp_1978 (Copper Contributor)
Solution

@davidp_1978 

 

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

A billion thank yous. This has worked in my testing, it should work in production but I'll keep some enthusiasm in reserve (a trillion thanks if it works in production).
I used it at the top level:
Set-SPOSite -Identity https://tenant.sharepoint.com/ -DenyAddAndCustomizePages $false

Then I tried to rename a subsite and it worked.
1 best response

Accepted Solutions
best response confirmed by davidp_1978 (Copper Contributor)
Solution

@davidp_1978 

 

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

View solution in original post