If you would like to discuss anything, please contact me via email: Howard.Bayliss@sequence.co.uk

Friday, January 28, 2011

SharePoint Relative Url

We have a number of page layouts which allow the user to enter a URL. SharePoint stores the value as an absolute value, along with a description.

(BTW, you can use the SPFieldUrlValue class to extract the URL and description).

Anyhow, I wanted to generate a relative path from stored absolute value. I used the following code to do it.

Maybe you know a better way.....?


SPFieldUrlValue spFieldUrlValue = new SPFieldUrlValue(fieldValue);

string targetUrl = spFieldUrlValue.Url;

using(SPSite site = new SPSite(targetUrl))
{
  return targetUrl.Replace(site.Url, String.Empty);
}

No comments:

Post a Comment