How to get the Absolute URL of a file in sharepoin

2019-02-04 14:27发布

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.

8条回答
Emotional °昔
2楼-- · 2019-02-04 15:01

Use below code to get absolute URL of file:

SPFile file;    
string url = file.Item[SPBuiltInFieldId.EncodedAbsUrl] as string;
查看更多
\"骚年 ilove
3楼-- · 2019-02-04 15:10

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.

查看更多
Emotional °昔
4楼-- · 2019-02-04 15:10

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

查看更多
Juvenile、少年°
5楼-- · 2019-02-04 15:12

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];
查看更多
地球回转人心会变
6楼-- · 2019-02-04 15:14
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)

查看更多
Anthone
7楼-- · 2019-02-04 15:15

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.

查看更多
登录 后发表回答