views:

26

answers:

3

I have a WSP containing a web scope feature with the following code:

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace Macaw.DualLayout.Samples.Features.DualLayoutSampleEmpty_Web
{
    [Guid("8b558382-5566-43a4-85fa-ca86845b04b0")]
    public class DualLayoutSampleEmpty_WebEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = (SPWeb)properties.Feature.Parent)
            {
                using (SPSite site = (SPSite)web.Site)
                {
                    Uri uri = new Uri(site.Url + "/_catalogs/masterpage/DLSampleEmpty.master");
                    web.CustomMasterUrl = uri.AbsolutePath; // Master for all publishing pages
                    web.Update();
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = (SPWeb)properties.Feature.Parent)
            {
                using (SPSite site = (SPSite)web.Site)
                {
                    Uri uri = new Uri(site.Url + "/_catalogs/masterpage/v4.master");
                    web.CustomMasterUrl = uri.AbsolutePath; // Master for all publishing pages
                    web.Update();
                }
            }
        }
    }
}

I do F5 deployment from Visual Studio 2010. When I activate the feature from UI I get into my breakpoint in the feature code, the feature code is executed. When I activate the feature from PowerShell:

Enable-SPFeature -Url http://myserver/sites/publishing/empty -Identity MyFeatureName -force -verbose

or with STSADM:

stsadm -o activatefeature -name MyFeatureName -url http://myserver/sites/Publishing/Empty -force

I see that the feature is activated (in the UI), but I don't hit my breakpoint and the feature receiver code is NOT executed.

Any ideas?

+2  A: 

-"By default, when you run a Visual Studio SharePoint application, its features are automatically activated for you on the SharePoint server. However, this causes problems when you debug feature event receivers, because when a feature is activated by Visual Studio, it runs in a different process than the debugger. This means that some debugging functionality, such as breakpoints, will not work correctly.

To disable the automatic activation of the feature in SharePoint and allow proper debugging of Feature Event Receivers, set the value of the project's Active Deployment Configuration property to No Activation before debugging. Then, after your Visual Studio SharePoint application is running, manually activate the feature in SharePoint. To do this, click Site Settings on the Site Actions menu in SharePoint, click the Manage Site Features link, and then click the Activate button next to the feature and resume debugging as normal."

Source: http://msdn.microsoft.com/en-us/library/ee231550.aspx

Inge Henriksen
This is the you're looking for.
Peter Walke
+2  A: 

If you use powershell or stsadm the feature will not run in the context of the IIS worker process. What do you attach VS studio to when you debug?

When debugging stsadm tasks I usually add:

System.Diagnostics.Debugger.Launch();

to the code and you will be prompted to attach a debugger when the command is run. Crude but easy. (Don't forget to remove)

ArjanP
Hi Arjan, good approach, forgot about that:-) This means you don't have to thing about the context where your code is executed (timer job, stsadm, powershell...).
Serge van den Oever
A: 

I have developed a simple feature receiver and I can debug it through Visual Studio (System Diagnostic) and UI or stsadm. But If I enable the feaure with PowerShell it does not work. Did someone face this? Powershell Enable-SPFeature does not execute the feature receiver associated to the feature