I must do Cascade Deleting when I delete one Element in First lists, it's deletes me all bounded elements from another list's. I only must set a lookup fields at Adding Items. When I chose by editing its delete me all elements, but I don't know how to set lookap field and choose element. I find this article, but it isn't work: http://sarangasl.blogspot.com/2009/12/add-item-to-list-with-lookup-column.html, or I cant understand. Please help.


Here of my shotes Code of items add:

 SPListItem item = lstOtherList.Items.Add();
                           item["Kontrakt"] = properties.AfterProperties["Kontrakt1"];
                           item["Pracownik"] = properties.UserDisplayName;
                           item["Dzien"] = dzienStart;
                           item["Procenty"] = properties.AfterProperties["Procenty"];

I made too Look Up Column caled "ID Czas Pracy Wpisy".

Edited 13:36

I have got two List's: First list has got elements:

                Contract | Worker | Day Start   | Day Stop   | Percent
 Example        Con1     | Paul   | 2012-10-12  | 2012-10-15 | 1/8

Secound List should have

                Contract   |   Worker   |   Day      |   Percent
Should Have     Con 1      |    Paul    | 2012-10-12 |    1/8
                Con 1      |    Paul    | 2012-10-13 |    1/8
                Con 1      |    Paul    | 2012-10-14 |    1/8
                Con 1      |    Paul    | 2012-10-15 |    1/8

So In Secound lists I want to have a for examle name of contract which can't repeat as lookap fild, but i must set ap this value when I make a Secound List.

  • cant you just use event handler? delete items from second list in the 'ItemDeleted' event of first list, i did not get lookup field part, can you be a bit more specific?
    – Diptarag
    Nov 20, 2012 at 11:31
  • Yes, but I must know which elements should delete, but in Items Adding Event i Cant take Id Element, because it non exists yet. So If it works By Delete Cascading only if elements is set a good lookup field, so I trying to find a lookup field method setting. Please help.
    – Grzegorz Z
    Nov 20, 2012 at 11:37

1 Answer 1


Hi please go through this documentation you will find everything you need to know here, go to the last of the page directly to know how to create the lookup column and if you want you can read the rest for detail information

If you have to accomplish the task with code

SPList firstList = site.Lists["FirstListName"];
SPList secondList = site.Lists["SecondListName"];
string strPrimaryCol = secondList.Fields.AddLookup("Lookupfieldname", firstList.ID, true);
SPFieldLookup primaryCol = (SPFieldLookup)secondList.Fields.GetFieldByInternalName(strPrimaryCol);
primaryCol.LookupField = firstList.Fields["ID"].InternalName;
primaryCol.Indexed = true;
primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Cascade;

but what i dont get is how do you plan to use this code in itemadding event, what are you trying to achieve?

I think I got your initial question wrong you wanted to add value to lookup field in itemadding, not creating the lookup field, for that purpose you need to use SPFieldLookupValue, just use item["Look Up Field Name"]=new SPFieldLookupValue(lookupid, lookupvalue);

that's it, read the msdn documentation if you want

  • 1
    ok let me give you some code
    – Diptarag
    Nov 20, 2012 at 11:56
  • Because I have Two lists. One with ranges from Date Start, to Date Stop. And Secound with Only one date, detailed a elements. I loop Items Adding to add all data From Date Start to Date Stop. And Secound list showing only detailed element's by element's. So If I add a Detailed Element I must have got a lookap Value to delete all Bound Items. Item From First List and All Bounded Elements from secound Lists, which are bounded in first element which is deleting.
    – Grzegorz Z
    Nov 20, 2012 at 12:10
  • I have got a problem with this line of code primaryCol.LookupField = firstList.Fields["Kontrakt1"].InternalName;
    – Grzegorz Z
    Nov 20, 2012 at 12:26
  • 1
    see my update i think you need that, the code if to create the lookup field, but you have already created the lookup field, you just want to add item to it?
    – Diptarag
    Nov 20, 2012 at 12:29
  • 1
    use something like item["ID Czas Pracy Wpisy"]=new SPFieldLookupValue(lookupid, lookupvalue);
    – Diptarag
    Nov 20, 2012 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.