Sunday, April 1, 2012

Create custom actions menu in Sharepoint

As you now in Sharepoint there is OTB Site actions menu available which contains links on administrative pages (e.g. Site settings, People and Groups, etc.), links for create and update page, and others. This menu is security trimmed and by default is placed on the masterpage. It is shown via javascript to end users. It is possible to add new links to OTB Site actions by activating of custom feature with CustomAction element in elements manifest (see example e.g. here). But what if we need to create our own custom independent menu and place it to the same masterpage? It would be useful to reuse standard functionality if you need to create custom menu which should look like OTB menu.

In order to do it we will reuse SiteActions control (Sharepoint uses it for Site actions menu). We need to create inheritor and override its Text property in order to show custom title:

   1: public class CustomSiteActions : SiteActions
   2: {
   3:     public override string Text
   4:     {
   5:         get
   6:         {
   7:             return "Custom Site Actions";
   8:         }
   9:     }
  10: }

After that we can add this control to the masterpage and define custom menu items:

   1: <uc:CustomSiteActions runat="server" id="customSiteActions"
   2:     PrefixHtml="&lt;div&gt;&lt;div&gt;"
   3:     SuffixHtml="&lt;/div&gt;&lt;/div&gt;"
   4:     MenuNotVisibleHtml="&amp;nbsp;">
   5:     <CustomTemplate>
   6:         <SharePoint:FeatureMenuTemplate runat="server"
   7:             FeatureScope="Site"
   8:             Location="CustomSiteActions"
   9:             GroupId="CustomSiteActionsGroup"
  10:             UseShortId="true">
  11:             <SharePoint:MenuItemTemplate runat="server" id="Item1"
  12:                 Text="Foo"
  13:                 MenuGroupId="100"
  14:                 Sequence="100"
  15:                 ClientOnClickNavigateUrl="~site/_layouts/test/example1.aspx" />
  16:             <SharePoint:MenuItemTemplate runat="server" id="Item1"
  17:                 Text="Bar"
  18:                 MenuGroupId="100"
  19:                 Sequence="200"
  20:                 ClientOnClickNavigateUrl="~site/_layouts/test/example2.aspx" />
  21:         </SharePoint:FeatureMenuTemplate>
  22:     </CustomTemplate>
  23: </uc:CustomSiteActions>

Note that we changed Location and GroupId properties to the custom values. They can be used in order to add custom actions to the custom site actions menu via features (by the same way as they are added to the standard menu). With this technique you will be able to create custom actions menu which looks like OTB Site actions.

No comments:

Post a Comment