1

I have created a publishing web template. Now I need to activate a feature once the site is provisioned.

In order to achieve this, I've created an event receiver with something like this:

public override void WebProvisioned(SPWebEventProperties properties)
    {
        base.WebProvisioned(properties);

        using (SPWeb web = properties.Web)
        {
            SPFeatureCollection featureCollect = web.Features;
            if (******)
            {
                featureCollect.Add(new Guid("FEATUREGUID"), true);
            }
        }
   }

I want this feature only to be activated when a site is provisioned with my custom web template. What should I write in (**)?

I also accept other approaches.

Thank you.

UPDATE: Onet.xml parts:

<Lists>
    <List FeatureId="00BFEA71-6A49-43FA-B535-D15C05500108" Type="108" Title="$Resources:core,discussions_Title;" Url="$Resources:core,lists_Folder;/$Resources:core,discussions_Folder;" QuickLaunchUrl="$Resources:core,lists_Folder;/$Resources:core,discussions_Folder;/AllItems.aspx" EmailAlias="$Resources:core,discussions_EmailAlias;" />
    <List FeatureId="00BFEA71-D1CE-42de-9C63-A44004CE0104" Type="104" Title="Anuncios" Url="$Resources:core,lists_Folder;/$Resources:core,announce_Folder;">
      <Data>
        <Rows>
          <Row>
            <Field Name="Title">$Resources:onetid11;</Field>
            <Field Name="Body">$Resources:onetid12;</Field>
            <Field Name="Expires">&lt;ows:TodayISO/&gt;</Field>
          </Row>
        </Rows>
      </Data>
    </List>
    <List FeatureId="00BFEA71-2062-426C-90BF-714C59600103" Type="103" Title="$Resources:core,linksList;" Url="$Resources:core,lists_Folder;/$Resources:core,links_Folder;" />
    <List FeatureId="00BFEA71-EC85-4903-972D-EBE475780106" Type="106" Title="$Resources:core,calendarList;" Url="$Resources:core,lists_Folder;/$Resources:core,calendar_Folder;" QuickLaunchUrl="$Resources:core,lists_Folder;/$Resources:core,calendar_Folder;/Calendar.aspx" EmailAlias="$Resources:core,calendar_EmailAlias;" />
    <List FeatureId="00BFEA71-A83E-497E-9BA0-7A5C597D0107" Type="107" Title="$Resources:core,taskList;" Url="$Resources:core,lists_Folder;/$Resources:core,tasks_Folder;" QuickLaunchUrl="$Resources:core,lists_Folder;/$Resources:core,tasks_Folder;/AllItems.aspx" />
  </Lists>

(...)

<WebFeatures>
    (...)
    <!-- Links List-->
    <Feature ID="00bfea71-2062-426c-90bf-714c59600103" />
    <!-- Documents Libraries-->
    <Feature ID="6e53dd27-98f2-4ae5-85a0-e9a8ef4aa6df" />
    (...)
    <!-- Page loading feature -->
    <Feature ID="3ce5c12e-b3fe-4996-9608-18b8dedc137b" />
    <!-- The next Feature adds ListViewWebParts to the pages, but doesn't work here. It says that List Links doesn't exist -->
    <!-- Activating it manually or via Event Receiver works -->
    <!--<Feature ID="7fce2ec9-856c-4fc0-93b8-b82c78e4f227" />-->
  </WebFeatures>

3 Answers 3

4

Why dont you just add the feature to SiteFeatures or WebFeatures element of onet.xml ? It is what it is there for :)

The features are executed synchronous, so only in very rare occasions have I experienced race conditions.

4
  • Among other things, the feature adds a listview from a Links list in default.aspx. When I add the feature in onet.xml, an error raises saying that the list doesn't exist. When I activate the feature manually it works perfectly, there was when I thought to activate it throught event receiver...
    – Bargant
    Commented Oct 10, 2012 at 14:59
  • 1
    Maybe post (parts of) your onet.xml? Be aware that you shold NEVER use modules in ONET.XML for web templates! It does work, but it is UNSUPPORTED! They are just as prone for race conditions as before (custom site definitions) and there is no need to use them, as you can just wrap them in a module (as Per suggest) and make sure they are placed above the features where you use them (since features are executed synchronously -per scope). You can get more info on WebTemplates in Vesa Juvonens article here blogs.msdn.com/b/vesku/archive/2010/10/14/… Commented Oct 11, 2012 at 7:29
  • Onet.xml parts added. For the Web Template creation I followed this article, thany you :) My ONET.XML is not inside a Module, I've created it with empty element. The problems started when I wanted to add the ViewListWebParts...
    – Bargant
    Commented Oct 11, 2012 at 8:14
  • 1
    what I ment was NO modules in ONET.XML (and also NO lists). It is not supported and will give you just the kind of race conditions you experience. Put files into a module in a feature, put list instances into another feature and add them in correct order as WebFeatures in ONET.XML . You should ONLY have one Configurations/Configuration element in your ONET.XML containing only SiteFeatures and WebFeatures and nothing more :) Commented Oct 11, 2012 at 13:19
0

The most native method to activate some features on webs created by a custom site definition is to use feature stapling. In this way you should not write server code. http://blogs.msdn.com/b/kunal_mukherjee/archive/2011/01/11/feature-stapling-in-sharepoint-2010.aspx

1
  • There are very very few cases where I would recommend using custom site definitions when doing SP2010 site templates. WebTemplates is the way to go 99% of the cases. They dont suffer from the same problems as custom site definitions especially in B2B and V2V upgrade scenarios. Commented Oct 11, 2012 at 7:25
0

If you want to do it your way then you should check web.WebTemplate and web.Configuration.

But I agree with @AndersRask that you should use WebFeatures of onet.xml. Then you should move the creation of Default.aspx and links list out of onet.xml into features which you list before this feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.