/// <summary> /// Provisions a Subweb at the specified location, based on a previously exported template file (.cmp) /// in the path specified. /// </summary> /// <param name="destinationSiteUrl">The Parent Website the new site should be created under, i.e.: 'http://sharepoint/sites/MyCollection/MyParentWeb'</param> /// <param name="destinationWebUrl">The relative URL to assign to the imported website. i.e.: 'MyNewWebSite'</param> /// <param name="pathToTemplateFile">The File Location where an existing template file with extension .CMP can be found. i.e.: @"C:\MyPath\Templates\SiteTemplate.cmp"</param> /// <returns>Boolean, True or False, indicative of success.</returns> public static bool importSpWeb(string destinationSiteUrl, string destinationWebUrl, string pathToTemplateFile) { bool success = false; string importedSite = null; try { using (SPSite site = new SPSite(destinationSiteUrl)) { using (SPWeb web = site.OpenWeb()) { SPImportSettings importSettings = new SPImportSettings(); importSettings.CommandLineVerbose = true; // uncomment for testing importSettings.SiteUrl = site.Url; importSettings.WebUrl = web.Url; importSettings.FileLocation = Path.GetDirectoryName(pathToTemplateFile); importSettings.BaseFileName = Path.GetFileName(pathToTemplateFile); importSettings.FileCompression = true; importSettings.IncludeSecurity = SPIncludeSecurity.All; importSettings.Validate(); SPImport import = new SPImport(importSettings); import.ObjectImported += delegate(object delegateSender, SPObjectImportedEventArgs delegateArgs) { if (string.IsNullOrEmpty(importedSite)) { importedSite = delegateArgs.TargetUrl; } return; }; import.Run(); // The above brought in a new web, but it's identical to the template, including the URL and title. // So let's get the new web and set its URL appropriately, and change the title to a temporary one. using (SPWeb newWeb = site.OpenWeb(importedSite, true)) { newWeb.Name = destinationWebUrl; newWeb.AllProperties.SetProperty("Site_Created", DateTime.Now.ToString()); newWeb.Title = "Temporary Import Generated at " + newWeb.AllProperties["Site_Created"].ToString(); newWeb.Update(); } success = true; } } } catch (Exception ex) { success = false; log.addError("Could not create site from imported template file: " + ex.ToString()); return(success); } return(success); }
public void Validate() { m_importSettings.Validate(); }