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

8

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.

Brussels answered 7/3, 2011 at 7:8 Comment(0)
O
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];
Orville answered 7/3, 2011 at 8:22 Comment(5)
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)!Scolex
Hi Jason,I think you have to cast the second line as string absurl = (string)item[spbuiltinfieldid.Encodedurl];Brussels
I didn't know about the SPBuiltInFieldId before, that was very helpful, thanks Stefan.Cardew
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);Transformism
How can I achieve this in Client Object Model C#Rsfsr
U
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.

Underquote answered 13/8, 2013 at 1:30 Comment(0)
B
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.

Blast answered 7/3, 2011 at 7:51 Comment(2)
It only gives the site relative url.But i need the absolute urlBrussels
in terms of "abcd:24931" and "/subsite" , you already have the site url , so you can concatenate and prepare the absolute Url.:)Blast
C
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)

Chappelka answered 3/1, 2012 at 14:45 Comment(1)
This works but it brings to the "File properties" view.Behoof
M
1

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

item.Web.Url+"/"+item.File.Url
Mot answered 23/8, 2011 at 7:32 Comment(0)
P
1

Use below code to get absolute URL of file:

SPFile file;    
string url = file.Item[SPBuiltInFieldId.EncodedAbsUrl] as string;
Psychokinesis answered 1/10, 2013 at 10:34 Comment(0)
U
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.

Unwrap answered 6/1, 2012 at 12:11 Comment(0)
B
-4

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

Brussels answered 10/3, 2011 at 13:59 Comment(2)
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.Orville
Yeah You are right.Please tell me the way to get it for subsites also Thank youBrussels

© 2022 - 2024 — McMap. All rights reserved.