我是靠谱客的博主 炙热荔枝,最近开发中收集的这篇文章主要介绍SharePoint - Register an assembly as a safe control in the Web.config file,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

SharePoint - Register an assembly as a safe control in the Web.config file

In order for you to use your own custom assembly with your web parts and other little bits, you will need to add your safe control to the web.config file.  However, you need to think "WEB FARM" with many servers hosting the web application so I will show you a couple ways to do this.

The entry in the web.config

You need to place a SaveControl element entry into the web.config file of the web application.  The entry looks like the following:


<configuration>
<SharePoint>
<SafeControls>
<SafeControl Assembly="[Assembly Name]" Namespace="[Namespace]" TypeName="*" Safe="True" />
</SafeControls>
</SharePoint>
</configuration>

Assembly The name of your assembly needs to added to this section. Although you can simply type the name of the DLL hosting the control into the Assembly element, it is important to not that this is not the recommended practice.  Rather, use a full four part name; i.e. [assembly], version=[version], culture=[culture], publickeytoken=[publickeytoken] NamespaceThe namespace that your web controls are in.  If you have your controls in multiple namespaces, you will need to add one <SafeContol ...> element for each control. TypeNameThe name of the web control which is allowed to be executed with the SharePoint web application.  Should your namespace have multiple web controls, you do not need to register each control.  You can simply use * (asterisk) to indicate the dll. SafeA boolean flag, indicating whether the control is treated as safe (true) or unsafe (false). AllowRemoteDesignerA boolean flag, indicating whether the control can be loaded by a remote designer, such as SharePoint Designer.

Sample


<SafeControl Assembly="Brett.DemoParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f03e5f7a44d50a3a"
Namespace="Brett.SharePoint.WebParts"
TypeName="*"
Safe="True"
AllowRemoteDesigner="True" />

Methods of updating the web.config file

There are three ways you can update the web.config file,

  • Manually adding the SafeControl to the web.config
  • Adding the SafeControl to the web.config with code
  • Deploy the assembly using a solution package

Manually editing the web.config (bad)

This approach may sound the easiest and quickest way as you simply open up your favourite xml editor, find the <SafeControls> element and add your own control into it.

WARNING!
If you do it this way, you are looking for trouble in a farm as you will need to remember to change the web.config modification for all your servers in the farm as well as all the web applications on the farm that use the custom control.  So should you have a really awsome web part that is used within 5 web applications hosted on your farm of 3 servers, you will need to make the modification to 15 web.config's .. have fun.

Also should you add a new server to your farm, please remember to add the entry the web.config.

Bottom line, this is the worst possible way you can do it  and stay away from doing it this way

Adding the SafeControl to the web.config with code (good)

SharePoint provides a class called SPWebConfigModification which has a set of modification commands in a collection.  These modification commands are applied to the default web.config of the Web Application.  These configuration modification commands will also be added and applied to all servers in a farm.   Finally, should a new server be added to the farm, these modifications will also be applied.

The following code could be added to the FeatureActivated override method in your feature that deploys the web part.


public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// A reference to the features Site Collection
SPSite site = null;
// Get a reference to the Site Collection of the feature
if (properties.Feature is SPWeb)
{ site = ((SPWeb)properties.Feature.Parent).Site; }
else if (properties.Feature.Parent is SPSite)
{ site = properties.Feature.Parent as SPSite; }
if (site != null)
{
SPWebApplication webApp = site.WebApplication;
// Create a modification
SPWebConfigModification mod = new SPWebConfigModification(
"SafeControl[@Assembly="MyAssembly"][@Namespace="My.Namespace"]"
+ "[@TypeName="*"][@Safe="True"][@AllowRemoteDesigner="True"]"
, "/configuration/SharePoint/SafeControls"
);
// Add the modification to the collection of modifications
webApp.WebConfigModifications.Add(mod);
// Apply the modification
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
}

Deploy the assembly using a solution package (best)

The preferred way to provision your features, web parts and assemblies is by creating a Solution Package (.wsp file).  You will add add your assembly, the manifest.xml file and all your other components and resources into the cabinet.

You will need to add the following entry into the manifest.xml


<Solution SolutionId="{1E0FDA58-6611-423a-92EC-8E7355810CEE}"
xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureManifests
/>
<ApplicationResourceFiles />
<CodeAccessSecurity />
<DwpFiles />
<Resources />
<RootFiles />
<SiteDefinitionManifests />
<TemplateFiles />
<Assemblies>
<Assembly DeploymentTarget="WebApplication" Location="Brett.DemoParts.dll">
<SafeControls>
<SafeControl Assembly="Brett.DemoParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f03e5f7a44d50a3a"
Namespace="LitwareWebParts"
TypeName="*"
Safe="True"
/>
</SafeControls>
</Assembly>
</Assemblies>
</Solution>

Key highlights

DeploymentTarget The depoloyment target is location where the assembly will be copied to and can ether be the bin folder of the WebApplication or it could be the GlobalAssemblyCache (GAC) LocationThe location of the assembly within the cabinet file. SafeControlA SafeControl element entry as described at the beginning of the post.   

Using this method, your assembly will be correctly deployed the servers in the farm as well as added to the safe controls of the web application.  Again any new server added to the farm will automatically get all the solution packages deployed.

See

  • Preparing for exam 70-541- TS- Microsoft Windows SharePoint Services 3.0 – Application Development
  • A series of posts on the art of provisioning in SharePoint
  • Creating a WSS 3.0 Web Part using Visual Studio 2005
  • Determine the Public Key Token for assembly

References

  • SafeControl Adder utility for SharePoint
  • Creating SharePoint’s SafeControl Entries in PowerShell
  • Registering a Web Part Assembly as a Safe Control

http://www.cnblogs.com/icedog/archive/2010/07/08/1773248.html

最后

以上就是炙热荔枝为你收集整理的SharePoint - Register an assembly as a safe control in the Web.config file的全部内容,希望文章能够帮你解决SharePoint - Register an assembly as a safe control in the Web.config file所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部