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).
3 thoughts on “Getting Absolute URLs in SharePoint”
Great article! Helped me with my issue. I also blogged about it as a reminder for myself and linked back to your article. http://www.sharepointwendy.com/2012/02/how-to-get-absolute-urls-in-sharepoint.html. Thanks for posting this!
I have a strange problem..
lets say my relative url is:
when i use the MakeFullUrl method i get:
when it should be :http://port/sites/ABCSiteCollectionNew/ProjectLib/doc1.doc
do you have any idea why that happens??
I would guess it’s that the MakeFullUrl relies on the ‘relative’ url being relative to the Site Collection root, rather than the server root. What object are you gettig your relative URL from? Most objects – SPFile, SPList, etc. – have multiple properties for getting these different URLs relative to the server, site collection, or site.