Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, December 10, 2012

Dynamic SpQuery using SharePoint Object Model


Hi

Actually It's very difficult to make the SharePoint query Dynamic. By using SharePoint CAML Helper Dll you can acheive it faster. Dll required will be available within the below url.

http://sharepointcamlhelper.codeplex.com/



Procedure:-

  1. Copy and paste your dll with in your SharePoint site bin folder.
  2. Add Reference of the dll from the path you have copied.
  3. Write your logic & deploy the WebPart.
  4. Add the CAML Helper Name space within your SharePoint WebPart.
We have to declare like this as given below. Here is the code i have written.



Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DynamicSQUserControl.ascx.cs" Inherits="DynamicSQ.DynamicSQ.DynamicSQUserControl" %>
<div>
<span style="vertical-align:top;">Enter Fname</span>
<span><asp:TextBox ID="txtFName" runat="server"></asp:TextBox></span>
</div>
<div></div>
<div>
<span style="vertical-align:top;">Select City</span>
<span><asp:DropDownList ID="ddlCity" runat="server" DataValueField="ID" DataTextField="Title"></asp:DropDownList></span>
</div>
<div></div>
<div align="center">
<asp:Button ID="btnsearch" runat="server" Text="Search Record"
        onclick="btnsearch_Click" />
</div>
<div></div>
<div align="center">

    <asp:GridView ID="dgvSearchDynamicQuery" runat="server" AutoGenerateColumns="False"
        CellPadding="4" EnableModelValidation="True" ForeColor="#333333">
        <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:BoundField HeaderText="FName" DataField="Title" />
    <asp:BoundField HeaderText="City" DataField="City" />
    <asp:BoundField HeaderText="Telephone Number" DataField="Mobile" />
    </Columns>
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    </asp:GridView>

</div>
<div></div>
<div><asp:Label ID="lblMessage" runat="server"></asp:Label></div>

Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using SharePointStu.CAMLHelper;
using SharePointStu.CAMLHelper.CAML;

namespace DynamicSQ.DynamicSQ
{
    public partial class DynamicSQUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["City"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name='Title' /></OrderBy>";
                SPListItemCollection myColl = lst.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    ddlCity.DataSource = myColl.GetDataTable();
                    ddlCity.DataBind();
                }
                ddlCity.Items.Insert(0, new ListItem("Select City"));
            }
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            try
            {
                CAMLManager mgr = new CAMLManager();

                if (txtFName.Text != "")
                {
                    mgr.QueryGroups.Add(new QueryGroup("Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, txtFName.Text.ToLower()));
                }
                if (ddlCity.SelectedIndex != 0)
                {
                    mgr.QueryGroups.Add(new QueryGroup("City", Types.FieldTypes.Lookup, Types.QueryTypes.Eq, ddlCity.SelectedItem.Text));
                }

                string strQuery = mgr.GetCAML();
                //lblMessage.Text = strQuery;
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Telephone Numbers"];
                SPQuery sQuery=new SPQuery();
                sQuery.Query=strQuery;
                SPListItemCollection myColl = lst.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    dgvSearchDynamicQuery.DataSource = myColl.GetDataTable();
                    dgvSearchDynamicQuery.DataBind();
                }
            }
            catch (Exception ex)
            {

                lblMessage.Text = ex.ToString();
            }
        }
    }
}


No comments:

Post a Comment