Monday 25 March 2013

Use of caml query with datetime


 string[] dates;
      try
      {
          SPSecurity.RunWithElevatedPrivileges(delegate
          {

              using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
              {
                  using (SPWeb oWeb = oSite.OpenWeb())
                  {
                     
                      SPList list = SPContext.Current.Web.Lists["General Tasks"];
                      CustomListViewByQuery.List = list;
                      string query = null;
                      SPQuery qry = new SPQuery(list.DefaultView);
                      int iCnt = 0;
                      // Prepare start and end dates
                       DateTime startDate = new DateTime();
                       DateTime endDate = new DateTime();
                       if (ddlReports.SelectedValue == "Monthly")
                       {                        

                           dates = this.GetRange(FrequencyType.Monthly, DateTime.Today);
                           startDate = Convert.ToDateTime(dates[0]);
                           endDate= Convert.ToDateTime(dates[1]);
                           //startDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                           //endDate = startDate.AddMonths(1).AddMinutes(-1);
                       }
                     
                       else if (ddlReports.SelectedValue == "Weekly")
                       {
                           dates = this.GetRange(FrequencyType.Weekly, DateTime.Today);
                           startDate = Convert.ToDateTime(dates[0]);
                           endDate = Convert.ToDateTime(dates[1]);
                       }
                   
                      Response.Write(startDate.ToString());
                       Response.Write(endDate.ToString());

                      // Format dates
                      string startDateFx = startDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
                      string endDatFx = endDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
                       Response.Write(startDateFx);
                       Response.Write(endDatFx);

                      try
                      {
//                         query = @" <Where>    
//         <Eq>
//            <FieldRef Name='Priority' />
//            <Value Type='Choice'>(1) High</Value>
//         </Eq>            
//   </Where>";
                          query = @"<Where><And><Leq><FieldRef Name='DueDate' /><Value Type='DateTime'   IncludeTimeValue='FALSE'>"+endDatFx+"</Value></Leq><Geq><FieldRef Name='DueDate' /><Value Type='DateTime'  IncludeTimeValue='FALSE'>"+startDateFx+"</Value> </Geq></And></Where>";

                          //query = @"<View>" +
                          //              "<Query>" +
                          //                  "<Where>" +
                          //                  "<And>"+
                          //                       "<Eq>"+
                          //                          "<FieldRef Name='Priority' />"+
                          //                          "<Value Type='Choice'>(1) High</Value>"+
                          //                       "</Eq>"+
                          //                       "<Eq>"+
                          //                              "<And>" +
                          //                                  "<Geq>" +
                          //                                      "<FieldRef Name='DueDate'/>" +
                          //                                      "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + startDateFx + "</Value>" +
                          //                                  "</Geq>" +
                          //                                  "<Leq>" +
                          //                                      "<FieldRef Name='DueDate'/>" +
                          //                                      "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + endDatFx + "</Value>" +
                          //                                  "</Leq>" +
                          //                              "</And>" +
                          //                      "</Eq>"+
                          //                  "</And>"+                                                      
                          //                  "</Where>" +
                          //              "</Query>" +
                          //       "</View>";
                          qry.Query = query;
                          CustomListViewByQuery.Query = qry;
                          CustomListViewByQuery.List = list;
                          CustomListViewByQuery.DisableFilter = true;
                          CustomListViewByQuery.DisableSort = true;
                          iCnt++;

                          if (iCnt > 0)
                          {

                              CustomListViewByQuery.Visible = true;

                          }
                      }
                      catch (Exception ex)
                      {
                      }
                  }
              }
          });
      }
      catch { }
   

No comments:

Post a Comment