Check the Duplicates Values in SharePoint List

Advertisement
How to Check the Duplicates Values in SharePoint List


<div>
    <label>EmployeeName</label>
    <asp:TextBox ID ="txtempname" runat ="server"></asp:TextBox>
    <asp:Label ID ="lblerror" runat ="server" Text ="Employee Name Already Available" Visible ="false" ForeColor ="Red"></asp:Label>
</div>
<div>
    <asp:Button ID="btnInsert" runat ="server"  Text ="Submit" OnClick="btnInsert_Click" />
</div>


  protected void btnInsert_Click(object sender, EventArgs e)
        {
            using(SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
               using( SPWeb web = site.OpenWeb())
                {
                   SPList list = web.Lists["EmployeeInfo"];
                   SPQuery checkduplicates = new SPQuery();

                   checkduplicates.Query = "<Where><Eq><FieldRef Name='Name' /><Value Type='Text'>" + txtempname.Text.ToString().ToLower() + "</Value></Eq></Where>";
                   SPListItemCollection myListColl = list.GetItems(checkduplicates);
                   if (myListColl.Count > 0)
                   {
                       lblerror.Visible = true;
                     
                   }
                   else
                   {

                       SPListItem item = list.Items.Add();
                       item["Name"] = txtempname.Text;
                       item.Update();
                   }

                }

            }

        }

OutPut:

SharePoint Online Real Time Training Contact: JLAKSHMITULASI@GMAIL.COM
Advertisements
SHARE

Lakshmi Tulasi Jasti

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a comment