none
How to get File Url from document Library using Datatable c# RRS feed

  • Question

  • Hi,

    have retrieved data successfully.

    Requirement :

    just need to know how can I pick url of file like we may do using

    itemcollection ---> pListItem--- >    pListItemfolder.File.Url;

    Solution Required to pick url of file from document library using datattable

    Thanks


    Friday, July 3, 2020 1:02 PM

Answers

  • Hi,

    Add "FileRef" as ViewFields in the code snippet, this field is the file relative url path,

    For absolute file url, you can prepend the site domain:
       using (SPSite site=new SPSite("http://sp/sites/MyDev"))
                {
                    SPList List = site.OpenWeb().Lists["Documents"];
                    SPQuery query = new SPQuery();
                    query.Query= @"<Where><Eq> <FieldRef Name='FSObjType' /> <Value Type='Integer'>0</Value></Eq></Where>";
                    query.ViewFields = @"<FieldRef Name='FileRef' />"; 
                    query.ViewAttributes = "Scope='RecursiveAll'";
                    DataTable dt = List.GetItems(query).GetDataTable();
                    foreach (DataRow item in dt.Rows)
                    {
                        Console.WriteLine("FileUrl: "+"http://sp"+ item["FileRef"]);
                    }
    
                }


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, July 7, 2020 7:57 AM

All replies

  • Hi,

    Filter FSObjType eq 0 firstly using SPQuery class to get all file items and then get the file url, please check following demo:

       using (SPSite site=new SPSite("http://sp/"))
                {
                    SPList List = site.OpenWeb().Lists["Documents"];
                    SPQuery query = new SPQuery();
                    query.Query= @"<Where><Eq> <FieldRef Name='FSObjType' /> <Value Type='Integer'>0</Value></Eq></Where>";
                    query.ViewAttributes = "Scope='RecursiveAll'";
                    SPListItemCollection listItems = List.GetItems(query);
                    foreach (SPListItem item in listItems)
                    {
                        Console.WriteLine("File Url: " + site.Url + item.File.ServerRelativeUrl);
                    }
                }

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, July 6, 2020 2:30 AM
  • Thanks for reponse

    As I mentioned above returing Datatable not "SPListItemCollection ".

    Solution Required: while returning datatable like

         datatable dt= List.GetItems(query).getdatatable();

    how to get file URL ??

    Tuesday, July 7, 2020 7:28 AM
  • Hi,

    Add "FileRef" as ViewFields in the code snippet, this field is the file relative url path,

    For absolute file url, you can prepend the site domain:
       using (SPSite site=new SPSite("http://sp/sites/MyDev"))
                {
                    SPList List = site.OpenWeb().Lists["Documents"];
                    SPQuery query = new SPQuery();
                    query.Query= @"<Where><Eq> <FieldRef Name='FSObjType' /> <Value Type='Integer'>0</Value></Eq></Where>";
                    query.ViewFields = @"<FieldRef Name='FileRef' />"; 
                    query.ViewAttributes = "Scope='RecursiveAll'";
                    DataTable dt = List.GetItems(query).GetDataTable();
                    foreach (DataRow item in dt.Rows)
                    {
                        Console.WriteLine("FileUrl: "+"http://sp"+ item["FileRef"]);
                    }
    
                }


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, July 7, 2020 7:57 AM
  • Hi qasimidl,

     

    Would you please share some update on this question ? Did you solve it ?

     

    If my reply is helpful to this question, you could Mark as answer so that it could also help others in the forum who have the similiar question.

     

    Thanks

     

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, July 13, 2020 4:56 AM
  • FileRef this field name does not exist in document library,

    showing exception "column name FileRef  doesnot belong to table"

    • Edited by qasimidl Tuesday, July 14, 2020 7:30 AM
    Tuesday, July 14, 2020 7:26 AM
  • FileRef this field name does not exist in document library,

    showing exception "column name FileRef  doesnot belong to table"

    Hi qasimidl,

    You will need to add FileRef in query.ViewFields as the code snippet above, then in the DataTable, it will have such column:

    Here is the field list for SharePoint library, please check it, FileRef is existed at Document Library:

    https://dipansaha.wordpress.com/2012/07/23/useful-reference-for-sharepoint-internal-fields-name/

    Thanks

    Best Regards


    "SharePoint" forums will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !


    Tuesday, July 14, 2020 7:38 AM
  • Resolved by giving internal name "LinkFilename"

    Tuesday, July 14, 2020 7:53 AM
  • Hi qasimidl,

    Great to hear you resolve this question.

    You can Mark the reply as answer to close this question and make it helpful to others in the forum :)

    Thanks

    Best Regards


    "SharePoint" forums will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !

    Tuesday, July 14, 2020 8:12 AM