public static bool DoesSiteExist(this Tenant tenant, string siteFullUrl) { try { return tenant.CheckIfSiteExists(siteFullUrl, SITE_STATUS_ACTIVE) || tenant.CheckIfSiteExists(siteFullUrl, SITE_STATUS_CREATING) || tenant.CheckIfSiteExists(siteFullUrl, SITE_STATUS_RECYCLED); } catch (Exception ex) { if (ex is Microsoft.SharePoint.Client.ServerException && (ex.Message.IndexOf("Unable to access site") != -1 || ex.Message.IndexOf("Cannot get site") != -1)) { return true; } else { LoggingUtility.Internal.TraceError((int)EventId.UnknownExceptionAccessingSite, ex, CoreResources.TenantExtensions_UnknownExceptionAccessingSite); } return false; } }
/// <summary> /// Adds a SiteEntity by launching site collection creation and waits for the creation to finish /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="properties">Describes the site collection to be created</param> /// <param name="removeFromRecycleBin">It true and site is present in recycle bin, it will be removed first from the recycle bin</param> /// <param name="wait">If true, processing will halt until the site collection has been created</param> /// <returns>Guid of the created site collection and Guid.Empty is the wait parameter is specified as false</returns> public static Guid CreateSiteCollection(this Tenant tenant, SiteEntity properties, bool removeFromRecycleBin = false, bool wait = true) { if (removeFromRecycleBin) { if (tenant.CheckIfSiteExists(properties.Url, SITE_STATUS_RECYCLED)) { tenant.DeleteSiteCollectionFromRecycleBin(properties.Url); } } SiteCreationProperties newsite = new SiteCreationProperties(); newsite.Url = properties.Url; newsite.Owner = properties.SiteOwnerLogin; newsite.Template = properties.Template; newsite.Title = properties.Title; newsite.StorageMaximumLevel = properties.StorageMaximumLevel; newsite.StorageWarningLevel = properties.StorageWarningLevel; newsite.TimeZoneId = properties.TimeZoneId; newsite.UserCodeMaximumLevel = properties.UserCodeMaximumLevel; newsite.UserCodeWarningLevel = properties.UserCodeWarningLevel; newsite.Lcid = properties.Lcid; try { SpoOperation op = tenant.CreateSite(newsite); tenant.Context.Load(tenant); tenant.Context.Load(op, i => i.IsComplete, i => i.PollingInterval); tenant.Context.ExecuteQuery(); if (wait) { WaitForIsComplete(tenant, op); } } catch (Exception ex) { // Eat the siteSubscription exception to make the same code work for MT as on-prem April 2014 CU+ if (ex.Message.IndexOf("Parameter name: siteSubscription") == -1) { throw; } } // Get site guid and return. If we create the site asynchronously, return an empty guid as we cannot retrieve the site by URL yet. Guid siteGuid = Guid.Empty; if (wait) { siteGuid = tenant.GetSiteGuidByUrl(new Uri(properties.Url)); } return siteGuid; }
/// <summary> /// Adds a SiteEntity by launching site collection creation and waits for the creation to finish /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="properties">Describes the site collection to be created</param> /// <param name="removeFromRecycleBin">It true and site is present in recycle bin, it will be removed first from the recycle bin</param> /// <param name="wait">If true, processing will halt until the site collection has been created</param> /// <returns>Guid of the created site collection and Guid.Empty is the wait parameter is specified as false</returns> public static Guid CreateSiteCollection(this Tenant tenant, SiteEntity properties, bool removeFromRecycleBin = false, bool wait = true) { if (removeFromRecycleBin) { if (tenant.CheckIfSiteExists(properties.Url, SITE_STATUS_RECYCLED)) { tenant.DeleteSiteCollectionFromRecycleBin(properties.Url); } } SiteCreationProperties newsite = new SiteCreationProperties(); newsite.Url = properties.Url; newsite.Owner = properties.SiteOwnerLogin; newsite.Template = properties.Template; newsite.Title = properties.Title; newsite.StorageMaximumLevel = properties.StorageMaximumLevel; newsite.StorageWarningLevel = properties.StorageWarningLevel; newsite.TimeZoneId = properties.TimeZoneId; newsite.UserCodeMaximumLevel = properties.UserCodeMaximumLevel; newsite.UserCodeWarningLevel = properties.UserCodeWarningLevel; newsite.Lcid = properties.Lcid; SpoOperation op = tenant.CreateSite(newsite); tenant.Context.Load(tenant); tenant.Context.Load(op, i => i.IsComplete, i => i.PollingInterval); tenant.Context.ExecuteQueryRetry(); // Get site guid and return. If we create the site asynchronously, return an empty guid as we cannot retrieve the site by URL yet. Guid siteGuid = Guid.Empty; if (wait) { // Let's poll for site collection creation completion WaitForIsComplete(tenant, op); // Add delay to avoid race conditions Thread.Sleep(30 * 1000); // Return site guid of created site collection siteGuid = tenant.GetSiteGuidByUrl(new Uri(properties.Url)); } return siteGuid; }
/// <summary> /// Checks if a sub site exists /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="siteFullUrl">URL to the sub site</param> /// <returns>True if existing, false if not</returns> public static bool SubSiteExists(this Tenant tenant, string siteFullUrl) { try { return tenant.CheckIfSiteExists(siteFullUrl, "Active"); } catch (Exception ex) { if (IsCannotGetSiteException(ex) || IsUnableToAccessSiteException(ex)) { return true; } else { return false; } } }
/// <summary> /// Checks if a site collection is Active /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="siteFullUrl">URL to the site collection</param> /// <returns>True if active, false if not</returns> public static bool IsSiteActive(this Tenant tenant, string siteFullUrl) { try { return tenant.CheckIfSiteExists(siteFullUrl, "Active"); } catch (Exception ex) { if (IsCannotGetSiteException(ex)) { return false; } Log.Error(CoreResources.TenantExtensions_UnknownExceptionAccessingSite, ex.Message); throw; } }
/// <summary> /// Adds a SiteEntity by launching site collection creation and waits for the creation to finish /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="properties">Describes the site collection to be created</param> /// <param name="removeFromRecycleBin">It true and site is present in recycle bin, it will be removed first from the recycle bin</param> /// <param name="wait">If true, processing will halt until the site collection has been created</param> /// <returns>Guid of the created site collection and Guid.Empty is the wait parameter is specified as false</returns> public static Guid CreateSiteCollection(this Tenant tenant, SiteEntity properties, bool removeFromRecycleBin = false, bool wait = true) { if (removeFromRecycleBin) { if (tenant.CheckIfSiteExists(properties.Url, SITE_STATUS_RECYCLED)) { tenant.DeleteSiteCollectionFromRecycleBin(properties.Url); } } SiteCreationProperties newsite = new SiteCreationProperties(); newsite.Url = properties.Url; newsite.Owner = properties.SiteOwnerLogin; newsite.Template = properties.Template; newsite.Title = properties.Title; newsite.StorageMaximumLevel = properties.StorageMaximumLevel; newsite.StorageWarningLevel = properties.StorageWarningLevel; newsite.TimeZoneId = properties.TimeZoneId; newsite.UserCodeMaximumLevel = properties.UserCodeMaximumLevel; newsite.UserCodeWarningLevel = properties.UserCodeWarningLevel; newsite.Lcid = properties.Lcid; SpoOperation op = tenant.CreateSite(newsite); tenant.Context.Load(tenant); tenant.Context.Load(op, i => i.IsComplete, i => i.PollingInterval); tenant.Context.ExecuteQueryRetry(); // Get site guid and return. If we create the site asynchronously, return an empty guid as we cannot retrieve the site by URL yet. Guid siteGuid = Guid.Empty; if (wait) { // Let's poll for site collection creation completion WaitForIsComplete(tenant, op); // Add delay to avoid race conditions Thread.Sleep(30 * 1000); // Return site guid of created site collection try { siteGuid = tenant.GetSiteGuidByUrl(new Uri(properties.Url)); } catch(Exception ex) { // Eat all exceptions cause there's currently (December 16) an issue in the service that can make this call fail in combination with app-only usage Log.Error("Temp eating exception to issue in service (December 2016). Exception is {0}.", ex.ToDetailedString()); } } return siteGuid; }
public static Guid AddSiteCollection(this Tenant tenant, SiteEntity properties, bool removeFromRecycleBin = false, bool wait = true) { if (removeFromRecycleBin) { if (tenant.CheckIfSiteExists(properties.Url, SITE_STATUS_RECYCLED)) { tenant.DeleteSiteCollectionFromRecycleBin(properties.Url); } } SiteCreationProperties newsite = new SiteCreationProperties(); newsite.Url = properties.Url; newsite.Owner = properties.SiteOwnerLogin; newsite.Template = properties.Template; newsite.Title = properties.Title; newsite.StorageMaximumLevel = properties.StorageMaximumLevel; newsite.StorageWarningLevel = properties.StorageWarningLevel; newsite.TimeZoneId = properties.TimeZoneId; newsite.UserCodeMaximumLevel = properties.UserCodeMaximumLevel; newsite.UserCodeWarningLevel = properties.UserCodeWarningLevel; newsite.Lcid = properties.Lcid; try { SpoOperation op = tenant.CreateSite(newsite); tenant.Context.Load(tenant); tenant.Context.Load(op, i => i.IsComplete, i => i.PollingInterval); tenant.Context.ExecuteQuery(); if (wait) { //check if site creation operation is complete while (!op.IsComplete) { System.Threading.Thread.Sleep(op.PollingInterval); op.RefreshLoad(); if (!op.IsComplete) { try { tenant.Context.ExecuteQuery(); } catch (WebException webEx) { // Context connection gets closed after action completed. // Calling ExecuteQuery again returns an error which can be ignored LoggingUtility.LogWarning(MSG_CONTEXT_CLOSED, webEx, EventCategory.Site); } } } } } catch (Exception ex) { // Eat the siteSubscription exception to make the same code work for MT as on-prem April 2014 CU+ if (ex.Message.IndexOf("Parameter name: siteSubscription") == -1) { throw ex; } } // Get site guid and return var siteGuid = tenant.GetSiteGuidByUrl(new Uri(properties.Url)); return siteGuid; }
/// <summary> /// Checks if a sub site exists /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="siteFullUrl">URL to the sub site</param> /// <returns>True if existing, false if not</returns> public static bool SubSiteExists(this Tenant tenant, string siteFullUrl) { try { return tenant.CheckIfSiteExists(siteFullUrl, ""); } catch (Exception ex) { if (ex is Microsoft.SharePoint.Client.ServerException && (ex.Message.IndexOf("Unable to access site") != -1 || ex.Message.IndexOf("Cannot get site") != -1)) { return true; } else { return false; } } }
/// <summary> /// Checks if a site collection is Active /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="siteFullUrl">URL to the site collection</param> /// <returns>True if active, false if not</returns> public static bool IsSiteActive(this Tenant tenant, string siteFullUrl) { try { return tenant.CheckIfSiteExists(siteFullUrl, "Active"); } catch (Exception ex) { if (ex.Message.StartsWith("Cannot get site")) { return false; } LoggingUtility.LogError("Error finding if site is active tenant.", ex, EventCategory.Site); throw; } }
/// <summary> /// Checks if a site collection exists /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="siteFullUrl">URL to the site collection</param> /// <returns>True if existing, false if not</returns> public static bool DoesSiteExist(this Tenant tenant, string siteFullUrl) { try { return tenant.CheckIfSiteExists(siteFullUrl, SITE_STATUS_ACTIVE) || tenant.CheckIfSiteExists(siteFullUrl, SITE_STATUS_CREATING) || tenant.CheckIfSiteExists(siteFullUrl, SITE_STATUS_RECYCLED); } catch (Exception ex) { if (ex is Microsoft.SharePoint.Client.ServerException && (ex.Message.IndexOf("Unable to access site") != -1 || ex.Message.IndexOf("Cannot get site") != -1)) { return true; } else LoggingUtility.LogError("Could not determine if site exists in tenant.", ex, EventCategory.Site); return false; } }
/// <summary> /// Checks if a site collection is Active /// </summary> /// <param name="tenant">A tenant object pointing to the context of a Tenant Administration site</param> /// <param name="siteFullUrl">URL to the site collection</param> /// <returns>True if active, false if not</returns> public static bool IsSiteActive(this Tenant tenant, string siteFullUrl) { try { return tenant.CheckIfSiteExists(siteFullUrl, "Active"); } catch (Exception ex) { if (ex.Message.StartsWith("Cannot get site")) { return false; } LoggingUtility.Internal.TraceError((int)EventId.UnknownExceptionAccessingSite, ex, CoreResources.TenantExtensions_UnknownExceptionAccessingSite); throw; } }