Wednesday, June 29, 2011

Custom Page Addition Baseline Feature for MOSS 2007


After a long break, again came some requirements where I need to work on MOSS 2007. After working with SP 2010 and VS 2010 for more than a year, it is like again going back to the stone age of SP development. So, I thought it will be good to have a base VS 2008 Web Application Solution and a base Feature Template to add custom pages in SharePoint context ready for all the MOSS developers. So, I created one custom solution and feature which you can download and the modify accordingly. This feature is strictly for adding custom pages into SharePoint context and both the solution and feature template can be downloaded from the following link:

Steps:
1. Unzip the folder and build the solution.
2. Place the compiled dll into the GAC
3. Place the feature folder in 12 Hives feature folder
4. Install and activate the feature from STSADM
5. Open a browser window and write down the url in ServerName-PortNo/SiteName/UAR/Default.aspx in this fashion and see our custom page started running in SharePoint context.
6. To add your own custom page, open the solution and add new page there.
7. Once done designing, copy that .aspx page inside our own feature folder.
8. Again compile the solution and place the dll inside the GAC and open the new page in the similar approached defined in point no 5.

Cheers,
Avik

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

Thursday, June 16, 2011

How to change the Master Page of a Team Site

The master page option is not displayed in the Look and feel section of a Team Site, so you have to apply your common sense as specified below:

Append this "/_Layouts/ChangeSiteMasterPage.aspx" at the end of your site url. This will directly open the master page settings page and then you are free to do anything ;)

Cheers,
Avik

Saturday, June 11, 2011

How To: Hide the Quick Launch in a SharePoint Page


To hide the quick launch area do the following steps:

1. Edit the page and add a content editor web part

securedownload

2. Select the ’Click here to add new content’ link:

securedownload

3. Now, from the top ribbon select the Edit HTML option:

securedownload

4. Put the following code and save and close the Content Editor and you are done:

<style>
#s4-leftpanel{
display:none
}
.s4-ca{
margin-left:0px
}
</style>

securedownload

Once you select Ok, you will see the left side quick launch bar is not appearing now. You can also edit the properties of the added content editor web part and can select the Hidden option for beautification purpose.

Cheers,
Avik