Wednesday, March 23, 2011

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].

1. To execute JavaScript code on menu/link click.
2. Build the absolute URL of custom page.

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="">
                                GroupId = "SiteActions"
                                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;         


No comments: