3

I am trying to copy data from one list to other list (both lists are on different sites) along with lookup columns. But, I am getting an error for lookup field as:

Value does not fall within the expected range

Code works and data gets copied for other non-lookup fields. I tried every possible way including increasing List View Lookup Threshold and all possible ways of code but still error persists at ExecuteQuery().

Below is my code for lookup field:

if (field is FieldLookup && field.InternalName == "Country")
{
    var CountryLookup = (item.FieldValues["Country"] as FieldLookupValue).LookupValue.ToString();
    var CountryLookupId = (item.FieldValues["Country"] as FieldLookupValue).LookupId.ToString();
    FieldLookupValue flvRDS = new FieldLookupValue();
    flvRDS.LookupId = int.Parse(CountryLookupId);
    itemToCreate["Country"] = flvRDS;
    itemToCreate.Update();
    destContext.ExecuteQuery();
}

Help is really appreciated.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Pratik
  • 31
  • 1
  • 4

1 Answers1

0

I assume item is the new ListItem you're trying to create on your target list.

But you're never in fact reading any value from field here! So basically, you're trying to set your new FieldLookup.LookupId with the item["Country"].LookupId, which should logically be empty at this moment.

Here's a method I use to retrieve a lookup field ListItem from a value, feel free to modify it to fit your need, since I don't know how you want to retrieve it (SPList is an alias for Microsoft.SharePoint.Client.List).

private ListItem GetLookupItem(FieldLookup lookupField, string lookupValue)
{
    string mappingField = lookupField.LookupField;

    Microsoft.SharePoint.Client.List lookupList = Context.Web.Lists.GetById(new Guid(lookupField.LookupList));

    Context.Load(lookupList);
    Context.ExecuteQuery();

    ListItemCollection libListItems = lookupList.GetItems(CamlQuery.CreateAllItemsQuery());
    Context.Load(libListItems, items => items.Include(
        itemlookup => itemlookup.Id,
        itemlookup => itemlookup[mappingField]));
    Context.ExecuteQuery();

    foreach (ListItem mappedItem in libListItems)
    {
        object mappedField = mappedItem[mappingField];
        if (mappedField != null && mappedField.ToString().Equals(lookupValue))
            return mappedItem;
    }

    return null;
}

Now that you have the corresponding ListItem, you can set your item.LookupId with its Id:

if (field is FieldLookup && field.InternalName == "Country")
{
    FieldLookupValue flvRDS = new FieldLookupValue();
    flvRDS.LookupId = GetLookupItem(field as FieldLookup, "France").Id; // here, dunno how you get your country's name
    itemToCreate["Country"] = flvRDS;
    itemToCreate.Update();
    destContext.ExecuteQuery();
}

Feel free to add some more previous code if you want an answer more suited for your specific issue.

Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • Actually item is the existing data in the list item from where I am reading and itemToCreate is the new item where I need to copy it. Also I am getting country name & Id in CountryLookup & CountryLookupId respectively. Just the thing is they are not getting copied. – Pratik Feb 28 '17 at 08:54
  • Try retrieving the ID of the looked up item instead of the LookupId, as I did in my code. – Kilazur Mar 01 '17 at 08:11