How to add lookup column programmatically using csom in SharePoint online Office 365?

Add lookup column using SharePoint online in csom

Hi folks today we are going to see how we can add lookup column programmatically using csom in SharePoint 2016 or SharePoint 2013 and SharePoint online (Download PDF for FREE). There are two lists where from one we need to get Title as a lookup field display name and in second list we need to show the title field as a column value.

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

SharePoint deveopment training course
Add lookup column using SharePoint online in csom
Add lookup column using SharePoint online in csom

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.



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 list like below:

SharePoint online add lookup column csom
SharePoint online add lookup column csom

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

How to add lookup column in SharePoint online using csom
How to add lookup column in SharePoint online using csom

Read some sharepoint online tutorials:

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

About 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"

View all posts by Bhawana Rathore →