private void CreateSubsite(ClientContext context, string parentWebTitle, string subsiteFolderName, WebCollection subSiteCollection, ILogger logger) { try { if (context == null || string.IsNullOrEmpty(subsiteFolderName)) { return; } string subsiteInternalTitle = RemoveEmptySpaces(subsiteFolderName); Web parentWeb = context.Web; if (parentWeb.Title != parentWebTitle) { parentWeb = subSiteCollection.SingleOrDefault(e => e.Title.Equals(parentWebTitle)); } if (parentWeb == null) { subsiteInternalTitle = RemoveEmptySpaces(parentWebTitle); context.Web.Webs.Add(new Microsoft.SharePoint.Client.WebCreationInformation() { WebTemplate = "CMSPUBLISHING#0", Title = parentWebTitle, Description = parentWebTitle, Url = subsiteInternalTitle, Language = 1033, UseSamePermissionsAsParentSite = true }); context.ExecuteQuery(); parentWeb = subSiteCollection.SingleOrDefault(e => e.Title.Equals(parentWebTitle)); } if (!subSiteCollection.Where(w => w.Title == subsiteFolderName).Any()) { //If there is no subsites with the same name as the given subsite, create it parentWeb.Webs.Add(new Microsoft.SharePoint.Client.WebCreationInformation() { WebTemplate = "CMSPUBLISHING#0", Title = subsiteFolderName, Description = subsiteFolderName, Url = subsiteInternalTitle, Language = 1033, UseSamePermissionsAsParentSite = true }); context.ExecuteQuery(); logger.LogMessage(string.Format(Constants.SubSiteCreationSuccess, subsiteFolderName), LogType.Success); } else { logger.LogMessage(string.Format(Constants.DuplicateSubsite, subsiteFolderName), LogType.Info); } } catch (Exception ex) { logger.LogMessage(string.Format(Constants.SubSiteCreationError, subsiteFolderName, ex.Message), LogType.ErrorAndContinue); } }
public static bool SubSiteExistsWithUrl(this Web web, string url) { Utility.EnsureWeb(web.Context, web, "ServerRelativeUrl"); string siteUrl = string.Format("{0}/{1}", web.ServerRelativeUrl, url).ToLowerInvariant(); WebCollection subSites = web.Webs; IEnumerable <Web> results = web.Context.LoadQuery <Web>(subSites.Where(item => item.ServerRelativeUrl == siteUrl)); web.Context.ExecuteQuery(); Web existingWeb = results.FirstOrDefault(); if (existingWeb != null) { return(true); } return(false); }