Wednesday, May 22, 2013

Using Strings and Tokens as Querystring Parameters in SharePoint Links

At some point during your development adventures you'll need to put a rather generic link somewhere with the expectation that no matter where it is displayed you can get relevant information about the current site, list, etc.  For me this ended up being a Custom Action.  I needed to place a link on the List Settings page and append the list ID as a querystring parameter.  Luckily SharePoint has a set of URL tokens that help out in these scenarios.

First let's take a look at my Custom Action:

  <CustomAction Id="Custom.Configuration.ListCustomSettings"
    Title="My Awesome Link"
    Location="Microsoft.SharePoint.ListEdit"
    RequireSiteAdministrator="FALSE"
    GroupId="GeneralSettings"
    Sequence="1000" >
    <UrlAction Url="~site/_layouts/MyAdminPage.aspx?ListID={ListId}" />
  </CustomAction>

As you can see above the UrlAction Url property is taking advantage of two of these tokens:
  • ~site - Web site (SPWeb) relative link.
  • {ListId} - GUID that represents the list.
Now when this link is rendered on the List Settings page, the URL of the site and the list GUID will be populated in the link.  So this link can be rendered on any List Settings page throughout my Farm, and I can be guaranteed to get relevant information about where its being rendered.

MSDN has posts detailing all of the tokens available to you for your version of SharePoint:

SharePoint 2010 - http://msdn.microsoft.com/en-us/library/ms431831(v=office.14).aspx
SharePoint 2013 - http://msdn.microsoft.com/en-us/library/ms431831(v=office.15).aspx
Apps for SharePoint - http://msdn.microsoft.com/en-us/library/jj163816.aspx

Enjoy!

1 comment:

  1. Great blog for sharepoint ,thanks for sharing this information .





    Sharepoint Development

    ReplyDelete