0

I have two list called Employee and EmpInfo

columns for list Employee Name and Designation

columns for list EmployeeInfo Name and Address

So 2 list are linked with column called Name

I get information from first list as below

 using (var site = new SPSite(spWebUrl))
        {
            var web = site.RootWeb;
            var list = web.Lists.TryGetList("Employee");
            listItems = GetListItemsAsList(list.Items);
        }



 private List<SPListItem> GetListItemsAsList(SPListItemCollection liCol)
    {
        List<SPListItem> toReturn = new List<SPListItem>();
        List<SPListItem> childList = new List<SPListItem>();
        foreach (SPListItem li in liCol)
        {
            toReturn.Add(li);
        }
        return toReturn;
    }

So inside method GetListItemsAsList i want to access 2nd list EmployeeInfo and then add records to toReturn list

How to do this?

Update1 I found way to search records of one list in other list but it adds record from second to returning list. For example if Employee list has 5 records and EmployeeInfo has 2 records. Then if both record matches from EmployeeInfo then it should return only 5 records with field from second list appended.

My below code adds 2 records so total count is 7 which is wrong.

 private List<SPListItem> GetListItemsAsList(SPListItemCollection liCol)
    {
        List<SPListItem> toReturn = new List<SPListItem>();

        foreach (SPListItem li in liCol)
        {
            string spWebUrl = SPContext.Current.Web.Url;

            //Get the OrgChart list items
            using (var site = new SPSite(spWebUrl))
            {
                var web = site.RootWeb;
                var list = web.Lists.TryGetList("EmployeeInfo");
                SPQuery query = new SPQuery();
                query.Query = "<Where><Eq><FieldRef Name='Name'/>" + "<Value Type='Text'>" + li["Title"] + "</Value></Eq></Where>>";
                //query.Query = "<Where><And><Gt><FieldRef Name='ID'/>" + "<Value Type='Counter'>" + itemIndex + "</Value></Gt><Eq><FieldRef Name='Status'/>" + "<Value Type='Text'>Approved</Value></Eq></And></Where><OrderBy><FieldRef Name='ID' Ascending='TRUE'></FieldRef></OrderBy>";
                SPListItemCollection items1 = null;
                items1 = list.GetItems(query);
                foreach (SPListItem li1 in items1)
                {
                    toReturn.Add(li1);
                }

            }



            toReturn.Add(li);
        }
        return toReturn;
    }
1

Try the following:

private List<Employee> GetListItemsAsList(SPListItemCollection liCol)
{
    List<Employee> toReturn = new List<Employee>();
    Employee tempEmp;

    foreach (SPListItem li in liCol)
    {
        string spWebUrl = SPContext.Current.Web.Url;
        tempEmp = new Employee();
        tempEmp.Name = li["Title"].ToString();
        tempEmp.Designation = li["Designation"] != null ? li["Designation"].ToString() : string.Empty;

        //Get the OrgChart list items
        using (var site = new SPSite(spWebUrl))
        {
            var web = site.RootWeb;
            var list = web.Lists.TryGetList("EmployeeInfo");
            SPQuery query = new SPQuery();
            query.Query = "<Where><Eq><FieldRef Name='Name'/>" + "<Value Type='Text'>" + li["Title"] + "</Value></Eq></Where>";

            SPListItemCollection items1 = list.GetItems(query);
            foreach (SPListItem li1 in items1)
            {
                tempEmp.Address = li["Address"] != null ? li["Address"].ToString() : string.Empty;
            }

        }
        toReturn.Add(tempEmp);
    }

    return toReturn;
}

public class Employee
{
   public string Name {get; set;}
   public string Designation {get;set;}
   public string Address {get;set;}
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.