How to get the Absolute URL of a file in sharepoint library

Tortoise picture Tortoise · Mar 7, 2011 · Viewed 71.4k times · Source

I am working on SharePoint 2010.I have an documentlibrary ID and document ID in that library with me.i don't have either web,site in which the document library is present.So now I have to get the Full URL of the document at runtime.How can I get it . I have tried the following.

string filepath = currentList.DefaultViewUrl + "/" + sListItem.Url;

Please answer this.

Answer

Stefan picture Stefan · Mar 7, 2011

Use the field "EncodedAbsUrl" on the SPListItem. Works for SPFile as well:

SPListItem item = ...;
string absUrl = (string) item[SPBuiltInFieldId.EncodedAbsUrl];

or for a SPFile

 SPFile file = ...;
 string absUrl = (string) file.Item[SPBuiltInFieldId.EncodedAbsUrl];