public SPQuotaInstance(ObjectInstance prototype, SPQuota quota) : this(prototype) { if (quota == null) { throw new ArgumentNullException("quota"); } m_quota = quota; }
public void UpdateQuotas(Uri root, string url, long maxStorage, long warningStorage) { WindowsImpersonationContext wic = null; try { wic = WindowsIdentity.GetCurrent().Impersonate(); SPWebApplication rootWebApplication = SPWebApplication.Lookup(root); SPQuota quota = new SPQuota(); if (maxStorage != -1) { quota.StorageMaximumLevel = maxStorage * 1024 * 1024; } else { quota.StorageMaximumLevel = 0; } if (warningStorage != -1 && maxStorage != -1) { quota.StorageWarningLevel = Math.Min(warningStorage, maxStorage) * 1024 * 1024; } else { quota.StorageWarningLevel = 0; } rootWebApplication.GrantAccessToProcessIdentity(WindowsIdentity.GetCurrent().Name); rootWebApplication.Sites[url].Quota = quota; } catch (Exception ex) { HostedSolutionLog.LogError(ex); throw; } finally { if (wic != null) { wic.Undo(); } } }
/// <summary> /// Creates site collection within predefined root web application. /// </summary> /// <param name="rootWebApplicationUri">Root web application uri.</param> /// <param name="siteCollection">Information about site coolection to be created.</param> /// <exception cref="InvalidOperationException">Is thrown in case requested operation fails for any reason.</exception> public void CreateSiteCollection(Uri rootWebApplicationUri, SharePointSiteCollection siteCollection) { WindowsImpersonationContext wic = null; try { wic = WindowsIdentity.GetCurrent().Impersonate(); SPWebApplication rootWebApplication = SPWebApplication.Lookup(rootWebApplicationUri); string siteCollectionUrl = String.Format("{0}:{1}", siteCollection.Url, rootWebApplicationUri.Port); SPQuota spQuota; SPSite spSite = rootWebApplication.Sites.Add(siteCollectionUrl, siteCollection.Title, siteCollection.Description, (uint)siteCollection.LocaleId, String.Empty, siteCollection.OwnerLogin, siteCollection.OwnerName, siteCollection.OwnerEmail, null, null, null, true); try { spQuota = new SPQuota(); if (siteCollection.MaxSiteStorage != -1) { spQuota.StorageMaximumLevel = siteCollection.MaxSiteStorage * 1024 * 1024; } if (siteCollection.WarningStorage != -1 && siteCollection.MaxSiteStorage != -1) { spQuota.StorageWarningLevel = Math.Min(siteCollection.WarningStorage, siteCollection.MaxSiteStorage) * 1024 * 1024; } } catch (Exception) { rootWebApplication.Sites.Delete(siteCollectionUrl); throw; } try { rootWebApplication.GrantAccessToProcessIdentity(WindowsIdentity.GetCurrent().Name); spSite.Quota = spQuota; } catch (Exception) { rootWebApplication.Sites.Delete(siteCollectionUrl); DeleteQuotaTemplate(siteCollection.Title); throw; } rootWebApplication.Update(true); } catch (Exception ex) { HostedSolutionLog.LogError(ex); throw; } finally { if (wic != null) { wic.Undo(); } } }
/// <summary> /// Creates site collection within predefined root web application. /// </summary> /// <param name="rootWebApplicationUri">Root web application uri.</param> /// <param name="siteCollection">Information about site coolection to be created.</param> /// <exception cref="InvalidOperationException">Is thrown in case requested operation fails for any reason.</exception> public void CreateSiteCollection(Uri rootWebApplicationUri, SharePointSiteCollection siteCollection) { WindowsImpersonationContext wic = null; HostedSolutionLog.LogStart("CreateSiteCollection"); try { wic = WindowsIdentity.GetCurrent().Impersonate(); SPWebApplication rootWebApplication = SPWebApplication.Lookup(rootWebApplicationUri); string siteCollectionUrl = String.Format("{0}:{1}", siteCollection.Url, rootWebApplicationUri.Port); HostedSolutionLog.DebugInfo("rootWebApplicationUri: {0}", rootWebApplicationUri); HostedSolutionLog.DebugInfo("siteCollectionUrl: {0}", siteCollectionUrl); SPQuota spQuota; SPSite spSite = rootWebApplication.Sites.Add(siteCollectionUrl, siteCollection.Title, siteCollection.Description, (uint)siteCollection.LocaleId, String.Empty, siteCollection.OwnerLogin, siteCollection.OwnerName, siteCollection.OwnerEmail, null, null, null, true); try { spQuota = new SPQuota(); if (siteCollection.MaxSiteStorage != -1) { spQuota.StorageMaximumLevel = siteCollection.MaxSiteStorage * 1024 * 1024; } if (siteCollection.WarningStorage != -1 && siteCollection.MaxSiteStorage != -1) { spQuota.StorageWarningLevel = Math.Min(siteCollection.WarningStorage, siteCollection.MaxSiteStorage) * 1024 * 1024; } } catch (Exception) { rootWebApplication.Sites.Delete(siteCollectionUrl); throw; } try { rootWebApplication.GrantAccessToProcessIdentity(WindowsIdentity.GetCurrent().Name); spSite.Quota = spQuota; } catch (Exception) { rootWebApplication.Sites.Delete(siteCollectionUrl); DeleteQuotaTemplate(siteCollection.Title); throw; } rootWebApplication.Update(true); try { if (siteCollection.RootWebApplicationInteralIpAddress != string.Empty) { string dirPath = FileUtils.EvaluateSystemVariables(@"%windir%\system32\drivers\etc"); string path = dirPath + "\\hosts"; if (FileUtils.FileExists(path)) { string content = FileUtils.GetFileTextContent(path); content = content.Replace("\r\n", "\n").Replace("\n\r", "\n"); string[] contentArr = content.Split(new char[] { '\n' }); bool bRecordExist = false; foreach (string s in contentArr) { if (s != string.Empty) { string IPAddr = string.Empty; string hostName = string.Empty; if (s[0] != '#') { bool bSeperator = false; foreach (char c in s) { if ((c != ' ') & (c != '\t')) { if (bSeperator) { hostName += c; } else { IPAddr += c; } } else { bSeperator = true; } } if (hostName.ToLower() == siteCollection.RootWebApplicationFQDN.ToLower()) { bRecordExist = true; break; } } } } if (!bRecordExist) { string outPut = string.Empty; foreach (string o in contentArr) { if (o != string.Empty) { outPut += o + "\r\n"; } } outPut += siteCollection.RootWebApplicationInteralIpAddress + '\t' + siteCollection.RootWebApplicationFQDN + "\r\n"; FileUtils.UpdateFileTextContent(path, outPut); } } } } catch (Exception ex) { HostedSolutionLog.LogError(ex); } } catch (Exception ex) { HostedSolutionLog.LogError(ex); throw; } finally { if (wic != null) { wic.Undo(); } HostedSolutionLog.LogEnd("CreateSiteCollection"); } }
/// <summary> /// Creates site collection within predefined root web application. /// </summary> /// <param name="rootWebApplicationUri">Root web application uri.</param> /// <param name="siteCollection">Information about site coolection to be created.</param> /// <exception cref="InvalidOperationException">Is thrown in case requested operation fails for any reason.</exception> public void CreateSiteCollection(Uri rootWebApplicationUri, SharePointSiteCollection siteCollection) { WindowsImpersonationContext wic = null; HostedSolutionLog.LogStart("CreateSiteCollection"); try { wic = WindowsIdentity.GetCurrent().Impersonate(); SPWebApplication rootWebApplication = SPWebApplication.Lookup(rootWebApplicationUri); string siteCollectionUrl = String.Format("{0}:{1}", siteCollection.Url, rootWebApplicationUri.Port); HostedSolutionLog.DebugInfo("rootWebApplicationUri: {0}", rootWebApplicationUri); HostedSolutionLog.DebugInfo("siteCollectionUrl: {0}", siteCollectionUrl); SPQuota spQuota; SPSite spSite = rootWebApplication.Sites.Add(siteCollectionUrl, siteCollection.Title, siteCollection.Description, (uint)siteCollection.LocaleId, String.Empty, siteCollection.OwnerLogin, siteCollection.OwnerName, siteCollection.OwnerEmail, null, null, null, true); try { spQuota = new SPQuota(); if (siteCollection.MaxSiteStorage != -1) spQuota.StorageMaximumLevel = siteCollection.MaxSiteStorage * 1024 * 1024; if (siteCollection.WarningStorage != -1 && siteCollection.MaxSiteStorage != -1) spQuota.StorageWarningLevel = Math.Min(siteCollection.WarningStorage, siteCollection.MaxSiteStorage) * 1024 * 1024; } catch (Exception) { rootWebApplication.Sites.Delete(siteCollectionUrl); throw; } try { rootWebApplication.GrantAccessToProcessIdentity(WindowsIdentity.GetCurrent().Name); spSite.Quota = spQuota; } catch (Exception) { rootWebApplication.Sites.Delete(siteCollectionUrl); DeleteQuotaTemplate(siteCollection.Title); throw; } rootWebApplication.Update(true); try { if (siteCollection.RootWebApplicationInteralIpAddress != string.Empty) { string dirPath = FileUtils.EvaluateSystemVariables(@"%windir%\system32\drivers\etc"); string path = dirPath + "\\hosts"; if (FileUtils.FileExists(path)) { string content = FileUtils.GetFileTextContent(path); content = content.Replace("\r\n", "\n").Replace("\n\r", "\n"); string[] contentArr = content.Split(new char[] { '\n' }); bool bRecordExist = false; foreach (string s in contentArr) { if (s != string.Empty) { string IPAddr = string.Empty; string hostName = string.Empty; if (s[0] != '#') { bool bSeperator = false; foreach (char c in s) { if ((c != ' ') & (c != '\t')) { if (bSeperator) hostName += c; else IPAddr += c; } else bSeperator = true; } if (hostName.ToLower() == siteCollection.RootWebApplicationFQDN.ToLower()) { bRecordExist = true; break; } } } } if (!bRecordExist) { string outPut = string.Empty; foreach (string o in contentArr) { if (o != string.Empty) outPut += o + "\r\n"; } outPut += siteCollection.RootWebApplicationInteralIpAddress + '\t' + siteCollection.RootWebApplicationFQDN + "\r\n"; FileUtils.UpdateFileTextContent(path, outPut); } } } } catch (Exception ex) { HostedSolutionLog.LogError(ex); } } catch (Exception ex) { HostedSolutionLog.LogError(ex); throw; } finally { if (wic != null) wic.Undo(); HostedSolutionLog.LogEnd("CreateSiteCollection"); } }
public void UpdateQuotas(Uri root, string url, long maxStorage, long warningStorage) { WindowsImpersonationContext wic = null; try { wic = WindowsIdentity.GetCurrent().Impersonate(); SPWebApplication rootWebApplication = SPWebApplication.Lookup(root); SPQuota quota = new SPQuota(); if (maxStorage != -1) quota.StorageMaximumLevel = maxStorage * 1024 * 1024; else quota.StorageMaximumLevel = 0; if (warningStorage != -1 && maxStorage != -1) quota.StorageWarningLevel = Math.Min(warningStorage, maxStorage) * 1024 * 1024; else quota.StorageWarningLevel = 0; rootWebApplication.GrantAccessToProcessIdentity(WindowsIdentity.GetCurrent().Name); rootWebApplication.Sites[url].Quota = quota; } catch (Exception ex) { HostedSolutionLog.LogError(ex); throw; } finally { if (wic != null) wic.Undo(); } }
/// <summary> /// Creates site collection within predefined root web application. /// </summary> /// <param name="rootWebApplicationUri">Root web application uri.</param> /// <param name="siteCollection">Information about site coolection to be created.</param> /// <exception cref="InvalidOperationException">Is thrown in case requested operation fails for any reason.</exception> public void CreateSiteCollection(Uri rootWebApplicationUri, SharePointSiteCollection siteCollection) { WindowsImpersonationContext wic = null; try { wic = WindowsIdentity.GetCurrent().Impersonate(); SPWebApplication rootWebApplication = SPWebApplication.Lookup(rootWebApplicationUri); string siteCollectionUrl = String.Format("{0}:{1}", siteCollection.Url, rootWebApplicationUri.Port); SPQuota spQuota; SPSite spSite = rootWebApplication.Sites.Add(siteCollectionUrl, siteCollection.Title, siteCollection.Description, (uint) siteCollection.LocaleId, String.Empty, siteCollection.OwnerLogin, siteCollection.OwnerName, siteCollection.OwnerEmail, null, null, null, true); try { spQuota = new SPQuota(); if (siteCollection.MaxSiteStorage != -1) spQuota.StorageMaximumLevel = siteCollection.MaxSiteStorage * 1024 * 1024; if (siteCollection.WarningStorage != -1 && siteCollection.MaxSiteStorage != -1) spQuota.StorageWarningLevel = Math.Min(siteCollection.WarningStorage, siteCollection.MaxSiteStorage) * 1024 * 1024; } catch (Exception) { rootWebApplication.Sites.Delete(siteCollectionUrl); throw; } try { rootWebApplication.GrantAccessToProcessIdentity(WindowsIdentity.GetCurrent().Name); spSite.Quota = spQuota; } catch (Exception) { rootWebApplication.Sites.Delete(siteCollectionUrl); DeleteQuotaTemplate(siteCollection.Title); throw; } rootWebApplication.Update(true); } catch(Exception ex) { HostedSolutionLog.LogError(ex); throw; } finally { if (wic != null) wic.Undo(); } }
/// <summary> /// Syncs the specified site quota with the quota template. /// </summary> /// <param name="site">The site.</param> private void Sync(SPSite site) { SPQuota currentQuota = site.Quota; SPQuotaTemplate currentTemplate = null; foreach (SPQuotaTemplate quota in m_quotaColl) { if (currentQuota.QuotaID == quota.QuotaID) { currentTemplate = quota; break; } } if (currentTemplate == null) { if (!m_setQuota) { Logger.WriteWarning("No quota template has been assigned to site {0}. Use the -setquota parameter to assign a quota.", site.Url); return; } Logger.Write("PROGRESS: Synchronizing {0}", site.Url); Logger.Write("PROGRESS: No quota template assigned to site. Assigning template...", site.Url); site.Quota = m_quota; Logger.Write("PROGRESS: Template \"{0}\" assigned to site.", m_quota.Name); return; } if (m_quota == null || (m_quota != null && currentQuota.QuotaID == m_quota.QuotaID)) { Logger.Write("PROGRESS: Synchronizing {0}", site.Url); Logger.Write("PROGRESS: Currently using template \"{0}\".", currentTemplate.Name); if (site.Quota.InvitedUserMaximumLevel == currentTemplate.InvitedUserMaximumLevel && site.Quota.StorageMaximumLevel == currentTemplate.StorageMaximumLevel && site.Quota.StorageWarningLevel == currentTemplate.StorageWarningLevel && site.Quota.UserCodeMaximumLevel == currentTemplate.UserCodeMaximumLevel && site.Quota.UserCodeWarningLevel == currentTemplate.UserCodeWarningLevel) { Logger.Write("PROGRESS: No changes necessary, quota already synchronized with template."); return; } site.Quota = currentTemplate; Logger.Write("PROGRESS: Storage maximum updated from {0}MB to {1}MB", ((currentQuota.StorageMaximumLevel / 1024) / 1024).ToString(), ((site.Quota.StorageMaximumLevel / 1024) / 1024).ToString()); Logger.Write("PROGRESS: Storage warning updated from {0}MB to {1}MB", ((currentQuota.StorageWarningLevel / 1024) / 1024).ToString(), ((site.Quota.StorageWarningLevel / 1024) / 1024).ToString()); Logger.Write("PROGRESS: User Code Maximum Level updated from {0} to {1}", currentQuota.UserCodeMaximumLevel.ToString(), site.Quota.UserCodeMaximumLevel.ToString()); Logger.Write("PROGRESS: User Code Maximum Level updated from {0} to {1}", currentQuota.UserCodeWarningLevel.ToString(), site.Quota.UserCodeWarningLevel.ToString()); Logger.Write("PROGRESS: Invited user maximum updated from {0} to {1}", currentQuota.InvitedUserMaximumLevel.ToString(), site.Quota.InvitedUserMaximumLevel.ToString()); } }