Question

I have a ; separated string BusinessCategories. These values should be found as List Items in a custom list BusinessCategoryList, and I want to create a link to them in a multi value lookup field and save it as a field in a List Item in another list Companies.

Let's say I have this scenario: Cat1;Cat2 (Cat1 has ID = 1, Cat2 has ID = 2)

When debugging I found out that the Multi lookup field's ID at position 0 got updated with 1 on first assignment in the first iteration. The Multi lookup field's ID at position 0 and 1 both got wrongly updated with the value 2 on second assignment in the second iteration.

This is my code:

busCat = null;
FieldLookupValue relatedBC = new FieldLookupValue();
FieldLookupValue[] multiBC = null;


if (businessCategory != "")
{
    var bcarr = businessCategory.Split(';');

    multiBC = new FieldLookupValue[bcarr.Length];

    for (int z = 0; z < bcarr.Length; z++)
    {
        busCat = GetItem(ctx, businessCategories, bcarr[z]);

        if (busCat != null)
        {
            ctx.Load(busCat); ctx.ExecuteQuery();

            relatedBC.LookupId = busCat.Id;

            multiBC.SetValue(relatedBC, z);

        }
        else
        {
            multiBC.SetValue(relatedBC, z);
        }
    }

}
Was it helpful?

Solution

The problem was the context where the FieldLookupValue was being initialised. It was supposed to be re-initialised within the loop that appears in the code snippet, but it was actually being initialised out of the loop. This means that simply assigning a LookupId to an already existing FieldLookupValue will not work, unless the FieldLookupValue is re-initialised properly within the right context.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top