Create Lookup Type Site Column using CSOM

Create Lookup Type Site Column  using CSOM programmatically in SharePoint 2013

In this post, I will describe you to Create Lookup Type Site Column using CSOM.You .We create lookup then we associate one list column to another list column  If you need to create choice type site column, follow my post. Create Choice Type Site Column using CSOM

[AdSense-A]

 ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
string schemaLookupField = "<Field Type='Lookup' Name='CountryName' StaticName='CountryName' DisplayName='CountryName' 
    List='Countries' ShowField='Title' />"
rootWeb.Fields.AddFieldAsXml(schemaLookupField , true, AddFieldOptions.AddFieldInternalNameHint);
clientContext.ExecuteQuery();

If you need to show another column from the lookup list, instead of the Title field, Your choice you can set the ShowField attribute to another column.

 

string schemaLookupField = "<Field ID ='<GUID>' Type='Lookup' Name='CountryName' StaticName='CountryName' DisplayName='CountryName' 
    List='Countries' ShowField='Title' RelationshipDeleteBehavior='Restrict' Indexed='TRUE'/>"

If you need to create multi-select lookup column, you need to use the type as LookupMulti, and you need to set an additional attribute as Mult.

string schemaMultiLookupField = "<Field ID ='<GUID>' Type='LookupMulti' Name='CountryName' StaticName='CountryName' DisplayName='CountryName' 
    List='Countries' ShowField='Title' Mult='TRUE'/>

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.