0

Currently I have two Share Point lists (supposed to A & B), each list contains look up fields to each other.

I want to insert records to these lists, the problem is how can I reference the look up column (from list A) to ID (from list B) when item in the list B haven't been created yet and the reverse?

The look up column references to the ID which will be created after item inserted, any columns instead of ID?

I now have no good solutions for this. So anyone can suggest me?

1

I assume that your problem is that the items you create in the lists are referring each other. For example, you create ItemA in ListA and ItemB in ListB. ItemA references ItemB and ItemB references ItemA. I assume further, that you would like to create the items from code and the lookup fields are mandatory.

In this case you can simply create ItemB first (without the reference yet), the knowing the ID of ItemA you can create ItemB (setting the reference on ItemA), finally update the (null) reference on the item ItemA to ItemB.

So something like that should do the job:

    using(SPSite site = new SPSite("http://YouSharePoint"))
    using (SPWeb web = site.OpenWeb())
    {
        SPList listA = web.Lists["ListA"];
        SPList listB = web.Lists["ListB"];
        SPListItem itemA = listA.AddItem();
        itemA.Title = "TitleA";
        itemA.Update();

        SPListItem itemB = listB.AddItem();
        itemB.Title = "TitleB";
        itemB["LookUpFieldForListA"] = new SPFieldLookupValue(itemA.ID, itemA.Title);
        itemB.Update();

        itemA["LookUpFieldForListB"] = new SPFieldLookupValue(itemB.ID, itemB.Title);
        itemA.Update();
    }
1
  • I am finding better solutions now. Thanks Pholpar, maybe it should be slow if lots of items to insert and reference! Ok, I will test it and consider the performance. – tbbt Dec 2 '14 at 7:56

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.