Get the URL of a List in sharepoint using Caml Query

10,259

Solution 1

You can use some internal fields to build a link to the document. Here are some useful ones with an example of the data in each field:

  • FileRef - 1;#sites/SiteCollection/Kit/Kits Site Documents/Excel Report.xls
  • FileLeafRef - 1;#Excel Report.xls
  • FileDirRef - 1;#sites/SiteCollection/Kit/Kits Site Documents
  • ServerUrl - /sites/SiteCollection/Kit/Kits Site Documents/Excel Report.xls
  • EncodedAbsUrl - http://server/sites/SiteCollection/Kit/Kits%20Site%20Documents/Excel%20Report.xls

You'll just add one of the fields above as another viewfield to objSPSiteDataQuery.ViewFields:

objSPSiteDataQuery.ViewFields = "<FieldRef Name=\"ServerUrl\"/>" +
                                "<FieldRef Name=\"LinkFilename\"/>" +
                                "<FieldRef Name=\"Title\" />" +
                                "<FieldRef Name=\"Created\" />" +
                                "<FieldRef Name=\"Modified\"/>" +
                                "<FieldRef Name=\"Editor\"/>";

Solution 2

I did it like this:

 row["AbsolutePath"] = String.Format("{0}{1}", row["EncodedAbsUrl"], row["FileRef"].ToString().Substring(row["FileRef"].ToString().IndexOf("#") + 1));
Share:
10,259
Imir Hoxha
Author by

Imir Hoxha

Updated on June 11, 2022

Comments

  • Imir Hoxha
    Imir Hoxha almost 2 years

    I am using SPSiteDataQuery to display documents from different lists. I display the documents using a Gridview. One of the columns of the Gridview is an hyperlinkfield. How can I set the url of each document since each of them comes from different different document libraries? For your information, I am using Caml Query to filter the documents.

    Please help me.

    here is the code:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using System.Data;
    using Microsoft.SharePoint.Utilities;
    
    namespace Uniway.FOD.Intranet.ControlTemplates
    {
        public partial class Documents : UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                GridView1.DataSource = GetAllDocuments();
                // Set up the field bindings.
                BoundField boundField = new BoundField();
                boundField.HeaderText = "File name";
                boundField.DataField = "Title";//"LinkFilename";
                GridView1.Columns.Add(boundField);
    
                HyperLinkField hyperlinkField = new HyperLinkField();
                hyperlinkField.HeaderText = "Link name";
                hyperlinkField.DataTextField = "LinkFileName";
                hyperlinkField.DataNavigateUrlFields = new[] { "LinkFileName" };
                hyperlinkField.DataNavigateUrlFormatString = "{0}";
                GridView1.Columns.Add(hyperlinkField);
    
                BoundField boundField2 = new BoundField();
                boundField2.HeaderText = "Link File Name";
                boundField2.DataField = "LinkFilename";
                GridView1.Columns.Add(boundField2);
    
                GridView1.DataBind();
            }
    
            public DataTable GetAllDocuments()
            {
                SPSiteDataQuery objSPSiteDataQuery = null;
                SPWeb objSPWeb = null;
                DataTable objDataTable = null;
    
                objSPWeb = SPContext.Current.Web;
                objSPSiteDataQuery = new SPSiteDataQuery();
    
                //Specify the fields to be fetched in the results.Similar to select clause of an SQL query
    
                objSPSiteDataQuery.ViewFields = "<FieldRef Name=\"LinkFilename\"/>" +
                                                "<FieldRef Name=\"Title\" />" +
                                                "<FieldRef Name=\"Created\" />" +
                                                "<FieldRef Name=\"Modified\"/>" +
                                                "<FieldRef Name=\"Editor\"/>";
    
                //specifying list server template=101 so that it will query only document libraries
    
                objSPSiteDataQuery.Lists = "<Lists ServerTemplate=\"101\" BaseType=\"1\" Hidden=\"FALSE\" MaxListsLimit=\"0\"/>";
    
                objSPSiteDataQuery.RowLimit = 1000;
                objSPSiteDataQuery.Webs = "<Webs Scope=\"Recursive\"/>";
    
                //querying all documents of the content type 'CT23December1' having version=1.0
    
                objSPSiteDataQuery.Query = @"<Where> 
                                              <Eq> 
                                                <FieldRef Name='File_x0020_Type' /> 
                                                <Value Type='Text'>doc</Value> 
                                              </Eq>
                                            </Where><OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
    
                objDataTable = objSPWeb.GetSiteData(objSPSiteDataQuery);
                return objDataTable;
            }
        }
    }
    
  • Imir Hoxha
    Imir Hoxha over 12 years
    Hi Kit, thank you for your reply. I have used EncodeAbsUrl, but I don't get the full url as you show on your example. This is what I get: EncodedAbsUrl: http:// sp2010/ ServerUrl: / LinkFilename: How to.docx What am I doing wrong
  • Kit Menke
    Kit Menke over 12 years
    Wow crazy.. apparently other people have this issue too. What about FileRef?
  • Kit Menke
    Kit Menke over 12 years
    Unfortunately, looks like you don't have many other options. You probably will need to build the URL from the pieces you have.
  • Imir Hoxha
    Imir Hoxha over 12 years
    Hi @Kit Menke, I solved the problem by combining together the above fields. Thanks for your help.
  • Sebastian 506563
    Sebastian 506563 about 11 years
    but still u could share this solutions, i am looking for this same kind of solve