Tuesday, January 3, 2012

Checking Duplicate Values within a list using SpQuery.

using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Collections.Specialized;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Configuration;
[assembly: AllowPartiallyTrustedCallers]
namespace TestWebPart1
{
    [Guid("12084c4a-5bff-43e9-b89e-40bb11f2be86")]
    public class TestWebPart1 : System.Web.UI.WebControls.WebParts.WebPart
    {
        TextBox txtFname;
        Button btnSearchDuplicateValues;
        string strError = string.Empty;
        public TestWebPart1()
        {
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(strError);
            try
            {
                writer.Write("<Table>");
                writer.Write("<Tr>");
                writer.Write("<Td>");
                writer.Write("Enter Friend Name");
                writer.Write("</Td>");
                writer.Write("<Td>");
                txtFname.RenderControl(writer);
                writer.Write("</Td>");
                writer.Write("</Tr>");
                writer.Write("<Tr>");
                writer.Write("<Td colspan='2' Align='Center'>");
                btnSearchDuplicateValues.RenderControl(writer);
                writer.Write("</Td>");
                writer.Write("</Tr>");
                writer.Write("</Table>");
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
        protected override void CreateChildControls()
        {
            try
            {
                txtFname = new TextBox();
                txtFname.ID = "txtFname";
                this.Controls.Add(txtFname);
                btnSearchDuplicateValues = new Button();
                btnSearchDuplicateValues.ID = "btnSearchDuplicateValues";
                btnSearchDuplicateValues.Text = "Check Availability";
                btnSearchDuplicateValues.Click += new EventHandler(btnSearchDuplicateValues_Click);
                this.Controls.Add(btnSearchDuplicateValues);
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
        void btnSearchDuplicateValues_Click(object sender, EventArgs e)
        {
            try
            {
                SPWeb curentWeb = SPControl.GetContextWeb(Context);
                SPList lst = curentWeb.Lists["Telno"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + txtFname.Text.ToString().ToLower() + "</Value></Eq></Where>";
                SPListItemCollection myColl = lst.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    strError += "<Script Language='JavaScript' type='JavaScript'>";
                    strError += "alert('Friend Name Already Available');";
                    strError += "</Script>";
                }
                else
                {
                    strError += "<Script Language='JavaScript' type='JavaScript'>";
                    strError += "alert('Friend Name Not Available');";
                    strError += "</Script>";
                }
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
    }
}

No comments:

Post a Comment