博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sharepoint中查询列表绑定gridview
阅读量:4354 次
发布时间:2019-06-07

本文共 3729 字,大约阅读时间需要 12 分钟。

#region 数据源绑定

        /// <summary>
        /// 数据源绑定
        /// </summary>
        /// <param name="strQuery"></param>
        private void BindData(string strQuery)
        {
            using (SPSite site = new SPSite(Comment.webURL))     //http://moss:8000为你自己对应的网站集名称 
            {
                foreach (SPWeb web in site.AllWebs)
                {
                    SPList list = web.Lists["任务"];
                    SPQuery query = new SPQuery();
                    query.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='Title' /> <FieldRef  Name='DocIcon' />    <FieldRef      Name='LinkTitle' />    <FieldRef      Name='AssignedTo' />    <FieldRef      Name='Status' />    <FieldRef      Name='Priority' />    <FieldRef      Name='DueDate' />";//列表的字段名称
                    query.Query = strQuery;
                    this.GridViewList.DataSource = list.GetItems(query).GetDataTable();
                    this.GridViewList.DataBind();
                }
            }
        }
        #endregion

//是得到查询条件

效果:

方法一:

private string GetWhere()

        {
            //预定结束时间段查询
            string strQuery = " ";
            string stringBookingBegin = this.txtBookingTimeB.Text.Trim();
            string txtBookingTimeE = this.txtBookingTimeE.Text.Trim();
            if (stringBookingBegin.Length > 0 && txtBookingTimeE.Length <= 0)
            {
                strQuery += "<Leq><FieldRef Name='BookingEndTime' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + stringBookingBegin + "</Value><Leq>";
            }
            if (stringBookingBegin.Length <= 0 && txtBookingTimeE.Length > 0)
            {
                strQuery += "<Geq><FieldRef Name='BookingEndTime' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + txtBookingTimeE + "</Value></Geq>";
            }
            if (this.txtBookingTimeE.Text.Trim().Length > 0 && this.txtBookingTimeB.Text.Trim().Length > 0)
            {
                strQuery += string.Format(@"<And><Geq><FieldRef Name='BookingEndTime'/><Value Type='DateTime'>{0}</Value></Geq>
<Leq><FieldRef Name='BookingEndTime'/><Value Type='DateTime' >{1}</Value></Leq></And>", stringBookingBegin, txtBookingTimeE);
            }
                   if (this.ddlMRName_Query.SelectedIndex > 0)
            {
                strQuery += "<Eq><FieldRef Name='Title' /><Value Type='Text'>" + this.ddlMRName_Query.SelectedItem.Text.Trim() + "</Value></Eq>";
            }
                 if (!string.IsNullOrEmpty(strQuery.Trim()))
            {
                strQuery = "<Where><And> " + strQuery + "  <Eq><FieldRef Name='State' /><Value Type='Text'>有效</Value></Eq></And></Where>";
            }
            else
            {
                strQuery = "<Where><Eq><FieldRef Name='State' /><Value Type='Text'>有效</Value></Eq></Where>";
            }
            return strQuery;
        }

方法二:linq to sharepoint

#region 查询统计

        /// <summary>
        /// 查询统计按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            MRBModelDataContext a = new MRBModelDataContext(Comment.webURL);
            EntityList<MeetingRoomBookingItem> someList = a.GetList<MeetingRoomBookingItem>("列表名称");//.Where<a.MeetingRoomBooking>(getcon())
            var MRBLists = from m in someList
                           where GetCondition(m)
                           group m by new { m.Title, m.Region, m.BookingPerson } into demGrp
                           select
                                new
                                {
                                    MRBName = demGrp.Key.Title,
                                    BooingPerson = demGrp.Key.BookingPerson,
                                    BookingCount = demGrp.Count(),
                                    info = demGrp
                                };
            GridView1.DataSource = MRBLists;
            GridView1.DataBind();

        }

        #endregion

  #region 查询条件

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="mrl"></param>
        /// <returns></returns>
        private bool GetCondition(MeetingRoomBookingItem mrl)
        {
            //预定开始时间段查询
            bool boolResult = true;

            string stringBookingBegin = this.txtBookingTimeB.Text.Trim();

            string txtBookingTimeE = this.txtBookingTimeE.Text.Trim();
            if (stringBookingBegin.Length > 0)
            {
                boolResult &= mrl.BookingBeginTime >= Convert.ToDateTime(stringBookingBegin);
            }

            if (txtBookingTimeE.Length > 0)

            {
                boolResult &= mrl.BookingBeginTime <= Convert.ToDateTime(txtBookingTimeE);
            }
            if (this.ddlMRName_Query.SelectedIndex > 0)
            {
                boolResult &= mrl.Title.Equals(this.ddlMRName_Query.SelectedItem.Text.Trim());
            }            if (this.ddlRegion.SelectedIndex > 0)
            {
                boolResult &= mrl.Region.Equals(this.ddlRegion.SelectedValue.Trim());
            }
            return boolResult;
        }
        #endregion

转载于:https://www.cnblogs.com/Bany/archive/2013/03/19/2968549.html

你可能感兴趣的文章
hdu2191 多重背包
查看>>
libevent文档学习(一)多线程接口和使用
查看>>
【补hackbar的坑】关于hackbar需要钱的补救措施
查看>>
纤程与Quasar
查看>>
MySQL的一个麻烦事
查看>>
Uri、URL和URN三者的区别
查看>>
数据字典的转换
查看>>
导入模块的搜索路径以及sys.path
查看>>
二维数组按照指定的字段排序的函数
查看>>
08CMS Variable Override Write Arbitrarily WEBSHELL Into Arbitrarily Path
查看>>
Netty服务端启动
查看>>
在IAR下通过Jlink将程序直接下载到Flash指定地址
查看>>
POJ2560-雀斑(Freckles)【图论,并查集,最小生成树,KURUSKAL】
查看>>
[Angular] Tree shakable provider
查看>>
[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript
查看>>
[Angular 2] Select From Multiple Nested Angular 2 Elements
查看>>
C# 中的委托和事件[转帖]
查看>>
图的遍历(bfs+dfs)模板
查看>>
angular service 进行组件通信
查看>>
2016.6.11
查看>>