Add lookup column to SharePoint list programmatically using CSOM

Hi folks today we are going to see how to add lookup column programmatically using csom in SharePoint 2016/2013/Online. We will see how to add a lookup column to the SharePoint list programmatically using CSOM (C#).

Add lookup column to SharePoint list programmatically using CSOM

There are two SharePoint lists, where, from one we need to get Title as a lookup field display name and in the second list we need to show the title field as a column value.

Here we have a SharePoint list name as “SourceListName” which has a column as Title. The list looks like below:

And I have another list known as DestinationList which has a Title column. And here we need to add a lookup column to this list.

Below is the full code to add a lookup column to the SharePoint list programmatically using CSOM (c#).

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace SPOnlineConsoleAppDemo
{
class Program
{
static void Main(string[] args)
{
using (ClientContext clientContext = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
List SourceListName = clientContext.Web.Lists.GetByTitle("SourceListName");
List DestinationList = clientContext.Web.Lists.GetByTitle("DestinationList");
clientContext.Load(SourceListName);
clientContext.Load(DestinationList);
clientContext.ExecuteQuery();
Field LookUpField = DestinationList.Fields.AddFieldAsXml(
"<Field Type='Lookup' DisplayName='Employees' Required='FALSE' List='" + SourceListName.Id + "' ShowField = 'Title' StaticName = 'Title' Name = 'Title' /> ", true, AddFieldOptions.DefaultValue);
LookUpField.Update();
clientContext.ExecuteQuery();
}
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}
catch
{
throw;
}
}
}
}

Once we run the code the lookup column will be added to the SharePoint list like below:

Next time when you will add an item to the list you can see the lookup data like below:

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial, we learned how to add a lookup column to the sharepoint list programmatically using client-side object model (csom).

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

  • […] to add lookup column programmatically using csom in SharePoint online Office […]

  • >