Tuesday, June 21, 2011

How to: Create and Deploy Feature Receivers in SP 2010 and Customizing Top Link Bar


To create a Feature receiver

  1. In Microsoft Visual Studio, start an Empty SharePoint Project. When you are prompted, choose to make it a sandboxed solution.

  2. In Solution Explorer, right-click the Features folder, and then select Add Feature.

  3. Right-click the Feature, and select Add Event Receiver.

  4. Open the .cs or .vb file that was created in the previous step.

    image

image

5. Uncomment and override the event handlers in the file as needed using your event handling logic. As a general rule, if you override the FeatureActivated handler, you should also override the FeatureDeactivating handler to reverse what your code did in the FeatureActivated handler. Similarly, if you override the FeatureInstalled handler, you should also override the FeatureUninstalling handler to reverse what your code did in the FeatureInstalled handler.

6. Now to customize the top link bar, first you need to add the following reference:

Code Snippet
  1. using Microsoft.SharePoint.Navigation;

Then place the following code inside appropriate event handlers:

   1: public override void FeatureActivated(SPFeatureReceiverProperties properties)
   2: {
   3:     SPWeb topNavigationWeb = (SPWeb)properties.Feature.Parent;
   4:     SPSite topNavigationSite = topNavigationWeb.Site;
   5:  
   6:     SPNavigationNodeCollection topNavigationNodes = topNavigationWeb.Navigation.TopNavigationBar;
   7:     
   8:     SPNavigationNode MenuItem1 = new SPNavigationNode("Tab 1", "http://theSPgeek.com/Pages/default.aspx", true);
   9:     topNavigationNodes.AddAsFirst(MenuItem1);
  10:     SPNavigationNode MenuItem2 = new SPNavigationNode("Tab 2", "http://theSPgeek.com/my/default.aspx", true);
  11:     topNavigationNodes.Add(MenuItem2, MenuItem1);
  12:     SPNavigationNode MenuItem3 = new SPNavigationNode("Tab 3", "https://theSPgeek.com/", true);
  13:     topNavigationNodes.AddAsLast(MenuItem3);
  14:  
  15:     topNavigationWeb.Update(); 
  16: }

As told earlier, if you override the FeatureActivated handler, you should also override the FeatureDeactivating handler to reverse what your code did in the FeatureActivated handler. So, place the following code inside FeatureDeactivating event handler:



   1: public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
   2: {
   3:     SPWeb topNavigationWeb = (SPWeb)properties.Feature.Parent;
   4:     SPSite topNavigationSite = topNavigationWeb.Site;
   5:  
   6:     SPNavigationNodeCollection topNavigationNodes = topNavigationWeb.Navigation.TopNavigationBar;
   7:     foreach (SPNavigationNode node in topNavigationNodes)
   8:     {
   9:         if (node.Title.ToString() == "Tab 1" || node.Title.ToString() == "Tab 2"
  10:             || node.Title.ToString() == "Tab 3")
  11:         {
  12:             topNavigationNodes.Delete(node);
  13:         }
  14:     }
  15:     topNavigationWeb.Update();
  16: }

Now build the solution and then deploy it. Go to the particular SharePoint site where you want to apply this custom global navigation i.e., to update the top link bar. Now go to the Site Action > Site Settings page and from there select the ‘Manage Site Features’ option. On the Site Features page you will find our Custom Feature. Just activate it and you will see custom links has been added to the to link bar or global navigation bar. Now again come to the Site Action > Site Settings page and from there select the ‘Manage Site Features’ option again. Now deactivate our custom feature and you will see, the custom links that we have added by activating the feature just a few minutes ago, has disappeared.


Feature Receiver is a very powerful product and if you use it properly it will help not only you but the business users also to achieve some custom functionality by just activating – deactivating some feature!


Cheers,
Avik


Ref:


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


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

No comments:

Post a Comment

Post a Comment