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"); } }