I have come across two ways to modify the web.config with custom nodes when using SharePoint. Yes, I know SharePoint solutions (.wsp) allow you to update web.config for safe control entries and other areas but this model doesn’t allow for any modification you want…lets say a WCF service <system.serviceModel>.

Option 1
You can use the SPWebConfigModification class that is inside the Microsoft.SharePoint.Administration.dll. Its purpose is to write nodes and attributes into the web.config file. This is a great approach when you want to deploy your custom settings via a features/solutions deployment.

SPWebService service = SPWebService.ContentService; SPWebConfigModification myModification = new SPWebConfigModification(); myModification.Path = “configuration/SharePoint/SafeControls”; myModification.Name = “SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='True']“; myModification.Sequence = 0; myModification.Owner = “User Name“; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Value = “<SafeControl Assembly=’MyCustomAssembly’ Namespace=’MyCustomNamespace’ TypeName=’*’ Safe=’True’ />”; service.WebConfigModifications.Add(myModification); /*Call Update and ApplyWebConfigModifications to save changes*/ service.Update(); service.ApplyWebConfigModifications();

 http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx

So the above code would go in a FeatureReceiver event when activated and removed when deactivated.

Option 2
If you want to write custom nodes into a web.config when the web application is first created you could also do the following.

When a Web Application is first created WSS copies the web.config file from C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG to the root folder of the Web Application. But before this file is copied it checks the CONFIG directory for any xml file that has a name in the format webconfig.*.xml and merges the contents with the web.config.

So you would create a file called webconfig.myname.xml and save it to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG

However, this approach will not modify existing web applications.

webconfig.myname.xml contents:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<actions>
<add path=”configuration/appSettings”>
<add key=”MyFilePath” value=”C:\temp\path\” />
</add>
<add path=”configuration”>
<connectionStrings />
</add>
<add path=”configuration/connectionStrings”>
<remove name=”MySqlServerConnection” />
<add name=”MySqlServerConnection” connectionString=”server=[server];database=
db];Integrated Security=SSIP;” providerName=”System.Data.SqlClient” />
</add>
</actions>

So this needs to be completed on each web front end server. So to eliminate the manual approach again you can and package this up into a solution so WSS manages the deployment across the farm.


When creating a supplemental config file, the web.config modifications are NOT automatically mergeduntil you make a call to stsadm -o copyappbincontent.

You can also force this command to be run through a FeatureReceiver.

After exploring the stsadm tool in reflector I found that the copyappbincontent operation makes a call to SPWebApplication.WebService.ApplyApplicationContentToLocalServer()'

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
   
var webApp = (SPWebApplication)properties.Feature.Parent;
    webApp
.WebService.ApplyApplicationContentToLocalServer();
}




posted @ 2012-01-06 11:45 队长 阅读(2) 评论(0) 编辑
参考这个:
 
简言之,微软为了防止dos攻击,在2011年5~8月份不知道哪个patch中将WebPart的xsl转换时间限制为1秒之内,如果超时就不显示该WebPart而给出如下错误消息:
"Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator."
 
通常这种情况发生在字段很多(比如超过30个子段)的定制Form之上。
 
通常是第一次访问时出现这个错误,刷新页面后就正常显示了,估计是因为有缓存的缘故,如果不持续访问,过一段时间又会出现这个错误,估计是缓存失效了。
 
微软在这个KB中给出了解决方案:
1. 简化xls的代码,从而加快解析速度,于是不超时正常显示 (xsl不是很好写啊)
2. 使用infopath form替代默认的Web Form (好像需要sharepoint企业版)
3. 继承系统自带的DataFormWebPart,创建并应用自己的WebPart(看似复杂实则最可行)
1) Sub class the DataForm Web Part. Override the following methods. Then Deploy the assembly.
Example:
  public class customDFWP : DataFormWebPart
    {
        public override bool IsGhosted
        {
            get
            {
                return true;
            }
        }
        public override bool CanHaveServerControls
        {
            get
            {
                return true;
            }
        }
    }

2) Add a safe control entry to the web.config
Example: <SafeControl Assembly="customDFWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=963f869a440db619" Namespace="customDFWP" TypeName="*" Safe="True" AllowRemoteDesigner="True" SafeAgainstScript="False"/>

3) Add the following to the <tagMapping> element of the web.config
Example: <add tagType="Microsoft.SharePoint.WebPartPages.DataFormWebPart, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" mappedTagType="customDFWP.customDFWP, customDFWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=963f869a440db619" />

4) Register the assembly on the form page.
Example: <%@ Register tagprefix="customDFWP" namespace="customDFWP" assembly="customDFWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=963f869a440db619" %>

5) On the form page find <WebPartPages:DataFormWebPart > and replace it with the new custom tag.
Example:<customDFWP:customDFWP>
 
更多参考:





posted @ 2012-01-06 11:16 队长 阅读(95) 评论(0) 编辑