Serge van den Oever
Serge van den Oever

Reputation: 4392

SharePoint 2010: feature receiver code executed from UI, not from PowerShell or stdadm

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?

Upvotes: 5

Views: 7633

Answers (2)

ArjanP
ArjanP

Reputation: 2172

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)

Upvotes: 6

user152949
user152949

Reputation:

-"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

Upvotes: 5

Related Questions