I'm currently trying add the Url to an external web service to the Web.config of my SharePoint 2010 farm. When I try to do this, I get an error stating that I am missing the connection string portion of my web.config.
Looking over the code that I am using, I'm under the impression that the code is failing to lay down the connection string section of the Web.Config.
When I try to do this I get: "Failed to apply web.config modifications to Web application '2962b355-80e1-4183-a958-d4120a94741d'. Failed to apply web.config modifications to file 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config'. Failed to apply a web.config modification to file 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config'. The specified node "configuration/connectionStrings" was not found in the web.config file. Failed to apply web.config modifications to Web application '6fe60f19-9f96-4efb-ba99-a2789354950a'. Failed to apply web.config modifications to file 'C:\inetpub\wwwroot\wss\VirtualDirectories\81\web.config'. Failed to apply a web.config modification to file 'C:\inetpub\wwwroot\wss\VirtualDirectories\81\web.config'. The specified node "configuration/connectionStrings" was not found in the web.config file."
I would rather not have to manually modify the web.config file to ease the installation of the .wsp file.
public string Create(string url)
{
var spWebService = SPWebService.ContentService;
// Define the modification(s) to the web.config
var modification1 = new SPWebConfigModification
{
Path = "configuration",
Name = "connectionStrings",
Sequence = 100,
Owner = Owner,
Value = " < connectionStrings > < /connectionStrings > ",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
};
var modification2 = new SPWebConfigModification
{
Path = "configuration/connectionStrings",
Name = string.Format("add[@name='{0}']", KeyName),
Sequence = 100,
Owner = Owner,
Value = url,
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
};
// Register the Web.config modification with the web service
spWebService.WebConfigModifications.Add(modification1);
spWebService.WebConfigModifications.Add(modification2);
// Propagate those changes across the farm
spWebService.Update();
spWebService.ApplyWebConfigModifications();
// Set the Url globally
_webServiceUrl = url;
return _webServiceUrl;
}
Update!
Changing the second web.config modification to Ensure section fixed my initial issue. However, now when I try to run the code I get:
The '[' character, hexadecimal value 0x5B, cannot be included in a name. Line 1, position 5.
Second Update
I ended up ditching the idea of storing the configuration in the Web.Config. I instead opted to use the SPFarm's Property Bag. Had that implementation up and running in minutes. Thanks for the help!