Friday, March 27, 2009

Add Custom Action to SharePoint Site

Create a new feature which copy custom page(My Page) to the layout\CustomPages folder. Add the following in the elements.xml to create a custom action item.

<!-- Add Command to Site Actions Dropdown -->
<CustomAction Id="SiteActionsToolbar"
  GroupId="SiteActions"
  Location="Microsoft.SharePoint.StandardMenu"
  Sequence="2001"
  Title="My Page"
  Description="Browse to my custom page"
  ImageUrl="/_layouts/images/DECISION.GIF">
   <UrlAction Url="~site/_layouts/CustomPages/MyPage.aspx"/>
</CustomAction>

This would add a new menu called “My Page” to the Site Actions menu.

When creating a CustomAction element, you can configure the URL attribute of the inner UrlAction element by using the ~site token, as you have just seen. You could also use ~sitecollection token in cases in which custom page should always be executed within a URL associated with the current site collection and its top-level site.

To restrict this page to only viewable to administrator, add RequireSiteAdministrator attribute to CustomAction and set it to “True”. For more security, add the following code in the “My page” custom page -

protected override bool RequireSiteAdministrator {
    get { return true; }
  }

0 comments: