public void BuildFarmXml() { // Start with the Farm Farm = SPFarm.Local; XmlElement farmNode = FarmXml.CreateElement("Farm"); FarmXml.AppendChild(farmNode); // StartTime XmlNode startTimeAttribute = FarmXml.CreateAttribute("StartTime"); startTimeAttribute.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); farmNode.Attributes.SetNamedItem(startTimeAttribute); // FarmVersion // TODO config file to store the description of SharePoint versions for lookup XmlNode versionNode = FarmXml.CreateElement("FarmVersion"); Version farmVersion = Farm.BuildVersion; versionNode.InnerText = farmVersion.ToString(); farmNode.AppendChild(versionNode); // FarmName XmlNode farmNameNode = FarmXml.CreateElement("FarmName"); string farmName = Farm.DisplayName; farmNameNode.InnerText = farmName; farmNode.AppendChild(farmNameNode); // FarmId XmlNode farmIdNode = FarmXml.CreateElement("FarmId"); Guid farmId = Farm.Id; farmIdNode.InnerText = farmId.ToString(); farmNode.AppendChild(farmIdNode); // FarmStatus XmlNode farmStatusNode = FarmXml.CreateElement("FarmStatus"); SPObjectStatus farmStatus = Farm.Status; farmStatusNode.InnerText = farmStatus.ToString(); farmNode.AppendChild(farmStatusNode); // FarmServers farmNode.AppendChild(GetServersNode(Farm.Servers, "FarmServers")); // FarmServices XmlNode farmServicesNode = FarmXml.CreateElement("FarmServices"); farmServicesNode = GetFarmServicesNode(Farm.Services, farmServicesNode); farmNode.AppendChild(farmServicesNode); // FarmSolutions XmlNode farmSolutionsNode = GetFarmSolutionsNode(Farm.Solutions); farmNode.AppendChild(farmSolutionsNode); // FarmFeatureDefinitions XmlNode farmFeatureDefinitionsNode = FarmXml.CreateElement("FarmFeatureDefinitions"); farmFeatureDefinitionsNode = GetFarmFeatureDefinitionsNode(Farm.FeatureDefinitions, farmFeatureDefinitionsNode); farmNode.AppendChild(farmFeatureDefinitionsNode); // FarmSiteCollections farmSiteCollectionsNode = FarmXml.CreateElement("FarmSiteCollections"); // WebApplications SPWebServiceCollection webServices = new SPWebServiceCollection(Farm); // note this also populates the FarmSiteCollections XmlNode farmWebApplicationsNode = GetFarmWebApplicationsNode(webServices); farmNode.AppendChild(farmWebApplicationsNode); farmNode.AppendChild(farmSiteCollectionsNode); //ApplicationPools XmlNode farmApplicationPoolsNode = GetFarmApplicationPoolsNode(webServices); farmNode.AppendChild(farmApplicationPoolsNode); // FinishTime XmlNode finishTimeAttribute = FarmXml.CreateAttribute("FinishTime"); DateTime finishTime = DateTime.Now; finishTimeAttribute.Value = finishTime.ToString("yyyy-MM-dd HH:mm:ss:fff"); farmNode.Attributes.SetNamedItem(finishTimeAttribute); // A nicer GenerationTime format XmlNode generationTimeAttribute = FarmXml.CreateAttribute("GenerationTime"); generationTimeAttribute.Value = finishTime.ToLongDateString() + " " + finishTime.ToLongTimeString(); farmNode.Attributes.SetNamedItem(generationTimeAttribute); }
/// <summary> /// Runs the specified command. /// </summary> /// <param name="command">The command.</param> /// <param name="keyValues">The key values.</param> /// <param name="output">The output.</param> /// <returns></returns> public override int Execute(string command, StringDictionary keyValues, out string output) { output = string.Empty; Logger.Verbose = true; string dbserver = Params["dbserver"].Value; string dbname = Params["dbname"].Value; if (string.IsNullOrEmpty(dbserver)) { dbserver = SPWebService.ContentService.DefaultDatabaseInstance.NormalizedDataSource; } SPWebApplication webApp = SPWebApplication.Lookup(new Uri(Params["webapp"].Value)); SPContentDatabase db = null; foreach (SPContentDatabase tempDB in webApp.ContentDatabases) { if (tempDB.Name.ToLower() == dbname.ToLower()) { db = tempDB; break; } } if (db != null) { throw new Exception("Content database already exists."); } SPObjectStatus status = (SPObjectStatus)Enum.Parse(typeof(SPObjectStatus), Params["status"].Value, true); db = webApp.ContentDatabases.Add(dbserver, dbname, null, null, int.Parse(Params["warningsitecount"].Value), int.Parse(Params["maxsites"].Value), (status == SPObjectStatus.Online?0:1)); if (Params["searchserver"].UserTypedIn && !string.IsNullOrEmpty(Params["searchserver"].Value)) { // If they specified a search server then we need to try and find a valid // matching search server using the server address property. #if SP2010 SPSearchService service = SPFarm.Local.Services.GetValue <SPSearchService>("SPSearch"); #elif SP2013 SPSearchService service = SPFarm.Local.Services.GetValue <SPSearchService>("SPSearch4"); #endif SPServiceInstance searchServiceServer = null; foreach (SPServiceInstance tempsvc in service.Instances) { if (!(tempsvc is SPSearchServiceInstance)) { continue; } if (tempsvc.Status != SPObjectStatus.Online) { continue; } if (tempsvc.Server.Address.ToLowerInvariant() == Params["searchserver"].Value.ToLowerInvariant()) { // We found a match so bug out of the loop. searchServiceServer = tempsvc; break; } } if (searchServiceServer != null) { db.SearchServiceInstance = searchServiceServer; } else { Logger.Write("Search server \"{0}\" not found.", EventLogEntryType.Warning, Params["searchserver"].Value); } } else if (Params["searchserver"].UserTypedIn) { // The user specified the searchserver switch with no value which is what we use to indicate // clearing the value. db.SearchServiceInstance = null; } db.Update(); return((int)ErrorCodes.NoError); }