5

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

  • can you try with (lokupfieldxml,false,AddFieldOptions.AddToAllContentTypes); instead of defaultvalue? – userAZLogicApps Feb 16 '16 at 10:25
7
"<Field Name=\"Division\" DisplayName=\"Division\" Type=\"Lookup\" 
       Required=\"TRUE\" ShowField=\"Title\" List=\"Divisions\" />";

In the List attribute expected value is a GUID. Enter GUID id there and try.

"<Field Name=\"Division\" DisplayName=\"Division\" Type=\"Lookup\" 
           Required=\"TRUE\" ShowField=\"Title\" List=\"{Guid of the devision list}\" />";
  • Thank you so much Atish. In my case, this was the only issue. I updated it and it worked perfect for me. Thanks a lot. – Hemant Kabra Feb 16 '16 at 11:32
0

Updating Lookup fields via the Client Object Model appears to be limited by the View Lookup Threshold setting in the Resource Throttling settings for your web app (central administration). If your limit is 8, you will be able to work with 8 fields only, but 9th will fail with the Value does not fall within the expected range. error. This is likely due to the ListItem "reloading" the fields once the update has been done (refreshing itself) and hitting the limit.

So check your number of lookup fields in your list or site collection is more than 8. If its more than 8, go to the resource throttling section of your web app and increase the limit to 15 or 18.

  • Thanks for your support. I tried AddFieldOptions.AddToAllContentTypes , but it's still not working for me. As I am using Office 365, so I am not able to change it's threshold setting for lookup columns. Can you suggest me any possible way, which can help me to update this value for Office 365? – Hemant Kabra Feb 16 '16 at 10:56
  • In my list, I have only one lookup column, so I believe that this is not the case of threshold limit but still I would like to try this. Please suggest me how we can change this for Office 365/SharePoint Online environment, if is there any possible ways. – Hemant Kabra Feb 16 '16 at 10:57
  • community.office365.com/en-us/f/148/t/79787 as per this link, it seems that we can not change List View Lookup threshold. :-( Anything else that I can try? – Hemant Kabra Feb 16 '16 at 11:14

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.