hossein hashemian
hossein hashemian

Reputation: 311

Why FieldLookupValue of an item can not be initialized in csom?

I am going to initialize items of a sharepoint list. One of field is a lookup one, but when I'm going to initialize it, no value is set to it.

Here is my code:

var clientContext =
   new ClientContext(aURL)
   {
       Credentials = new System.Net.NetworkCredential(somestring)
   };
Web oWebsite = clientContext.Web;
List teachersList = oWebsite.Lists.GetByTitle("Teachers");
FieldLookupValue lookupField = new FieldLookupValue();
lookupField.LookupId = anInteger;
teacherInfoListItem["ProfessorID"] = lookupField;
teacherInfoListItem["Title"] = value;
teacherInfoListItem["LastName"] = value;
teacherInfoListItem.Update();
clientContext.ExecuteQuery();

Upvotes: 1

Views: 568

Answers (1)

Lee
Lee

Reputation: 5493

Your code logic should be fine, make sure the anInteger item exists in your lookup list.

My tested code.

 using(var clientContext =new ClientContext("http://sp"))
            {
                var web = clientContext .Web;
                var oList = web.Lists.GetByTitle("TestDetails");

                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem oListItem = oList.AddItem(itemCreateInfo);
                FieldLookupValue lookupField = new FieldLookupValue();
                lookupField.LookupId = 1;
                oListItem["Title"] = "My New Item!";
                oListItem["Name"] = lookupField;
                oListItem.Update();
                clientContext.ExecuteQuery();

                Console.WriteLine("complete");
            }

Upvotes: 1

Related Questions