i have two lists(Invoice & Client) which i had created dynamically through code and i want to create lookup field programmatically for relationship between this two list.
In Invoice List i have following columns:
3.ClientId - this column i need to create as lookup
4.ClientId:ClientName - this column is used to get ClientName from Client List when i enter ID in ClientId Column

In Client List i have following columns:

So far i'm able to create lookfield that is ClientId in invoice list 3rd column but how can i create programmatically 4th column has additional column.

When we create lookup column using sharepoint administration there is an option called "Add a column to show each of these additional fields" this i want to create programmatically

My Code

 List refList= webobj.Lists.GetByTitle(Constants.Client);
 Field LookUpField = listObj.Fields.AddFieldAsXml(
 "<Field Type='Lookup' DisplayName='ClientId' Required='FALSE' List='" + refList.Id + "' 
 ShowField='ID' StaticName='ClientId' 
 Name='ClientId' />", true,AddFieldOptions.DefaultValue);


1 Answer 1


Add ID to the lookup field you have already created. Suppose the ID is 1dbc2f2f-f7b7-4d38-aa60-5f23c737729e. So your field xml will look like this:

<Field ID='{1dbc2f2f-f7b7-4d38-aa60-5f23c737729e}' Type='Lookup' DisplayName='ClientId' Required='FALSE' List='" + refList.Id + "' 
 ShowField='ID' StaticName='ClientId' 
 Name='ClientId' />

Now create another field with following XML:

<Field Type='Lookup' DisplayName='ClientId:ClientName' List='" + refList.Id + "'
  ShowField="ClientName" FieldRef='1dbc2f2f-f7b7-4d38-aa60-5f23c737729e'  
  ID='{86e03e0b-c124-41e0-be41-0e1f6f94bc15}' StaticName='ClientId_x003a_ClientName' 
  Name='ClientId_x003a_ClientName' />

Add this field to the filedcollection of the list. Notice that the ID of first field is referenced in second field using FieldRef

  • ID value can be any random number or any other sequence i should use thnks
    – Light
    Oct 31, 2014 at 6:16
  • ID can be anything as long as it satisfies the format. You can use visual studio to generate one. Go to Visual Studio and click on tools-> then create GUID Oct 31, 2014 at 6:21

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.