JavaScript in Elements.xml file of a SharePoint Feature
Here my requirement was to create a menu/link in SharePoint's SiteActions, on click of which it redirects to custom site page created under Pages folder [/Pages/MyPage.aspx].
Challenges:
1. To execute JavaScript code on menu/link click.
2. Build the absolute URL of custom page.
Solution:
1. First create a SharePoint Feature to add a new menu/link in SiteActions.
2. Modify the Elements.xml using CustomAction/URLAction to execute the JavaScript code.
3. Write the required JavaScript code as per requirement [I have written a small piece of code to fetch the current server name and then build the absolute URL of custom page].
4. Install and Activate the SPFeature.
Following is the code snippet of Elements.xml file :
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
GroupId = "SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="1000"
RequireSiteAdministrator="TRUE"
Title="My Links">
<UrlAction Url="javascript:function process()
{
var site='{SiteUrl}';
var pageurl = '';
var serverName = '';
var endindex = site.indexOf('/', '10');
if(endindex == -1) { serverName = site; }
else { serverName = site.substring(0, endindex); }
pageurl = serverName + '/Pages/MyPage.aspx';
window.location = pageurl;
};
process();"/>
</CustomAction>
</Elements>
