Wednesday, June 16, 2010

Add distinct values in checkbox list from sharepoint list

  • Add distinct values checkbox list from sharepoint list

    Problem :-• I had created a list having a all employes names and other details and related entries.
    • I had developed a webpart having One checkboxlist and OK button.
    • In Checkbox list if I can select one or more employes and click on OK button. The List will get filtered and shws the data for selected employes.
    • On refreshing page I am inserting all employes names in checkboxlist. But list contains same employe name multiple times so the checkbox list also filed by same employes multiple times.
    • So I write the code to insert distinct values in checkbox from sharepoint list.

    Multiple names in List


    Distinct Names in Checkboxlist


    Code:-
//First Get all Employee names from List

if (collListItems.Count > 0)
{
//veraible for empname from list
string str_EmpName = string.Empty;
//clear checkboxlist
chk_Apply_all.Items.Clear();
//Add All option in checkboxlist
chk_Apply_all.Items.Add("All");
//veriable to compair values
string strEmpName1 = string.Empty;
string strEmpName2 = string.Empty;


foreach (SPItem item in collListItems)
{
//get value from list
SPFieldLookupValue lookupEmployee = new SPFieldLookupValue(item["EmployeeAccount"].ToString());
str_EmpName = lookupEmployee.LookupValue.ToString();
strEmpName1 = str_EmpName;

//first value check
if (str_EmpName == strEmpName1 && strEmpName2 =="")
{
chk_Apply_all.Items.Add(str_EmpName);
strEmpName2 = str_EmpName;
}
else
{
//value check loop from second value
if(strEmpName1==strEmpName2)
{
//do nothing
}
else
{
chk_Apply_all.Items.Add(str_EmpName);
strEmpName2 = str_EmpName;
}
}
}
}

No comments:

Post a Comment