Rebasing Absolute URLs in SharePoint

SharePoint has this funny mechanism ‘Alternate Access Maps’. Essentially, this means that the same content can be available via different web applications, which have different URLs.

But what do you do if you’ve an absolute URL to something in SharePoint, and you need it to be correct for the users’s current alternate access map zone?

Well, there is a function SPUtility.AlternateServerUrlFromHttpRequestUrl() – but I couldn’t see how to use it. Internally, it uses RebaseUriWithAlternateUri() – looks promising. And it works.

string url - "Absolute URL in the wrong zone";
SPUrlZone zone = SPContext.Current.Site.Zone;
Uri currentZoneUrl = SPFarm.Local.AlternateUrlCollections.RebaseUriWithAlternateUri(new Uri(url), zone);

I’m a bit suspicious of using the local SPFarm object, but that seems to work correctly. Others do seem to have done this:

Rebasing Absolute URLs in SharePoint

Get the URL of a list

This is a seemingly simple task – given a List or Library in SharePoint, how do I get it’s URL? The difficulty here is that a List has multiple URLs – one for each View on the list. We can get those URLs, but sometimes you really just want the URL to the list, without the /forms/something.aspx bit too.

For example, you might want http://server/siteCollection/site/lists/somelist . If you entered this URL, you’d be taken to the default View for the list, so you don’t really need the extra /forms/… bit to identify the list – that’s all about identifying the view.

Sadly, though, there is no SPList.Url property or equivalent on the SPList object. So, how can I get a list’s URL? Continue reading “Get the URL of a list”

Get the URL of a list

Getting Absolute URLs in SharePoint

This is a bit of a reminder for myself, but quite a lot of the URLs you deal with in SharePoint development are relative – that is, something like an SPList.DefaultViewURL might be:


That’s fine … but sometimes you need a full absolute URL. For example, you might be sending out an email with a hyperlink in it – a relative URL ain’t going to cut it.

Fortunately, there is a very useful function – SPSite.MakeFullUrl() :

Returns the full URL for the specified server-relative URL.

That’s great – but it raises some questions. What about Alternate Access Maps? What URL would be returned? Well, predictably, the URL used in the SPSite Constructor in the first place. Mostly, they either accept a URL (which defines a zone), or they accept a GUID and an SPUrlZone value. There is a constructor that just accepts a GUID – which will then use the default zone. Similarly, the constructor accepting a GUID and SPUserToken also uses the Default Zone.

(If you open it up in reflector, you can see it just calling a private constructor passing the SPUrlZone.Default value).

Getting Absolute URLs in SharePoint