Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Sites;
using (ClientContext ctx = new ClientContext(<TenantAdminUrl>))
{
// create new "modern" team site at the url
// https://[tenant].sharepoint.com/sites/mymodernteamsite
string alias = taskParam.TargetWebUrl.Substring(Url.LastIndexOf('/') + 1);
// Before creating let's make sure the site doesn't exist
bool exists = false;
Task existtask = Task.Run(async () => exists = await ctx.AliasExistsAsync(alias));
existtask.Wait();
if (!exists)
{
log.Info("Creating site with a new Async method");
TeamSiteCollectionCreationInformation teamSiteCreation = new TeamSiteCollectionCreationInformation();
// Provide the team site para
teamSiteCreation.Alias = <GroupAlias>;
teamSiteCreation.DisplayName = <WebTitle>;
teamSiteCreation.IsPublic = <true or false>;
teamSiteCreation.Description = <Description>;
ClientContext teamcontext = ctx.CreateSiteAsync(teamSiteCreation).GetAwaiter().GetResult();
teamcontext.Load(teamcontext.Web, w => w.Lists, w => w.Url);
teamcontext.ExecuteQueryRetry();
//Call the Ensure Site Assets Library method to initialize the Site Assets library
teamcontext.Web.Lists.EnsureSiteAssetsLibrary();
teamcontext.ExecuteQueryRetry();
log.Info("The site has been successfully created at: " + teamcontext.Web.Url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment