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

血红的双手。 提交于 2019-11-30 13:17:30

问题


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.


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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

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



回答5:


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)




回答6:


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.




回答7:


Use below code to get absolute URL of file:

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



回答8:


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



来源:https://stackoverflow.com/questions/5216832/how-to-get-the-absolute-url-of-a-file-in-sharepoint-library

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