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

强颜欢笑 提交于 2019-11-30 06:46:28

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];

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.

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.

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

item.Web.Url+"/"+item.File.Url
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)

Steve

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.

Nirikshita

Use below code to get absolute URL of file:

SPFile file;    
string url = file.Item[SPBuiltInFieldId.EncodedAbsUrl] as string;

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!