Freigeben über


Activating Content Type Feature at Web Application Scope

In SharePoint 2007, one of the ways of provisioning content type is by packaging content type definition as SharePoint Feature. This method allows a single content type definition to be leveraged across multipe site collections. One drawback of  this method is it's inability to restrict the Feature to be activated within a pariticular web application.

Content Type Features can be scoped only at a Site level. When this Feature is installed, it appears on every site collection in every web application. There are scenarios when this Feature needs to be activated only at a particular web application. For e.g. if you have two web applications; one for Sales and other for Marketing, you wouldn't want Sales specific content types to be activated in Marketing web application and vice versa.

One approach to work around this challenge is to write custom code in Feature activated event that makes the decision on whether the Feature can be activated. The other approach is to create a dependent Feature, scoped at Web Application, which would prevent Content Type Feature activation unless the dependent Feature is activated. Following section demonstrates the dependent Feature method:

Step 1 - Create Web Application Feature:

In this step, we will create a blank Feature, scoped at Web Application. Sole purpose of this Feature is to provide Activation Dependency for the Content Type Feature. Unless this Feature is activate for a Web Application, the content type Feature cannot be activated at site collection

________________________________ 

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="E4160D49-39CC-4a17-B700-03E4129F1350"
          Title="Allow Customer Content Type Feature"
          Description="Activating this Feature will allow site collection administrators to activate Customer Content Type Feature"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="WebApplication"
          DefaultResourceFile="core"
          xmlns="https://schemas.microsoft.com/sharepoint/">
 <ElementManifests>
 </ElementManifests>
</Feature>

______________________

Note that ElementManifest tag is empty because this Feature does not contain any elements.

 

Step 2 - Create Content Type Definition and Feature:

In this step, we will create content type definition and Feature that will provision the content type. The imporant part here is the Activation Dependency to the Web application Feature.

Content Type Definition

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="25994054-FF0E-49b1-8785-143D1A433924" xmlns="https://schemas.microsoft.com/sharepoint/">
 <ContentType ID="0x01010043e1c49b93d74e4b968fb95a6a330da40204"
  Name="Customer"
  Group="Document Content Types"
  Description="Content Type for Customer related documents"
  Version="0">
  <FieldRefs>
   <FieldRef ID="{7c8b4e26-cb9d-405c-84bd-18e76c40433e}" Name="CustomerName" />
  </FieldRefs>
 </ContentType>

<Field ID="{7c8b4e26-cb9d-405c-84bd-18e76c40433e}"
         Type="Text"
         Name="CustomerName"
         DisplayName="Customer Name"
         StaticName="CustomerName"
         Hidden="FALSE"
         Required="FALSE"
         Sealed="FALSE" />
</Elements>

Feature Definition

<?xml version="1.0" encoding="utf-8"?>

<Feature  Id="{969A3FA2-1357-48f8-9FC8-D40422A462D8}"

          Title="Customer Content Type"

          Description="Contains Content Types for Customer content"

          Version="12.0.0.0"

          Hidden="FALSE"

          Scope="Site"

          DefaultResourceFile="core"

          xmlns="https://schemas.microsoft.com/sharepoint/">

            <ElementManifests>

                        <ElementManifest Location="Customer.xml"/>

            </ElementManifests>

            <ActivationDependencies>

                        <ActivationDependency FeatureId=" E4160D49-39CC-4a17-B700-03E4129F1350"/>

</ActivationDependencies>

</Feature>

Note the ActivationDependency element in the content type Feature that points to Feature ID of the web applications scoped Feature that we created in step 1. This will ensure that content type Feature cannot be activated at a site collection, unless the Web application Feature is activated for the web application.

Comments

  • Anonymous
    May 30, 2009
    PingBack from http://microsoft-sharepoint.simplynetdev.com/activating-content-type-feature-at-web-application-scope/
  • Anonymous
    December 22, 2009
    Your statement: "Content Type Features can be scoped only at a Site level. When this Feature is installed, it appears on every site collection in every web application. There are scenarios when this Feature needs to be activated only at a particular web application. For e.g. if you have two web applications; one for Sales and other for Marketing, you wouldn't want Sales specific content types to be activated in Marketing web application and vice versa."This is incorrect. As long as your solution containing your site scoped feature makes a safe control entry in the web.config you can deploy your solution to a specific web application. The safe control entry forces you to deploy your solution to a specific web app because a web.config modification is made. This would eliminate the need for a WebApplication scoped feature for content types. I have used this successfully many times for clients. Thanks.
  • Anonymous
    February 05, 2010
    Matt, You are right about safe control entries. However, in that approach you are creating an assembly. The approach that I suggested is "no code" FEATURE. It is pure XML based content type feature, which does not provide deployment at Web App level.As I mentioned, if you are writing code, then you have multiple option, one being safe control, other being Feature activated validation and also creating content type using Object model.Thanks for your comment.Cheers,Arbindo