1

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:
1.ID
2.InvoiceNo
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
5.etc

In Client List i have following columns:
1.ID
2.ClientName
3.Address
4.etc

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);
 clientContext.Load(refList);
 clientContext.ExecuteQuery();
 Field LookUpField = listObj.Fields.AddFieldAsXml(
 "<Field Type='Lookup' DisplayName='ClientId' Required='FALSE' List='" + refList.Id + "' 
 ShowField='ID' StaticName='ClientId' 
 Name='ClientId' />", true,AddFieldOptions.DefaultValue);
 LookUpField.Update();
 clientContext.ExecuteQuery();

Thanks

3

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 – Rakesh Sadhula Oct 31 '14 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 – Nadeem Yousuf Oct 31 '14 at 6:21

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.