16

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.

Tortoise
  • 731
  • 4
  • 10
  • 21

8 Answers8

49

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];
Stefan
  • 14,530
  • 4
  • 55
  • 62
  • 1
    Do accept this as an answer if it helped you! It is good for you (improves your rating) and is good for the person who did give the right answer (more reputation)! – Dribbel Mar 07 '11 at 20:52
  • Hi Jason,I think you have to cast the second line as string absurl = (string)item[spbuiltinfieldid.Encodedurl]; – Tortoise Mar 10 '11 at 10:46
  • I didn't know about the SPBuiltInFieldId before, that was very helpful, thanks Stefan. – Marc May 24 '13 at 10:03
  • if original URL contains some Unicode chars like diacritics then it's not encoded and we may do a decode-encode operation: var encodedUrl = file.Item[SPBuiltInFieldId.EncodedAbsUrl] as string; var decodedUrl = SPEncode.UrlDecodeAsUrl(encodedUrl); encodedUrl = Uri.EscapeUriString(decodedUrl); – Oleg Savelyev Jun 09 '15 at 14:30
  • How can I achieve this in Client Object Model C# – Atish Kumar Dipongkor Aug 01 '15 at 09:48
3

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.

Colin Gardner
  • 531
  • 5
  • 5
2

Try this ,

          using (SPSite ospSite = new SPSite("http://abcd:24931"))
           {
              using (SPWeb web = ospSite.OpenWeb("/subsite")
               {
               // Get document library collection here and fetch all the document urls
                   SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["docu"]; 

                //where docu is my  document library
                SPListItemCollection items = docLib.Items;

                   foreach (SPListItem item in items)
                    {
                       string url = item.Url;
                    }
               }
          }

Hope this shall get you going.

Furqan Hameedi
  • 4,372
  • 3
  • 27
  • 34
2
public string GetItemURL(SPListItem item)
    {
        string web = item.Web.Url;
        string listID = item.ParentList.ID.ToString();
        string contentType = item.ContentTypeId.ToString();
        string itemID = item.ID.ToString();
        string url = web+"/_layouts/listform.aspx?PageType=4&ListID={"+listID+"}&ID="+itemID+"&ContentTypeID="+contentType;
        return url;
    }

It's Working for me. Hope I help (List Item url)

Andrey
  • 29
  • 2
1

Use below code to get absolute URL of file:

SPFile file;    
string url = file.Item[SPBuiltInFieldId.EncodedAbsUrl] as string;
Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
Nirikshita
  • 11
  • 2
1

If this is for the document library, try this one.

item.Web.Url+"/"+item.File.Url
0

For what it's worth, accessing the item.Web property means you are actually instantiating the SPWeb object which means that this should be disposed otherwise you'll be causing memory leakage.
It's a lot of overhead when there are better and quicker ways already mentioned.

I would use the BuiltInFieldId.EncodedAbsUrl approach mentioned since this gives you the easiest access to what you want.

Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
Steve
  • 111
  • 1
-4

The answer is string url = currentweb.url+"/"+ Listitem.url;

Tortoise
  • 731
  • 4
  • 10
  • 21
  • SPListItem.Url returns the site relative url of the list item. Since you concatenate this with the web url you will get the wrong url if the web is not the root web of the site collection. – Stefan Mar 10 '11 at 15:59
  • Yeah You are right.Please tell me the way to get it for subsites also Thank you – Tortoise Mar 11 '11 at 05:52