使用Caml Query获取sharepoint中List的URL

时间:2012-01-06 10:56:13

标签: sharepoint sharepoint-2010

我正在使用SPSiteDataQuery显示来自不同列表的文档。我使用Gridview显示文档。 Gridview的其中一列是超链接字段。如何设置每个文档的URL,因为每个文档都来自不同的文档库?为了您的信息,我使用Caml查询来过滤文档。

请帮帮我。

这是代码:

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

2 个答案:

答案 0 :(得分:4)

您可以使用一些内部字段来构建指向文档的链接。以下是一些有用的示例,其中包含每个字段中的数据示例:

  • 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

您只需将上面的一个字段作为另一个视野添加到objSPSiteDataQuery.ViewFields

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

答案 1 :(得分:1)

我是这样做的:

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