Feature Activated Event not triggered when creating a new sub site with web template
-
10-12-2019 - |
Question
When I create a new site I have a Web Scoped FeatureActivated event that I would like to run. The feature does get Activated but the FeatureActivated event is not triggered.
The site is created with webtemplate that includes the features guid in the webfeatures node. From my webtemplate's onet.xml that specifies the feature
<WebFeatures>
<Feature ID="e6e83174-0e6a-4fb6-a3e9-f16739a08a93" />
</WebFeatures>
Here is my featureactivated code:
private const string QuicklaunchHeader = "Saksmappe";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb webTemp = properties.Feature.Parent as SPWeb;
var web = webTemp.Site.RootWeb;
var rootFolder = web.RootFolder;
rootFolder.WelcomePage = "Pages/CaseFolder.aspx";
rootFolder.Update();
//Remove old custom quick launch entry
RemoveQuickLaunchItems(QuicklaunchHeader, web);
//Add custom quick launch entry
var header = new SPNavigationNode(QuicklaunchHeader,
web.Navigation.Home.Url, false);
web.Navigation.QuickLaunch.AddAsFirst(header);
//TODO Set correct urls
header.Children.AddAsLast(new SPNavigationNode("Saksdetaljer", ""));
header.Children.AddAsLast(new SPNavigationNode("Merknader", ""));
}
When I create the site I am not able to debug the code. I have tried Debugger.Break() and putting a break point in VS2010.
When I deploy the package from VS2010 the featureactivated code gets triggered. So the event does work, but not when I create the new site. Anyone knows how to solve this problem?
Solution
You are using the rootweb
of the site, not the current web, so any changing you are making is always to the rootweb
. Instead of using webTemp to get rootweb
, just use webTemp
as the web.
private const string QuicklaunchHeader = "Saksmappe";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
var rootFolder = web.RootFolder;
rootFolder.WelcomePage = "Pages/CaseFolder.aspx";
rootFolder.Update();
//Remove old custom quick launch entry
RemoveQuickLaunchItems(QuicklaunchHeader, web);
//Add custom quick launch entry
var header = new SPNavigationNode(QuicklaunchHeader,
web.Navigation.Home.Url, false);
web.Navigation.QuickLaunch.AddAsFirst(header);
//TODO Set correct urls
header.Children.AddAsLast(new SPNavigationNode("Saksdetaljer", ""));
header.Children.AddAsLast(new SPNavigationNode("Merknader", ""));
}