Custom Site action using MenuItemTemplate


Hi,

Site action links are by default added by sharepoint but there could be some cases when you feel like adding new site action to navigate to your custom page or OOB page. In this blog, I will explain how you can and what are the various way to do that.

Solution 1 (Using Custom Action):

You must heard this term if ever got chance to customize the ribbon or any contextual menu. You can very easily add this element to your custom farm solution just by adding a empty module and then add following code in to it.
<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;

<CustomAction Id=”CustomScript”
ScriptBlock=”function OpenDialog() {
window.scrollTo(0, 0);
var context = SP.ClientContext.get_current();
var siteUrl = context.get_url();
if (siteUrl == ‘/’) siteUrl = ”;
var options = SP.UI.$create_DialogOptions();
options.url = siteUrl + ‘/_layouts/<url of your custom page>’;
options.autosize= true;
SP.UI.ModalDialog.showModalDialog(options);
}”
Location=”ScriptLink”>
</CustomAction>

<CustomAction Id=”MyCustomAction”GroupId=”SiteActions”
Location=”Microsoft.SharePoint.StandardMenu”
Sequence=”100″
Title=”Add News”
Description=”Link to Add new news for Sg finance” >
<UrlAction Url=”javascript:OpenDialog();”/>
</CustomAction>
</Elements>

Solution 2 (Using MenuItemTemplate):

If you ever notice the master page of your site(Oslo.master or Seattle.master), there is a “SiteActions” tag present which contains the definition of default site action links. Now depending upon the customer/project customization potential we can add our custom link to the master page. What I mean is if you are free to customize or create custom master page this approach will be best for you.

Open the master page in any tool of your choice, now find out the site actions tag and notice the various MenuItemTemplate tags for each link. Read more about them here.

Now copy and paste one of the tag to customize it. Now change the title and descriptions or may be the permission if you want. Notice the “ClientOnClickScriptContainingPrefixedUrl” attribute there which can contains the action to be performed when link will be clicked. Read more about it here.

For an example, In case you want to open the pop up then like “Add page” then use the OpenCreateWebPageDialog(‘~siteLayouts/createwebpage.aspx’) code.

If your link will navigate to new page then use GoToPage  like: GoToPage(‘~siteLayouts/settings.aspx’). Whole code should looks like:

<SharePoint:MenuItemTemplate runat=”server” ID=”MenuItem_CreateNews”
Text=”<%$Resources:XXX,SiteAction_AddNews_Title;%>”
Description=”<%$Resources:XXX,SiteAction_AddNews_Desc%>”
MenuGroupId=”200″
Sequence=”230″
UseShortId=”true”
ClientOnClickScriptContainingPrefixedUrl=”OpenCreateWebPageDialog(‘~siteCollectionLayouts/CreatePublishingPageDialog.aspx’)”
PermissionsString=”ManageLists, ManageSubwebs”
PermissionMode=”Any” />

You can use various tokens to create proper url to navigate: Tokens in Sharepoint.

Hope this helps you to create custom site setting link.

Happy SharePointing 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s