#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