public void ExtendVirtualServer(SharePointSite site, bool exclusiveNTLM) { try { WindowsImpersonationContext wic = WindowsIdentity.GetCurrent().Impersonate(); string siteUrl = "http://" + site.Name; // check input parameters if (String.IsNullOrEmpty(site.RootFolder) || !Directory.Exists(site.RootFolder)) { throw new Exception("Could not create SharePoint site, because web site root folder does not exist. Open web site properties and click \"Update\" button to re-create site folder."); } SPWebApplication app = SPWebApplication.Lookup(new Uri(siteUrl)); if (app != null) { throw new Exception("SharePoint is already installed on this web site."); } //SPFarm farm = SPFarm.Local; SPFarm farm = SPFarm.Local; SPWebApplicationBuilder builder = new SPWebApplicationBuilder(farm); builder.ApplicationPoolId = site.ApplicationPool; builder.DatabaseServer = site.DatabaseServer; builder.DatabaseName = site.DatabaseName; builder.DatabaseUsername = site.DatabaseUser; builder.DatabasePassword = site.DatabasePassword; builder.ServerComment = site.Name; builder.HostHeader = site.Name; builder.Port = 80; builder.RootDirectory = new DirectoryInfo(site.RootFolder); builder.DefaultZoneUri = new Uri(siteUrl); builder.UseNTLMExclusively = exclusiveNTLM; app = builder.Create(); app.Name = site.Name; app.Sites.Add(siteUrl, null, null, (uint)site.LocaleID, null, site.OwnerLogin, null, site.OwnerEmail); app.Update(); app.Provision(); wic.Undo(); } catch (Exception ex) { try { // try to delete app if it was created SPWebApplication app = SPWebApplication.Lookup(new Uri("http://" + site.Name)); if (app != null) { app.Delete(); } } catch { /* nop */ } throw new Exception("Error creating SharePoint site", ex); } }