現在、外部 Web サービスへの URL を SharePoint 2010 ファームの Web.config に追加しようとしています。これを実行しようとすると、web.config の接続文字列部分が見つからないというエラーが表示されます。
私が使用しているコードに目を通すと、コードが Web.Config の接続文字列セクションの配置に失敗しているという印象を受けます。
これをやろうとすると、次のようになります。「web.config の変更を Web アプリケーション '2962b355-80e1-4183-a958-d4120a94741d' に適用できませんでした。web.config の変更をファイル 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config' に適用できませんでした」 . web.config の変更をファイル 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config' に適用できませんでした。指定されたノード "configuration/connectionStrings" が web.config ファイルに見つかりませんでした。 web.config の変更を Web アプリケーション '6fe60f19-9f96-4efb-ba99-a2789354950a' に適用します。web.config の変更をファイル 'C:\inetpub\wwwroot\wss\VirtualDirectories\81\web.config' に適用できませんでした。ファイル 'C:\inetpub\wwwroot\wss\VirtualDirectories\81\web.config' に web.config の変更を適用します。指定されたノード "configuration/connectionStrings"web.config ファイルに見つかりませんでした。」
.wsp ファイルのインストールを容易にするために、web.config ファイルを手動で変更する必要はありません。
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;
}
アップデート!
2番目のweb.configの変更をEnsureセクションに変更すると、最初の問題が修正されました。ただし、コードを実行しようとすると、次のようになります。
The '[' character, hexadecimal value 0x5B, cannot be included in a name. Line 1, position 5.
2 回目の更新
構成を Web.Config に保存するという考えを捨てることになりました。代わりに、SPFarm のプロパティ バッグを使用することにしました。その実装を数分で稼働させました。助けてくれてありがとう!