码迷,mamicode.com
首页 > Web开发 > 详细

将sharepoint中的跨网站、列表查询的结果用SPGridView分页显示

时间:2015-03-08 16:58:29      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

将sharepoint中的跨网站、列表查询的结果用SPGridView分页显示

2008-12-30 10:14 by Virus-BeautyCode, 1200 阅读, 0 评论, 收藏编辑

我是用户控件(也就是ascx控件)写的查询界面和显示结果,然后用QuickPart包装了一下,这样做的好处就是复杂界面可以使用拖动控件来开发,要比写代码来的直观,不足之处就是调试困难,但是昨天我的同事发现了一个调试的好办法,很不错,下一篇我会写出来。

需求是开发一个公司动态发布系统,我将未发布动态,已发布动态和已过期动态分三个列表存放,但是普通用户查询的时候需要查询的结果是从已发布动态和已过期动态两个列表库中来的数据。

下面是ascx界面的示例代码

技术分享
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CorpTrendQuery3.ascx.cs" Inherits="CorpTrendQuery3" %>
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1" %>
    
<div><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 
        style="width: 56px" />
<br /></div>
<div>

<br />
<cc1:SPGridView ID="SPGridView1" runat="server" AutoGenerateColumns="false">
</cc1:SPGridView>
<asp:LinkButton ID="LinkButton_Pre" runat="server" 
    onclick="LinkButton_Pre_Click">上一页</asp:LinkButton><asp:LinkButton ID="LinkButton_Next" runat="server" 
    onclick="LinkButton_Next_Click">下一页</asp:LinkButton>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>

技术分享


下面是这个ascx控件的后台cs代码

技术分享
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

public partial class CorpTrendQuery3 : System.Web.UI.UserControl
{
    private string strQuery = null;

    private SPSite site = null;
    public SPSite SP_Site
    {
        get
        {
            if (site == null)
                site = new SPSite("http://virus/sites/intranet");
            return site;
        }
    }
    private SPWeb web = null;
    public SPWeb SP_Web
    {
        get
        {
            if (web == null)
            {
                web = SP_Site.OpenWeb("team");

            }
            return web;
        }

    }

    private PagedDataSource pds = new PagedDataSource();
    private DataTable dt;

