How to apply site-design in SharePoint Online?
-
09-02-2021 - |
Question
Does any one know how to apply the custom site design to the team site collection URL in SharePoint online through client side object model?
Solution
You need to use the ApplySiteDesign
method of the Tenant class.
In that method, you need to pass the site design's Id. For that you first need fetch the site Design id that you want to apply and then use the ApplySiteDesign method to execute the site design on the site collection.
The code for that would be as below:
var tenantUrl = "https://<tenant>-admin.sharepoint.com";
var tenantContext = new ClientContext(tenantUrl));
var tenant = new Tenant(tenantContext);
var existingDesigns = tenant.GetSiteDesigns();
tenantContext.Load(existingDesigns);
tenantContext.ExecuteQueryRetry();
var existingSiteDesign = existingDesigns.FirstOrDefault(d => d.Title == "Your-site-design-name");
var results = tenant.ApplySiteDesign("<your-site-collection-url>", existingSiteDesign.Id);
tenantContext.Load(results);
tenantContext.ExecuteQueryRetry();
If there are >30 actions, you can se SiteDesignTask
as below:
var designTask = tenant.AddSiteDesignTask(tenantContext, "<your-site-collection-url>", existingSiteDesign.Id);
tenantContext.Load(designTask);
tenantContext.ExecuteQueryRetry();
References -
You need to be atleast SharePoint admin to execute this code, being a site collection admin alone is not sufficient if you want to do that via CSOM.
OTHER TIPS
Using CSOM, you can use tenant.applySiteDesign()
. You need to pass the web URL and site design ID to this method.
Syntax:
public ClientObjectList<TenantSiteScriptActionResult> ApplySiteDesign(
string webUrl,
Guid siteDesignId
)
Official documentation can be found here:
Also, you can get complete code at: The Ultimate Guide to SharePoint Site Designs and Site Scripts