4

I have an Employee list which has 2 columns(department and manager) which are lookup columns. I need code to update these lookup columns when a new user registers. (Though the other columns are being updated in the list)

The following is the code:

using (ClientContext clientContext = new ClientContext("https://sp13appstoredev.xyz.com/sites/DevApps/TrainingSite/"))
        {
            clientContext.Load(clientContext.Web, web => web.Title);
            List oList = clientContext.Web.Lists.GetByTitle("Employees");
            clientContext.Load(oList);
            ListItemCreationInformation itemInfo = new ListItemCreationInformation();
            Microsoft.SharePoint.Client.ListItem myItem = oList.AddItem(itemInfo);
            myItem["Title"] = txtFirstName.Text;
            myItem["Last_x0020_Name"] = txtLastName.Text;
            myItem["u5ib"] = txtAge.Text;
            myItem["Address"] = txtAddress.Text;
            //FieldLookupValue lookUpDepartment = new FieldLookupValue();
            //myItem["Department"] = lookUpDepartment as FieldLookupValue;
            //FieldLookupValue lookUpManager = new FieldLookupValue();
            //myItem["Manager"] = lookUpManager as FieldLookupValue;
            myItem["Gender"] = radioBtnGender.Text;
            myItem["Salary"] = txtSalary.Text;
            myItem.Update();
            clientContext.ExecuteQuery();
        }

I commented the lookup column lines to check if the other columns are being updated in the list. Kindly help. Thanks

2

The following should work:

using (ClientContext clientContext = new ClientContext("https://sp13appstoredev.xyz.com/sites/DevApps/TrainingSite/"))
{
    clientContext.Load(clientContext.Web, web => web.Title);
    List oList = clientContext.Web.Lists.GetByTitle("Employees");
    clientContext.Load(oList);
    ListItemCreationInformation itemInfo = new ListItemCreationInformation();
    Microsoft.SharePoint.Client.ListItem myItem = oList.AddItem(itemInfo);
    myItem["Title"] = txtFirstName.Text;
    myItem["Last_x0020_Name"] = txtLastName.Text;
    myItem["u5ib"] = txtAge.Text;
    myItem["Address"] = txtAddress.Text;
    var lookUpDepartment = new FieldLookupValue{LookupId = departmentId};
    myItem["Department"] = lookUpDepartment;
    var lookUpManager = new FieldLookupValue{LookupId = managerId};
    myItem["Manager"] = lookUpManager;
    myItem["Gender"] = radioBtnGender.Text;
    myItem["Salary"] = txtSalary.Text;
    myItem.Update();
    clientContext.ExecuteQuery();
}

Examples here and here.

|improve this answer|||||
  • The following line gives error because of the departmentId, I need to update the lookup column by value and not by ID. var lookUpDepartment = new FieldLookupValue{LookupId = departmentId}; @wjervis – Sanjana V Aug 7 '17 at 4:53
  • @SanjanaV You might be able to use LookupValue instead of LookupId, but I am not sure if that will work. – wjervis Aug 7 '17 at 12:43
1

The following code worked for me :

myItem["Department"] = 1;

myItem["Manager"] = 1;

I also had to change the parent list to ID (which was earlier Title)

|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.