SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I work with SPUrl in my code-behind file for master pages and page layouts?

I don't see any access to this in the SharePoint namespaces.

share|improve this question ... in your code behind, why not use SPContext to get the url path?

  • SPContext.Current.Web.ServerRelativeUrl [Site]
  • SPContext.Current.Site.ServerRelativeUrl [Site Collection]
share|improve this answer
i was looking to make modifications to the SPURL mark-up from code behind, not sure if it's possible or if SPURL derives from anything? thx – Anonymous Feb 2 '10 at 19:28
check out the MSDN links Jaap and I both posted and you may also want to take a look at this article on creating your own custom expressionbuilder:… – Anonymous Feb 2 '10 at 20:30

If I understand the question right, you should be able to work with the SPUrl expression builder by instantiating it in code, obtaining the expression result, then doing whatever you need to do. Alternatively if you're trying to work with an existing instance, you should be able to get it in the code-behind by ID, though you'd need to pay attention to when in the page lifecycle the actual expression building happens.

Remember also that expression builders are typically only a wrapper anyway (generally around some logic which is more "central" but that you want to make use of in declarative markup), so you could get what you need by using a more direct route with the API e.g. per mike11steven's reply.

If you need an example of a custom expression builder, my SharePoint Config Store project has one.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.