Register an assembly as a safe control in the web. config file

Source: Internet
Author: User
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 your 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:

   1: <configuration>
   2:   <SharePoint>
   3:     <SafeControls>
   4:       <SafeControl Assembly="[Assembly Name]" Namespace="[Namespace]" TypeName="*" Safe="True" />
   5:     </SafeControls>
   6:   </SharePoint>
   7: </configuration>

AssemblyThe 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. shocould 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

   1: <SafeControl Assembly="Brett.DemoParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f03e5f7a44d50a3a" 
   2:              Namespace="Brett.SharePoint.WebParts" 
   3:              TypeName="*" 
   4:              Safe="True" 
   5:              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 favorite 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 shoshould 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 shocould 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, shocould a new server be added to the farm, these modifications will also be applied.

The following code cocould be added to the featureactivated override method in your feature that deploys the Web part.

   1: public override void FeatureActivated(SPFeatureReceiverProperties properties) 
   2: {
   3:     // A reference to the features Site Collection
   4:     SPSite site = null;
   5:  
   6:     // Get a reference to the Site Collection of the feature
   7:     if (properties.Feature is SPWeb)
   8:     { site = ((SPWeb)properties.Feature.Parent).Site; }
   9:     else if (properties.Feature.Parent is SPSite)
  10:     { site = properties.Feature.Parent as SPSite; }
  11:  
  12:     if (site != null)
  13:     {
  14:         SPWebApplication webApp = site.WebApplication;
  15:  
  16:         // Create a modification
  17:         SPWebConfigModification mod = new SPWebConfigModification(
  18:             "SafeControl[@Assembly=\"MyAssembly\"][@Namespace=\"My.Namespace\"]"
  19:                 + "[@TypeName=\"*\"][@Safe=\"True\"][@AllowRemoteDesigner=\"True\"]"
  20:             , "/configuration/SharePoint/SafeControls"
  21:             );
  22:  
  23:         // Add the modification to the collection of modifications
  24:         webApp.WebConfigModifications.Add(mod);
  25:  
  26:         // Apply the modification
  27:         webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
  28:     }
  29: }
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 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

   1: <Solution SolutionId="{1E0FDA58-6611-423a-92EC-8E7355810CEE}"
   2:           xmlns="http://schemas.microsoft.com/sharepoint/">
   3:   <FeatureManifests  />
   4:   <ApplicationResourceFiles />
   5:   <CodeAccessSecurity />
   6:   <DwpFiles />
   7:   <Resources />
   8:   <RootFiles />
   9:   <SiteDefinitionManifests />
  10:   <TemplateFiles />
  11:   
  12:    <Assemblies>
  13:       <Assembly DeploymentTarget="WebApplication" Location="Brett.DemoParts.dll">
  14:          <SafeControls>
  15:             <SafeControl Assembly="Brett.DemoParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f03e5f7a44d50a3a"
  16:                          Namespace="LitwareWebParts" 
  17:                          TypeName="*" 
  18:                          Safe="True"                         
  19:                          />
  20:          </SafeControls>
  21:       </Assembly>
  22:    </Assemblies>
  23: </Solution>
  24:  

Key highlights

DeploymenttargetThe depoloyment target is location where the Assembly will be copied to and can ether be the bin folder of the webapplication or it cocould 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.