Wednesday, September 05, 2007 9:42 AM Jan Tielens

Using the Current Page URL in the UrlAction of a SharePoint Feature

With the introduction of the feature framework in SharePoint 2007, developers have some great opportunities to customize and enhance nearly everything in SharePoint. One of the things that's quite easy to do with the help of a feature, is to add and/or replace functionality in the web user interface of SharePoint site. This was typically very hard to do (in a nice way) for the previous version of SharePoint (remember having to edit a javascript file to add a menu item in the ECB?).

Let's take the following example of a feature's manifest file:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
        Id="{6FCB0F81-2105-4d9f-96BF-C48A19B8E439}"
        Title="My Link"
        Location="Microsoft.SharePoint.StandardMenu"
        GroupId="SettingsMenu">
        <UrlAction Url="_layouts/mypage.aspx"/>
    </CustomAction>
</Elements>

When activated, the feature will add a menu item to the Settings menu of any SharePoint list or document library. The menu item will have the title My Link, when clicked the user will navigate to the page mypage.aspx in the _layouts folder (the typical place to deploy your custom application pages).

If you build some functionality in the mypage.aspx, in many scenarios this page will need to know from which list the link originated. This can be done by using URL tokens in the UrlAction element:

<UrlAction Url="_layouts/mypage.aspx?listid={ListId}"/>

The {ListId} URL token will be automatically replaced with the ID of the list, in which the menu item is shown. In the mypage.aspx, you can retrieve the value of the listid parameter by making use of the QueryString. Once you've got the ID, the object model can be used to get a reference to the SPList instance of that list. According to the documentation on MSDN, the following URL tokens can be used:

  • ~site - Web site (SPWeb) relative link.
  • ~sitecollection - site collection (SPSite) relative link.
  • In addition, you can use the following tokens within a URL:
    • {ItemId} - Integer ID that represents the item within a list.
    • {ItemUrl} - URL of the item being acted upon. Only work for documents in libraries. [Not functional in Beta 2]
    • {ListId} - GUID that represents the list.
    • {SiteUrl} - URL of the Web site (SPWeb).
    • {RecurrenceId} - Recurrence index. This token is not supported for use in the context menus of list items.

Unfortunately there is no token that will give you the URL of the page on which the feature's link is being displayed. In many cases you want to have that URL to be able to redirect, after you've shown your custom functionality, to the originating page. SharePoint itself uses this technique a lot: in many URL's you'll find the Source parameter:

http://wss.u2ucourse.com/Lists/Links/NewForm.aspx?Source=http%3A%2F%2Fwss%2Eu2ucourse%2Ecom%2Fdefault%2Easpx

The URL above points to the NewForm.aspx for the a Links list. Normally when the user fill's out this form and clicks OK, this page redirects to the default view of the list. Because this link has the Source parameter, when the user clicks OK (or cancel), the page will redirect to the default.aspx instead. You can add the Source parameter to a lot of pages in SharePoint, giving you full control over the redirecting.

So the issue is: we want to include the URL of the originating page in the UrlAction element of the feature's CustomAction, but all we get are a bunch of ID's and some URL's that are not useful for this scenario. As usual peeking in the machine room of SharePoint itself can give you some good ideas to solve this issue. The SharePoint guys themselves sometimes use Javascript functions in the UrlAction, instead of ordinary hyperlinks. Thus with some clever use of Javascript, it's quite easy to solve the problem:

<UrlAction Url="javascript:window.location= '{SiteUrl}/_layouts/mypage.aspx?List={ListId}&amp;Source=' + window.location"/>

The actual link is a Javascript function that will navigate to a specific URL. This URL is a concatenation of the URL of the page to display (including for example the ID of the list as a parameter in the QueryString), and the Source parameter which is dynamically set the current page's URL. Et voila, the constructed link will point to your page, and the redirect will always point to the page you started from.

Extra: this tric can also be used to overcome a bug in SharePoint that causes a URL token of a CustomAction to be replaced only once. So if you have used the ListID token two times in a UrlAction element, only one of the token's will be replaced with the actual ID of the list. The user "FlatEric" (what's in a name?) explains this in the Community Content of the How to: Add Actions to the User Interface article on MSDN.

I found an ugly way to bypass this flaw:
<UrlAction Url="javascript:function process(){var site='{SiteUrl}';var item={ItemId};window.location.href=site+'/Lists/MyList/NewForm.aspx?ID='+item+'&amp;Source='+site+'/Lists/myOtherList/DispForm.aspx?ID='+item;};process();"/>

Filed under:

Comments

# re: Using the Current Page URL in the UrlAction of a SharePoint Feature

Wednesday, September 05, 2007 10:41 AM by Tony

Thanks very much for the javascript tip.

A question for you though: I am trying to create a feature to upload a document to the current folder of a document library by adding a custom action to the Upload Menu of document library. By using the token {ListId} I can upload the document to the root folder of the document library, but I have found no way to identify the current folder . Any suggestions?

Thanks,

Tony

# Links (9/5/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

Wednesday, September 05, 2007 8:47 PM by Links (9/5/2007) « Steve Pietrek’s SharePoint Stuff

Pingback from  Links (9/5/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

# re: Using the Current Page URL in the UrlAction of a SharePoint Feature

Thursday, September 06, 2007 2:10 AM by Jan Tielens

Tony, if you include the page URL as a parameter, you'll be able to retrieve the folder from that URL.

# re: Using the Current Page URL in the UrlAction of a SharePoint Feature

Thursday, September 06, 2007 2:30 AM by FlatEric (Eric Bartels)

Hi its FlatEric,

nice post and nice to be mentioned in here :-)

PS: The name "FlatEric" is from a song (some days old now) where a yellow doll moved to a song named "flat beat". This doll was "FlatEric" ... (See his photo: www.schadeberg.de/flateric.jpg)

Greetings from Germany :-)

# re: Using the Current Page URL in the UrlAction of a SharePoint Feature

Thursday, September 06, 2007 11:46 AM by Tony

Hi Jan,

Is this what you refer to?

<UrlAction Url="javascript:window.location= '{SiteUrl}/_layouts/mypage.aspx?List={ListId}&amp;PageUrl=' + window.location"/>

Then in the code behind or inline script, I can parse the PageUrl parameter.

Thanks,

Tony

# re: Using the Current Page URL in the UrlAction of a SharePoint Feature

Thursday, September 06, 2007 1:36 PM by Jan Tielens

Yes, that should do the trick. :-)

# re: Using the Current Page URL in the UrlAction of a SharePoint Feature

Wednesday, September 19, 2007 5:28 PM by mosza

You rock! I've been searching for a legit param in Action Menu for the current folder for sometime now.

thanks.

# hosthg &raquo; Blog Archive &raquo; Using the Current Page URL in the UrlAction of a SharePoint Feature

Pingback from  hosthg  &raquo; Blog Archive   &raquo; Using the Current Page URL in the UrlAction of a SharePoint Feature

# SharePoint 2007: Using the Source Query String Parameter &laquo; SharePoint Sherpa

Pingback from  SharePoint 2007: Using the Source Query String Parameter &laquo; SharePoint Sherpa

# cj 2ecom

Sunday, September 07, 2008 7:32 AM by cj 2ecom

Pingback from  cj 2ecom

# Локализация Windows Live Authentication

Wednesday, November 12, 2008 4:06 AM by DkmS's блог

На codeplex lawrenceliu размещено отличное решение по аутентификации на Sharepoint-сайтах с использованием...

# Локализация Windows Live Authentication

Tuesday, December 16, 2008 6:59 AM by DkmS's блог

На codeplex Lawrence Liu размещено отличное решение по аутентификации на Sharepoint-сайтах с использованием...

# Adding a Source parameter to the ECB UrlAction &laquo; SharePoint NutBag&#8217;s Weblog

Pingback from  Adding a Source parameter to the ECB UrlAction &laquo; SharePoint NutBag&#8217;s Weblog

# CustomAction

Tuesday, September 08, 2009 12:14 AM by Confluence: SharePoint Development Wiki

The CustomAction element is used to add links to menus throughout SharePoints user interface. This includes adding links to locations such as the site settings pages, or to the list menu toolbars. A detailed list of attributes can be foundo n MSDN at