Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 990
Tortoise Avatar asked Mar 07 '11 07:03

Tortoise


People also ask

How do I get the URL of a SharePoint file?

Here is another way to get the direct URL of any file in SharePoint Online. Just select the file >> Open the document information panel by clicking on the “i” icon and clicking on the little copy icon under “Path”! This gets you the direct URL of the document in your clipboard!

What is SharePoint absolute URL?

Types of URLs in SharePoint An absolute URL specifies a full path and begins with a protocol. For example, http:// domain_or_server/[ sites/ ] Web_Site/ Lists / List_Title/ AllItems. aspx . A domain-relative URL is based on the domain (which might be the name of a server) address and always begins with a forward slash.


2 Answers

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];
like image 159
Stefan Avatar answered Sep 18 '22 16:09

Stefan


The built in field id is for sure the best way to go but it returns the Url as encoded which may or may not be what you want.

I think the best way is to add a little extension method to a utilities class somewhere:

public static string AbsoluteUrl(this SPFile File, bool Decode = true)
{
    string EncodedUrl = File.Item[SPBuiltInFieldId.EncodedAbsUrl].ToString();
    if (Decode)
        return SPEncode.UrlDecodeAsUrl(EncodedUrl);
    else
        return EncodedUrl;
}

And then call as follows

Item.File.AbsoluteUrl();

if you want a decoded Url or

Item.File.AbsoluteUrl(false);

if you want the Url to remain encoded.

Note that the default parameter value for Decode will only be available in .Net4+ and therefore SP2013 only but you can easily create an overload method for SP2010. You'll also need a reference to Microsoft.SharePoint.Utilities namespace to access the SPEncode class.

like image 41
Colin Gardner Avatar answered Sep 22 '22 16:09

Colin Gardner