Skip to content

Instantly share code, notes, and snippets.

@AsishP
Last active August 12, 2018 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsishP/388af1e2dab5aed6a55b173dba64254d to your computer and use it in GitHub Desktop.
Save AsishP/388af1e2dab5aed6a55b173dba64254d to your computer and use it in GitHub Desktop.
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