Friday, March 2, 2012

Lesson 11: Basic of Caml Query


using System;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebControls;
using System.ComponentModel;
using System.Xml.Serialization;

namespace ProBasicOfCAML
{
    
    [DefaultProperty("Text"),
        ToolboxData("<{0}:CAML runat=server></{0}:CAML>"),
        XmlRoot(Namespace = "ProBasicOfCAML")]
    public class CAML : WebPart
    {
        Literal literalError = null;
        protected override void CreateChildControls()
        {
            literalError = new Literal();
          
            this.Controls.Add(literalError);
            GridView gridView = new GridView();
            this.Controls.Add(gridView);
            try
            {
                //ShowAllItems(gridView);
                //ShowSomeFields(gridView);
                //ShowSomeItems(gridView);
                //ShowSomeItemsOrderBy(gridView);
                ShowSomeItemsGroupBy(gridView);
            }
            catch (Exception ex)
            {
               
                literalError.Text = ex.Message;
             
                
            }
           
        }
        void ShowAllItems(GridView gridView)
        {
            literalError.Text = "ShowAllItems";
            SPSite spSite = SPControl.GetContextSite(Context);
            using (SPWeb spWeb = spSite.OpenWeb())
            {
                SPList spList = spWeb.Lists["Tasks"];
                SPQuery spQuery = new SPQuery();
                SPListItemCollection collection = spList.GetItems(spQuery);
                gridView.DataSource = collection.GetDataTable();
                gridView.DataBind();
            }
        }
        void ShowSomeFields(GridView gridView)
        {
            literalError.Text = "ShowSomeFields";
            SPSite spSite = SPControl.GetContextSite(Context);
            using (SPWeb spWeb = spSite.OpenWeb())
            {
                SPList spList = spWeb.Lists["Tasks"];
                SPQuery spQuery = new SPQuery();
                spQuery.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
                SPListItemCollection collection = spList.GetItems(spQuery);
                gridView.DataSource = collection.GetDataTable();
                gridView.DataBind();
            }
        }
        void ShowSomeItems(GridView gridView)
        {
            literalError.Text = "ShowSomeItems";
            SPSite spSite = SPControl.GetContextSite(Context);
            using (SPWeb spWeb = spSite.OpenWeb())
            {
                SPList spList = spWeb.Lists["Tasks"];
                SPQuery spQuery = new SPQuery();
                spQuery.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
                spQuery.Query = "<Where><Eq><FieldRef Name='Status'/><Value Type='Text'>Completed</Value></Eq></Where>";
                SPListItemCollection collection = spList.GetItems(spQuery);
                gridView.DataSource = collection.GetDataTable();
                gridView.DataBind();
            }
        }
        void ShowSomeItemsOrderBy(GridView gridView)
        {
            literalError.Text = "ShowSomeItemsOrderBy";
            SPSite spSite = SPControl.GetContextSite(Context);
            using (SPWeb spWeb = spSite.OpenWeb())
            {
                SPList spList = spWeb.Lists["Tasks"];
                SPQuery spQuery = new SPQuery();
                spQuery.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
                spQuery.Query = "<Where><Eq><FieldRef Name='Status'/><Value Type='Text'>Completed</Value></Eq></Where>";
                spQuery.Query += "<OrderBy><FieldRef Name='ID' Ascending=False/></OrderBy>";
                SPListItemCollection collection = spList.GetItems(spQuery);
                gridView.DataSource = collection.GetDataTable();
                gridView.DataBind();
            }
        }
        void ShowSomeItemsGroupBy(GridView gridView)
        {
            literalError.Text = "ShowSomeItemsGroupBy";
            SPSite spSite = SPControl.GetContextSite(Context);
            using (SPWeb spWeb = spSite.OpenWeb())
            {
                SPList spList = spWeb.Lists["Tasks"];
                SPQuery spQuery = new SPQuery();
                spQuery.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
               
                spQuery.Query = "<GroupBy><FieldRef Name='Status'/></GroupBy>";
                SPListItemCollection collection = spList.GetItems(spQuery);
                gridView.DataSource = collection.GetDataTable();
                gridView.DataBind();
            }
        }
        void ShowSomeItemsOnView(GridView gridView)
        {
            literalError.Text = "ShowSomeItemsOnView";
            SPSite spSite = SPControl.GetContextSite(Context);
            using (SPWeb spWeb = spSite.OpenWeb())
            {
                SPList spList = spWeb.Lists["Tasks"];
                SPQuery spQuery = new SPQuery();
                spQuery.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
                spQuery.Query = "<Where><Eq><FieldRef Name='Status'/><Value Type='Text'>Completed</Value></Eq></Where>";
                SPListItemCollection collection = spList.GetItems(spQuery, "AllItems");
                gridView.DataSource = collection.GetDataTable();
                gridView.DataBind();
            }
        }
    }
}

0 comments:

Post a Comment