    private int CurrentPage
    {
        get
        {
            if (this.ViewState["CurrentPage"] == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(this.ViewState["CurrentPage"].ToString());
            }
        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }
    }
    /// <summary>
    /// 添加一个查询节点
    /// </summary>
    /// <param name="strQuery">整个查询的where节点字符串</param>
    /// <param name="nodeName">要添加的查询节点的名称</param>
    /// <param name="name">查询节点中的查询列的名称</param>
    /// <param name="type">查询列的类型</param>
    /// <param name="value">查询节点中的查询列的值</param>
    /// <returns>整合之后的查询条件</returns>
    private string xmlAttributeUpdate(string strQuery, string nodeName, string name,string type, string value)
    {
        XmlDocument docQuery = new XmlDocument();
        docQuery.LoadXml(strQuery);
        StringWriter sw = new StringWriter();
技术分享
技术分享
        XmlNode andNode = docQuery.SelectSingleNode("/Where/And");
        if (andNode == null)
        {
            //不存在And节点,说明原来是一个查询条件,
            //要添加一个条件的话,就要先添加and节点,然后用and节点将以前的条件和现在的条件包起来
            XmlNode whereChildNode = docQuery.DocumentElement.ChildNodes[0];
技术分享
技术分享
            docQuery.DocumentElement.RemoveAll();
            //添加and节点
            XmlElement xe = docQuery.CreateElement("And");
            XmlNode whereNode = docQuery.DocumentElement;
            whereNode.AppendChild(xe);
            //添加原来的查询节点
            XmlNode and1Node = docQuery.SelectSingleNode("/Where/And");
            and1Node.AppendChild(whereChildNode);
            //添加新查询节点
            XmlElement conditionNode = docQuery.CreateElement(nodeName);
            and1Node.AppendChild(conditionNode);
技术分享
            XmlElement nameNode = docQuery.CreateElement("FieldRef");
            nameNode.SetAttribute("Name", name);
            conditionNode.AppendChild(nameNode);
技术分享
            XmlElement valueNode = docQuery.CreateElement("Value");
            valueNode.SetAttribute("Type", type);
            if (nodeName.Equals("Geq") || nodeName.Equals("Leq"))
                valueNode.InnerText = value + "Z";
            else
                valueNode.InnerText = value;
            conditionNode.AppendChild(valueNode);
技术分享
          
            return docQuery.DocumentElement.OuterXml;
        }
        else
        {
            //存在and节点,再用一个and节点将原来的and节点和新条件包起来
            docQuery.DocumentElement.RemoveAll();
技术分享
            //添加and节点
            XmlElement xe = docQuery.CreateElement("And");
            XmlNode whereNode = docQuery.DocumentElement;
            whereNode.AppendChild(xe);
            //添加原来的查询节点
            XmlNode and1Node = docQuery.SelectSingleNode("/Where/And");
            and1Node.AppendChild(andNode);
            //添加新查询节点
            XmlElement conditionNode = docQuery.CreateElement(nodeName);
            and1Node.AppendChild(conditionNode);
技术分享
            XmlElement nameNode = docQuery.CreateElement("FieldRef");
            nameNode.SetAttribute("Name", name);
            conditionNode.AppendChild(nameNode);
技术分享
            XmlElement valueNode = docQuery.CreateElement("Value");
            valueNode.SetAttribute("Type", type);
            if (nodeName.Equals("Geq") || nodeName.Equals("Leq"))
                valueNode.InnerText = value + "Z";
            else
                valueNode.InnerText = value;
            conditionNode.AppendChild(valueNode);
            return docQuery.DocumentElement.OuterXml;
技术分享

        }
    }
技术分享
    private string getQueryString()
    {
        string corpTrendTitle;
        string corpTrendContent;
        DateTime corpTrendReleaseDT = new DateTime();
        DateTime corpTrendExpireDT = new DateTime();
技术分享
技术分享
        if (!string.IsNullOrEmpty(TextBox_CorpTrendTitle.Text))
        {
            if (strQuery == null)
            {
                strQuery = @"<Where>
                              <Contains>
                                 <FieldRef Name=‘Title‘ />
                                 <Value Type=‘Text‘>" + TextBox_CorpTrendTitle.Text.Trim() + @"</Value>
                              </Contains>
                           </Where>";
            }
            else
            {
                strQuery = xmlAttributeUpdate(strQuery, "Contains", "Title","Text", TextBox_CorpTrendTitle.Text.Trim());
技术分享
技术分享
            }
        }
        if (!string.IsNullOrEmpty(TextBox_CorpTrendContent.Text))
        {
            if (strQuery == null)
            {
                strQuery = string.Format(@"<Where>
                              <Contains>
                                 <FieldRef Name=‘{0}‘ />
                                 <Value Type=‘Text‘>" + TextBox_CorpTrendContent.Text.Trim() + @"</Value>
                              </Contains>
                           </Where>", SP_Web.Lists["未发布公告"].Fields["公告内容"].InternalName);
            }
            else
            {
                strQuery = xmlAttributeUpdate(strQuery, "Contains", "PublishingPageContent","Text", TextBox_CorpTrendContent.Text.Trim());
技术分享
技术分享
            }
        }
        if (!this.CorpTrendReleaseDTBegin.IsDateEmpty && !this.CorpTrendReleaseDTEnd.IsDateEmpty)
        {
            if (strQuery == null)
            {
                strQuery = string.Format(@"<Where>
                                              <And>
                                                 <Geq>
                                                    <FieldRef Name=‘{0}‘ />
                                                    <Value Type=‘DateTime‘>{1}Z</Value>
                                                 </Geq>
                                                 <Leq>
                                                    <FieldRef Name=‘{2}‘ />
                                                    <Value Type=‘DateTime‘>{3}Z</Value>
                                                 </Leq>
                                              </And>
                                           </Where>", SP_Web.Lists["未发布公告"].Fields["公告发布时间"].InternalName,
                                                CorpTrendReleaseDTBegin.SelectedDate.ToString(),
                                                SP_Web.Lists["未发布公告"].Fields["公告发布时间"].InternalName,
                                                CorpTrendReleaseDTEnd.SelectedDate.ToString());
            }
            else
            {
                strQuery = xmlAttributeUpdate(strQuery, "Geq", SP_Web.Lists["未发布公告"].Fields["公告发布时间"].InternalName,"Text", CorpTrendReleaseDTBegin.SelectedDate.ToString());
                strQuery = xmlAttributeUpdate(strQuery, "Leq", SP_Web.Lists["未发布公告"].Fields["公告发布时间"].InternalName, "Text", CorpTrendReleaseDTEnd.SelectedDate.ToString());
技术分享
技术分享
            }
        }
        if (!this.CorpTrendExpireDTBegin.IsDateEmpty && !this.CorpTrendExpireDTEnd.IsDateEmpty)
        {
            if (strQuery == null)
            {
                strQuery = string.Format(@"<Where>
                                              <And>
                                                 <Geq>
                                                    <FieldRef Name=‘{0}‘ />
                                                    <Value Type=‘DateTime‘>{1}Z</Value>
                                                 </Geq>
                                                 <Leq>
                                                    <FieldRef Name=‘{2}‘ />
                                                    <Value Type=‘DateTime‘>{3}Z</Value>
                                                 </Leq>
                                              </And>
                                           </Where>", SP_Web.Lists["未发布公告"].Fields["公告过期时间"].InternalName,
                                                CorpTrendExpireDTBegin.SelectedDate.ToString(),
                                                SP_Web.Lists["未发布公告"].Fields["公告过期时间"].InternalName,
                                                CorpTrendExpireDTEnd.SelectedDate.ToString());
            }
            else
            {
                strQuery = xmlAttributeUpdate(strQuery, "Geq", SP_Web.Lists["未发布公告"].Fields["公告过期时间"].InternalName, "DateTime", CorpTrendExpireDTBegin.SelectedDate.ToString());
                strQuery = xmlAttributeUpdate(strQuery, "Leq", SP_Web.Lists["未发布公告"].Fields["公告过期时间"].InternalName, "DateTime", CorpTrendExpireDTEnd.SelectedDate.ToString());
技术分享
技术分享
            }
        }
       
        return strQuery;
     
    }

    private void doPaging(string strQuery)
    {

        SPSiteDataQuery query = new SPSiteDataQuery();
        query.Webs = "<Webs Scope=\"Recursive\" ></Webs>";
        query.Lists = string.Format("<Lists><List ID=‘{0}‘ /><List ID=‘{1}‘ /></Lists>", SP_Web.Lists["已过期动态"].ID, SP_Web.Lists["已发布动态"].ID);
       
        query.ViewFields = string.Format("<FieldRef Name=‘Title‘ /><FieldRef Name=‘FileRef‘ /><FieldRef Name=‘EncodedAbsUrl‘ /><FieldRef Name=‘{0}‘ /><FieldRef Name=‘{1}‘ />",
            SP_Web.Lists["已过期动态"].Fields["动态发布时间"].InternalName, SP_Web.Lists["已过期动态"].Fields["动态过期时间"].InternalName);

        if (strQuery == null)
            query.Query = string.Empty;
        else
            query.Query = strQuery;

        dt = web.GetSiteData(query);

        pds = new PagedDataSource();
        pds.DataSource = dt.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 4;

        Label1.Text = "总共:" + pds.PageCount.ToString() + "页";
        
        pds.CurrentPageIndex = CurrentPage;

        LinkButton_Next.Enabled = !pds.IsLastPage;
        LinkButton_Pre.Enabled = !pds.IsFirstPage;

        initSPGridView();

        SPGridView1.DataSource = pds;
        SPGridView1.DataBind();

    }
    public CorpTrendQuery3()
    {
    }
    private void initSPGridView()
    {
        SPGridView1.Columns.Clear();

        HyperLinkField linkTitle = new HyperLinkField();
        linkTitle.DataTextField = "Title";
        linkTitle.HeaderText = "主题";
        linkTitle.DataNavigateUrlFields = new string[] { "FileRef" };
        linkTitle.DataNavigateUrlFormatString = "http://virus/{0}";
        SPGridView1.Columns.Add(linkTitle);

        SPBoundField col = new SPBoundField();
        col.DataField = "Title";
        col.HeaderText = "Title";

        SPGridView1.Columns.Add(col);

        col = new SPBoundField();
        col.DataField = "_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_";
        col.HeaderText = "动态发布时间";
        SPGridView1.Columns.Add(col);

        col = new SPBoundField();
        col.DataField = "_x52a8__x6001__x8fc7__x671f__x65f6__x95f4_";
        col.HeaderText = "动态过期时间";
        SPGridView1.Columns.Add(col);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.TextBox_CorpTrendTitle.Text = string.Empty;
            //site = SPContext.Current.Site;
            //web = SPContext.Current.Web;
           doPaging(getQueryString());
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.ViewState["CurrentPage"] = null;
        //CurrentPage = 0;
        doPaging(getQueryString());

    }

    protected void LinkButton_Pre_Click(object sender, EventArgs e)
    {

        CurrentPage -= 1;
        doPaging(getQueryString());
    }
    protected void LinkButton_Next_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        doPaging(getQueryString());
    }
}
技术分享

将sharepoint中的跨网站、列表查询的结果用SPGridView分页显示

标签:

原文地址:http://www.cnblogs.com/ningang/p/4321832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!