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.

Thursday, January 03, 2013

Insert Search Update using Multi Lookup Column and Checkbox List in SharePoint



Insert Search Update using Multi Lookup Column and Checkbox List in SharePoint
WebPart

Backend List

Note:-
  • Sample Category is the Parent List.
  • Multi lookup is the child list which is using the lookup from Sample Category List.
  • SampleCategory is the lookup column name in the child list.
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="TestMLookUpUserControl.ascx.cs" Inherits="TestMLookUp.TestMLookUp.TestMLookUpUserControl" %>
<table align="center">
<tr>
<td valign="top">Enter Title</td>
<td><asp:TextBox ID="txtTitle" runat="server" /></td>
</tr>
<tr>
<td valign="top">Select Category</td>
<td><asp:CheckBoxList runat="server" ID="chkCategory" DataValueField="ID" DataTextField="Title" RepeatColumns="3" /></td>
</tr>
<tr><td colspan="2" align="center"><asp:Button ID="btnSave" runat="server"
        Text="Save" onclick="btnSave_Click" />&nbsp;<asp:Button ID="btnSearch" Text="Search"
        runat="server" onclick="btnSearch_Click" />&nbsp;<asp:Button ID="btnUpdate"
        runat="server" Text="Update" onclick="btnUpdate_Click" /> </td></tr>
<tr><td colspan="2" align="center"><asp:Label ID="lblMessage" runat="server" /></td></tr>
</table>

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;

namespace TestMLookUp.TestMLookUp
{
    public partial class TestMLookUpUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCategory();
            }
        }

        public void BindCategory()
        {
            try
            {
                 SPWeb currentWeb = SPContext.Current.Web;
                 SPList lst = currentWeb.Lists["Sample Category"];
                 SPQuery sQuery = new SPQuery();
                 sQuery.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
                 SPListItemCollection myColl = lst.GetItems(sQuery);
                 if (myColl.Count > 0)
                 {
                    chkCategory.DataSource = myColl.GetDataTable();
                    chkCategory.DataBind();
                 }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Multi LookUp"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>"+txtTitle.Text.ToLower()+"</Value></Eq></Where>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                SPListItem item = myColl[0];
                SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(item["SampleCategory"].ToString());
                chkCategory.ClearSelection();
                foreach (SPFieldLookupValue value in values)
                {
                    foreach (ListItem oItem in chkCategory.Items)
                    {
                        int i = int.Parse(oItem.Value.ToString());
                        int j = int.Parse(value.LookupId.ToString());
                        if (i == j)
                        {
                            oItem.Selected = true;
                        }
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Multi LookUp"];
                SPListItemCollection myColl = lst.Items;
                SPListItem item = myColl.Add();
                item["Title"] = txtTitle.Text;
                string strMultiLookUp = string.Empty;
                int i = 0;
                SPFieldLookupValueCollection itemValues = new SPFieldLookupValueCollection();
                foreach (ListItem oItem in chkCategory.Items)
                {
                    if (oItem.Selected)
                    {
                        itemValues.Add(new SPFieldLookupValue(int.Parse(oItem.Value), oItem.Text));
                    }
                }
                item["SampleCategory"] = itemValues;
                item.Update();
                txtTitle.Text = "";
                chkCategory.ClearSelection();
                lblMessage.Text = "Insertion Successful";
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                 SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Multi LookUp"];
                SPListItemCollection myColl = lst.Items;
                SPFieldLookupValueCollection itemValues = new SPFieldLookupValueCollection();
                foreach (SPListItem item in myColl)
                {
                    if (item["Title"].ToString() == txtTitle.Text)
                    {
                         foreach (ListItem oItem in chkCategory.Items)
                         {
                            if (oItem.Selected)
                            {
                                itemValues.Add(new SPFieldLookupValue(int.Parse(oItem.Value), oItem.Text));
                            }
                         }
                         item["SampleCategory"] = itemValues;
                         item.Update();
                    }
                }
                txtTitle.Text = "";
                chkCategory.ClearSelection();
                lblMessage.Text = "Updation Successful";
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
           
        }
    }
}

No comments:

Post a Comment