I am getting below error while creating a lookup field in SharePoint List (Office 365/SharePoint Online Environment) using CSOM:
"Value does not fall within the expected range."
Here is my code:
var strShowField = "Title";
var strSourceListName = "Divisions";
List lstSourceList = web.Lists.GetByTitle(strSourceListName);
clientContext.Load(lstSourceList);
clientContext.ExecuteQuery();
var lookupFieldXml = @"<Field Name=\"Division\" DisplayName=\"Division\" Type=\"Lookup\" Required=\"TRUE\" ShowField=\"Title\" List=\"Divisions\" />";
FieldCollection lstFields = list.Fields;
clientContext.Load(list);
clientContext.Load(lstFields);
clientContext.ExecuteQuery();
Field lookupField = lstFields.AddFieldAsXml(lookupFieldXml, false, AddFieldOptions.DefaultValue);
lookupField.Update();
clientContext.Load(lookupField);
clientContext.ExecuteQuery();
I have tried different solutions but not succeeded at all. Please suggest me some solutions. Thanks in advance..!