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"
  Title="My Page"
  Description="Browse to my custom page"
   <UrlAction Url="~site/_layouts/CustomPages/MyPage.aspx"/>

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; }