Imir Hoxha
Imir Hoxha

Reputation: 1694

Get the URL of a List in sharepoint using Caml Query

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

Upvotes: 2

Views: 10584

Answers (2)

Imir Hoxha
Imir Hoxha

Reputation: 1694

I did it like this:

 row["AbsolutePath"] = String.Format("{0}{1}", row["EncodedAbsUrl"], row["FileRef"].ToString().Substring(row["FileRef"].ToString().IndexOf("#") + 1));

Upvotes: 1

Kit Menke
Kit Menke

Reputation: 7056

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\"/>";

Upvotes: 4

Related Questions