add a lookup column and set relationship behaviour for Sharepoint 2010 list
This post is a short example for how to add a lookup column and set the relationship behaviour for the lookup column using sharePoint 2010 object model
The demo code was created in a console applictaion
SPList SourceList = lists[SourceListId]; -> List.ID of your Source or lookup list
SPList TargetList = lists[TargetListId]; -> List.ID of your list that will contain lookup column
SPFieldCollection Fields = TargetList.Fields;
//Adding a lookup Field in Child List
Fields.AddLookup(lookupFieldName, SourceList.ID, true);
Console.WriteLine("Lookup Field Created");
//Adding the Restrict behaviour
SPFieldLookup NewLookupField = Fields[lookupFieldName] as SPFieldLookup;
NewLookupField.Indexed = true;
NewLookupField.LookupField = "Title";
NewLookupField.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
NewLookupField.Update();
Thats it!
The demo code was created in a console applictaion
SPList SourceList = lists[SourceListId]; -> List.ID of your Source or lookup list
SPList TargetList = lists[TargetListId]; -> List.ID of your list that will contain lookup column
SPFieldCollection Fields = TargetList.Fields;
//Adding a lookup Field in Child List
Fields.AddLookup(lookupFieldName, SourceList.ID, true);
Console.WriteLine("Lookup Field Created");
//Adding the Restrict behaviour
SPFieldLookup NewLookupField = Fields[lookupFieldName] as SPFieldLookup;
NewLookupField.Indexed = true;
NewLookupField.LookupField = "Title";
NewLookupField.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
NewLookupField.Update();
Thats it!
0 comments:
Post a Comment