Sunday, June 20, 2010

Adding a custom menu item into "Site Actions" Menu

There may be some scenario when we need to add custom menu item to Site Actions drop down, for doing this we need to add a feature which add the menu. The procedures for this is mentioned below.

1. Create a folder named 'MyCustomSiteActionMenu'
2. Create feature.xml in that folder and add the following code to the feature.xml and save
<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="{NEWGUID}" Hidden="TRUE"
Title="Custom Menu Site Action demo feature"
Description="The feature will add a custom menu item into the Site Actions drop down"
Version="12.0.0.0"
Scope="Web"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>

Replace {NEWGUID} with a newly created GUID - Use Visual Studio 'Create GUID' tool for this. (Go to Tools -> Create Guid in IDE)
'Id' Any new GUID
'Hidden' denotes whether the feature should be listed under Site Features.
'Title' denotes the feature title, this will be displayed as the title in feature listing page
'Description' denotes the feature description, which will be displayed in the feature listing page
'Version' denotes the version
'Scope' denotes the feature activation scope. Possible values are Site, Web and Farm
And finally mention the element Manifests of the feature, in this case 'elements.xml'
3. Create the elements.xml in the same folder and add the below code and save
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="customAction.Menu.JsAlert"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="999"
Title="Show Javascript Alert"
Description="Click to see Javascript alert" RequireSiteAdministrator="True" >
<UrlAction Url="javascript:alert('Hi.. From Site Actions')"/>
</CustomAction>
<CustomAction
Id="customAction.Menu.RedirectToPage"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="1000"
Title="Redirect to Bing.com"
Description="Click to redirect to a url www.bing.com" RequireSiteAdministrator="True" >
<UrlAction Url="http://www.bing.com"/>
</CustomAction>
</Elements>
Refer the MSDN url for more information on the Attributes http://msdn.microsoft.com/en-us/library/ms460194.aspx
'GroupId' Value is important if you want to add as Sub menu of another Menu Item
'Location' is important as well, because it denotes where the custome menu should be added.
Refer the MSDN url for more information on the Location values http://msdn.microsoft.com/en-us/library/bb802730.aspx
In this post, we are going to add a custom menu to the SiteActions menu so location is 'Microsoft.SharePoint.StandardMenu' 'Sequence' as the name indicates the ordering of the menu item.
'Title' is what we see in the menu item
'Description' is also seen just below the menu item title
'RequireSiteAdministrator' value indicating whether the menu item will be visible only to Site Administrators
<UrlAction> denotes the action to be performed.
Now we are good enough to deploy the feature.
4. Copy the folder 'MyCustomSiteActionMenu' to 12\TEMPLATE\FEATURES
5. Using Stsadm command install the feature.
Using Command Prompt, go to the folder 12\bin and then execute the following command
STSADM -o installfeature -name 'MyCustomSiteActionMenu'
Wait for message "Operation Completed Successfully."
6. Using Stsadm command activate the feature (The feature is Hidden so it wont be seen in the features listing).
STSADM -o activatefeature -name "MyCustomSiteActionMenu" -url "http://localhost"
Wait for message "Operation Completed Successfully."
7. Done - Go back to the page, refresh to find the newly added custom menu.
8. Cheers !!!

No comments: