SharePoint Internals – Hristo Pavlov’s Blog

8 December, 2008

UrlAction Tokens of the CustomAction Feature

Filed under: SharePoint — Tags: , , , , , , , , , — hristopavlov @ 1:07 am

I was looking today for the complete list of tokens that can be used in the UrlAction element when building a CustomAction feature but I couldn’t find much. So I decided to put together this blog entry to help other people that may be looking for the same thing.

As you know some tokens can be used when specifying the Url of the custom action: 

<UrlActionUrl=”~site/_layouts/ItemAudit.aspx?ID={ItemId}&amp;List={ListId}”/>

Those token will be replaced by SharePoint at runtime with values derived from the current context. To get the complete list we just need to find the function that does the replacement. Luckily this function is in the .NET libraries of SharePoint and can be disassembled with Reflector. So here is the complete list of the tokens:

Token Replaced By
~site/ SPContext.Current.Web.ServerRelativeUrl
~sitecollection/ SPContext.Current.Site.ServerRelativeUrl
{ItemId} item.ID.ToString()
{ItemUrl} item.Url
{SiteUrl} web.Url
{ListId} list.ID.ToString(“B”)
{RecurrenceId} item.RecurrenceID

For the records the method that does the replacement is Microsoft.SharePoint.SPCustomActionElement.ReplaceUrlTokens() and looks like this:

private static string ReplaceUrlTokens(

    string urlAction,

    SPWeb web,

    SPList list,

    SPListItem item)

{

    string recurrenceID;

 

    if (!string.IsNullOrEmpty(urlAction))

    {

        if (item != null)

        {

            string newValue = item.ID.ToString(CultureInfo.InvariantCulture);

 

            urlAction = urlAction.Replace(“{ItemId}”, newValue);

            urlAction = urlAction.Replace(“{ItemUrl}”, item.Url);

            recurrenceID = newValue;

 

            if (!string.IsNullOrEmpty(item.RecurrenceID))

            {

                recurrenceID = item.RecurrenceID;

            }

        }

    }

    else

    {

        return urlAction;

    }

 

    urlAction = urlAction.Replace(“{RecurrenceId}”, recurrenceID);

 

    if (web != null)

        urlAction = urlAction.Replace(“{SiteUrl}”, web.Url);

 

    if (list != null)

        urlAction = urlAction.Replace(“{ListId}”, list.ID.ToString(“B”));

 

    // Replaces ~site/ and ~sitecollection/ with the site and site collection urls

    urlAction = SPUtility.GetServerRelativeUrlFromPrefixedUrl(urlAction);

 

    return urlAction;

}

The web, list and item arguments are taken from the context before the user is redirected to the custom action page.

14 Comments »

  1. […] UrlAction Tokens of the CustomAction Feature […]

    Pingback by Links (12/11/2008) « Steve Pietrek - Everything SharePoint — 12 December, 2008 @ 2:03 am

  2. Hi Hristo.

    First of all, thank you very much for this blog post. It was really helpful.
    But I have a problem I don’t know how to solve:
    I’d written a feature that replaces the builtin upload forms of a document library with custom ones.
    But now I ran into the problem, that not all of the tokens in the UrlAction element are replaced properly.
    I’m looking for a solution for about 3 hours, but couldn’t find any mistake within my code.

    This is the markup specifying the ‘Upload Document’ upload menu item in the feature:

    And the url is replaced with this:
    http://%5BSite%5D:%5BPort%5D/%5BWeb%5D/_layouts/%5BLayoutsSubFolder%5D/Upload.aspx?List=4056dfd9-a48f-4403-a04b-5473492653f9&RootFolder=ItemUrl

    (Identifiers within square backets are replaced with project specific values.)

    This is the url I get within each folder within the document library (incl. the root folder).

    The custom upload page is derived from the Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase class.
    I’m using Office SharePoint Server 2007 v. 12.0.6425.1000

    Do you’ve got any idea what I am doin’ wrong?

    Comment by Benjamin Beck — 13 July, 2009 @ 3:36 pm

  3. I hope this works:

    <CustomAction Id=”DocLibUploadToolbarButton”
    RegistrationType=”List”
    RegistrationId=”10001″
    GroupId=”UploadMenu”
    Rights=”AddListItems”
    Location=”Microsoft.SharePoint.StandardMenu”
    Sequence=”120″
    Title=”Upload Document”
    ShowInLists=”TRUE”
    Description=”[Description]”
    ImageUrl=”_layouts/images/MenuUploadDocument.gif”&gt
    &ltUrlAction Url=”~site/_layouts/[LayoutsSubFolder]/Upload.aspx?List={ListId}&RootFolder={ItemUrl}” /&gt
    &lt/CustomAction&gt

    Comment by Benjamin Beck — 13 July, 2009 @ 3:38 pm

  4. one thing to note here is that these token cannot be repeated in the URL, you will have to do some ugly javascript hacks to use them more than once in the URL

    Comment by FZ — 30 July, 2009 @ 8:20 pm

  5. IMO having CultureInfo.InvariantCulture is a bit extra in the code. Thank you for the post!

    Comment by Ilya — 28 May, 2010 @ 12:56 pm

  6. […] UrlTokens of the CustomAction Feature – tokens that can be used with UrlAction for CustomActions […]

    Pingback by Tune Up Your PC » Post Topic » Creating a SharePoint Site Page With Code-Behind Using Visual Studio 2010 — 30 June, 2010 @ 8:00 am

  7. Just to comment the difference between “~site/” and “{SiteUrl}”.

    The “~site/” must be used at the beginning of the UrlAction. If it is used in the middle of the string, it won’t work. (Since SharePoint look for the “~” at the first character of the line as the indicator of a relative url).

    “{SiteUrl}” can be used inside the string or at the beginning of the string.

    Comment by Ethan — 19 September, 2010 @ 8:51 pm

  8. Do you know if there is a token for the source page?

    Comment by Vu Dang — 20 March, 2011 @ 1:39 pm

  9. Thanks Hristo, 3 years after you wrote this blog post, it’s exactly what I needed to know.

    Comment by David Barrows — 30 May, 2011 @ 6:51 pm

  10. […] When creating CustomAction Elements, a number of tokens are available for use within the UrlAction Element in SharePoint 2010. The list of UrlAction tokens in SharePoint 2010 has grown ever so slightly (For a SharePoint 2007 reference, see UrlAction Tokens Of The CustomAction Feature). […]

    Pingback by UrlAction Tokens in SharePoint 2010 | mylifeinaminute.com — 2 September, 2011 @ 7:25 pm

  11. Hi
    Thank you for this useful post. I have a problem I hope you can help me to solve it.
    I have a module which is added by a feature to the sharepoint. it adds a extra link in Site Settings to open a aspx file. the problem is I have used {SiteUrl} in the UrlAction which works fine only on root web. what I mean siteurl is right only for rootweb which is http://myappsite and if I try to open a aspx in a sub web the SiteUrl is http://mysappsite/subweb but the aspx file only exists in myappsite/sitepages/ and it does not exists in myappsite/subweb/sitepages. I tested to use ~/sitecollection with and without {} but it does work. following is element.xml of the module.

    Thank you very much for your help.

    Comment by koruosh — 13 November, 2011 @ 7:57 pm

  12. […] After seeing that, I really wanted to know what other magic tokens you can use, but the Microsoft Documentaiton on the class doesn’t even show the inline $SPUrl syntax, let alone list any of the usable tokens! Doing a quick search will find you an assortment of undocumented tokens. However this can be misleading as the tokens listed in {} braces are generally only for custom actions and not for inline links (you can find examples of those lists here and here). […]

    Pingback by Relative Paths in SharePoint using SPUrlExpressionBuilder ($SPUrl) « The Chris Kent — 2 April, 2012 @ 2:51 pm

  13. For custom actions you can use a token called {Source} and it will add the normal Source query paramater that you see SharePoint 2010 using.

    Comment by Subhash — 30 October, 2012 @ 12:36 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.