views:

390

answers:

0

Hi,

I have created a (staplee) feature that is stapled to a site template with a second feature as described in the blog post here. The idea is that when a site is created, my staplee feature will be activated and the code I have added to the FeatureActivated method will be fired and will create a new document library. The problem is that when a new site is created, the FeatureActivated method doesn't get called!

Looking in the log files (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS) I can see messages such as "Installing Feature ''" and "Calling 'FeatureActivated' method of SPFeatureReceiver for Feature '' (ID: 'b9cefee0-0746-11df-8a39-0800200c9a66')". The Guid matches the Guid that I have got configured in feature.xml so I believe this suggests that I have got everything configured correctly. However, my new document library doesn't get created, I don't see any debug messages that I add and if I try attaching to the correct w3wp process then it appears that my dll (which was installed in the gac) hasn't been loaded - my breakpoints aren't enabled and if I do [ctrl]+[alt]+u to view the currently loaded modules, my dll doesn't appear in the list.

Assuming I've configured my staplee feature to be visible (i.e. Hidden='False') if I browse to ->Site Settings->Site Features then I can see my feature in the list of site features and it has a status of Active! If I then proceed to deactivate this feature and then activate it again, my code runs - my document library is created, my debug is output and if I go into Visual Studio my breakpoints are enabled and my dll appears in the list of loaded modules. It seems that initially Sharepoint didn't even attempt to load my dll let alone try and run my code.

If I run my test a second time (i.e. with my assembly already loaded) my code still doesn't get run when the site is first created, but if I go in and deactivate my feature and reactivate it again, it appears to run fine and without error. I have considered the possibility of problems with the configuration of my features and errors in my code but the fact that everything seems to run fine if I run it manually and that Sharepoint doesn't appear to try and run my code when a new site is created, suggests to me that my configuration and code is fine - although it obviously isn't as it would work if was right, wouldn't it? :o)

I understand that if all I wanted to do was to create a document library then a feature would be overkill as I could do it with a new template - however I need to enable email on the document library giving it a specific email address which can't be done through a template (as far as I know).

I have included the xml for my two features and the code for my feature receiver below.

SiteCreateStaplee Feature.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/&quot;
         Id="b9cefee0-0746-11df-8a39-0800200c9a66"
         Title="Create Email Enabled Correspondence Document Library"
         Description="This feature creates an email enabled document library called correspondence."
         Scope="Web"
         Hidden="False"
         Version="1.0.0.0"
         ReceiverAssembly="SiteCreate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=67a497d9571ede4a"
         ReceiverClass="SiteCreate.CorrespondenceDocLibCreate">
</Feature>

SiteCreateStapler Feature.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/&quot;
         Id="747c57f0-0748-11df-8a39-0800200c9a66"
         Title="Create Email Enabled Correspondence Document Library Stapler"
         Description="This feature staples the SiteCreateStaplee feature to the project site definition."
         Scope="Farm"
         Hidden="False"
         Version="1.0.0.0">

  <ElementManifests>
    <ElementManifest Location="elements.xml" />
  </ElementManifests>

</Feature>

elements.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/&quot;&gt;
  <FeatureSiteTemplateAssociation Id="b9cefee0-0746-11df-8a39-0800200c9a66"
                                  TemplateName="PWS#0"/>
</Elements>

My feature receiver code which is in a dll that is deployed to the GAC is as follows:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;
using System.IO;

namespace SiteCreate
{
    public class CorrespondenceDocLibCreate : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (StreamWriter writer = new StreamWriter("C:\\Temp\\Debug.txt", true))
            {
                if (properties != null && properties.Feature != null && properties.Feature.Parent != null)
                {
                    using (SPWeb web = (SPWeb)properties.Feature.Parent)
                    {
                        Guid listID = web.Lists.Add("Correspondence", "Correspondence",
                                                    SPListTemplateType.DocumentLibrary);

                        SPList correspList = web.Lists[listID];

                        if (correspList != null)
                        {
                            correspList.OnQuickLaunch = true;
                            correspList.EmailAlias = web.ID.ToString();

                            correspList.Update();
                        }
                    }
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}

The sharepoint installation that I am trying to add this feature to is actually a project server installation, but I don't think this should make any difference.

If there is any further useful information that I can provide then please let me know.

Please note, this is a copy of the question posted by me at http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/128b5ea9-438e-4982-a352-cbfdeb690b0c - apologies to anyone reading this for a second time.

Thanks