Retrieve SPList URL programmatically

Jonas W picture Jonas W · Dec 30, 2011 · Viewed 27.2k times · Source

Lets say I have a SharePoint List provided on URL http://webapp:1234/Lists/TestList.

In code, I have access to the corresponding SPList-Instance only. How can I get the URL of this SPList-Instance programmatically?

The SPList class is missing an URL property or something like that...

Answer

Ondrej Tucny picture Ondrej Tucny · Dec 30, 2011

You need to combine the list's root folder URL with the web URL. Root folder URL can be accessed via SPFolder.Url and web's absolute URL via SPWeb.Url. Providing you have a list it will be something like list.ParentWeb.Url + list.RootFolder.Url.

However, read the documentation, try it and fix things like proper slashes etc. You might also need to figure out the hostname portion of the URL from the parent SPWebApplication object. Note that the site may be accessible via multiple hostnames because of alternate access mappings.

Also, SPUtility.GetFullUrl may come in handy to convert a server-relative URL into a full URL.