1

I have the following code which retrieves all items from list "Employee Info" and bind it to Dropdown list EmployeeDD.

  if (!Page.IsPostBack)
            {
                DataSet ds = new DataSet();
                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = mySite.OpenWeb();

                SPList list = myWeb.Lists["Employee Info"];
                DataTable DTable_List = list.Items.GetDataTable();
                DTable_List.TableName = "Table1";
                ds.Tables.Add(DTable_List);

                EmployeeDD.DataSource = ds.Tables["Table1"];
                EmployeeDD.DataTextField = "Title";
                EmployeeDD.DataValueField = "Title";

                EmployeeDD.DataBind();
                EmployeeDD.SelectedIndex = 0;
            }

it displays all the Employees in a list.Now how do I filter the Employees based on Current logged in user and just show that user in dropdown list?

2 Answers 2

5

To query any person or group field against logged in user you can use **<UserID/>** tag in Value.

if (!Page.IsPostBack)
                {
                    using (SPSite mySite = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb myWeb = mySite.OpenWeb())
                        {
                            SPList list = myWeb.Lists.TryGetList("Employee Info");
                            if (list != null)
                            {
                                SPQuery query = new SPQuery();
                                query.Query = @"<Where><Eq><FieldRef Name='FieldToCompare' LookupId='TRUE'/><Value Type='Integer' ><UserID/></Value></Eq></Where>";
                                query.ViewFields = "<FieldRef Name='Title'/>";
                                query.ViewFieldsOnly = true;
                                SPListItemCollection itemColl = list.GetItems(query);
                                if (itemColl != null)
                                {
                                    EmployeeDD.DataSource = itemColl;
                                    EmployeeDD.DataBind();
                                }
                            }
                        }
                    }
                }
0

Here I am going to fill DropdownList 'ddEmp' with all the values present in the List 'EmpInfo' where userID is equal to current userId.

    if(!Page.IsPostBack)

    {

    using(SPSite siteColl= new SPSite("http://hjkdh.com"));

      {
        using(SPWeb web1=siteColl.OpenWeb());

         {


             List mylist= mywb.List("EmployeeInfo");

             ListItemCollection itemslist=new ListItemCollection();

             CamlQuery query=new CamlQuery();
             query="";

             itemslist=mylist.GetItems(query);

             ddEmp.DataSource=itemsList;
             ddEmp.DataBind();

          }

      }


    }  

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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