SharePoint CSOM : Create Lookup column with additional field

This post shows how to create a lookup column with additional field using SharePoint C# Client side object model code.
I have two lists, Employee List and Department List. The Department List has a Title field and Code field. We will create a lookup column in Employee List which gets information from Title field of Department List. The lookup column will also include the additional Code field. 

List list = clientContext.Web.Lists.GetByTitle("Employee");
List lookupList = clientContext.Web.Lists.GetByTitle("Department");

string lookupFieldSchema = @"<Field Type = 'Lookup' DisplayName = 'Employee Department' 
                    List='{fdd167ea-8aa5-4376-8320-2b123d03acf9}' 
                    ID = '{769BE2A4-E014-442F-B8F9-5B9581223CF5}' StaticName = 'EmployeeDepartment' 
                    Name = 'EmployeeDepartment' ShowField = 'Title' />";
string additionalFieldSchema = @"<Field Type = 'Lookup' DisplayName = 'Employee Department: Code' 
                            List='{fdd167ea-8aa5-4376-8320-2b123d03acf9}' 
                            ID = '{A1AE51CF-F273-4E15-9DC1-84B4DC80608E}' 
                            StaticName = 'EmployeeDepartmentCode' Name = 'EmployeeDepartmentCode' 
                            ShowField = 'Code' FieldRef = '769BE2A4-E014-442F-B8F9-5B9581223CF5' />";

Field field = list.Fields.AddFieldAsXml(lookupFieldSchema, true, AddFieldOptions.AddFieldToDefaultView);

Field additionalField = list.Fields.AddFieldAsXml(additionalFieldSchema, true, AddFieldOptions.AddFieldToDefaultView);

clientContext.ExecuteQuery();

The schema for both fields has a List property which contains the List ID of the Department List. The ShowField attribute of the lookup column gets value from Title field of the Department List and the ShowField attribute of the additional field gets its value from Code filed of the Department List. The other important property of additional field is the FieldRef property which contains the ID of the lookup column.
Lookup Column


SharePoint: Export Managed Metadata terms to XML using PowerShell

This script uses CSOM PowerShell to export terms stored in managed metadata services to XML.

param (
    [string]$tennantUrl,
    [string]$userName,
    [string]$password,
    [string]$termStoreName
 )



#Load all required client assemblies
#If this script is run on a machine where SharePoint is NOT installed, 
#Please copy these libraries from the a SP server and copy to a local directory and update the paths below

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"

$currentDir = (Resolve-Path .\).Path

 function Initialize(
    [string]$url,
    [string]$user,
    [string]$pass,
    [string]$termStoreName,
    [string]$groupName,
    [string]$termSetName,
    [string]$xmlFilePath
    )
 {

    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url);
    $xmlWriter = $null;

    try
    {
        $xmlWriter = New-Object System.Xml.XmlTextWriter($xmlFilePath,$null);
        $xmlWriter.Formatting = [System.Xml.Formatting]::Indented

        $user = $user.Trim();
        $pass = $pass.Trim();

        #$securePassword = ConvertTo-SecureString $pass -AsPlainText -Force 
        #$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $securePassword) 

        if( [System.String]::IsNullOrEmpty( $user ) )
        {
            $credentials =[System.Net.CredentialCache]::DefaultCredentials;
        } else {
            $credentials = New-Object System.Net.NetworkCredential($user, $pass);
        }

        $ctx.Credentials = $credentials;
        # Works this way, but won't if using just $ctx.  Go figure....
        $ctx2 = [Microsoft.SharePoint.Client.ClientRuntimeContext] $ctx;
        $taxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession( $ctx2 );
        $ctx.Load($taxonomySession);
        $ctx.Load($taxonomySession.TermStores);
        $ctx.ExecuteQuery();

        $termStore = $taxonomySession.TermStores.GetByName($termStoreName);
        $groups = $termStore.Groups;

        $ctx.Load($termStore);
        $ctx.Load($groups);
        $ctx.ExecuteQuery();

        $termSetCollection = $termStore.Groups.GetByName($groupName).TermSets;

        $ctx.Load($termSetCollection);
        $ctx.ExecuteQuery();

        $termSet = $termSetCollection.GetByName($termSetName);


        $ctx.Load($termSet);
        $ctx.Load($termSet.Group);
        $ctx.Load($termSet.Terms);
        $ctx.ExecuteQuery( );

        $xmlWriter.WriteStartDocument();

        $xmlWriter.WriteStartElement("termSet");
        $xmlWriter.WriteAttributeString("name", $termSetName );
        $xmlWriter.WriteAttributeString("groupName", $groupName );        

        foreach($term in $termSet.Terms)
        {
            $ctx.Load($term);
            $ctx.ExecuteQuery();

            WriteTerm $xmlWriter $ctx $term;
        }

        $xmlWriter.WriteEndElement();
        $xmlWriter.WriteEndDocument();
        write-host -ForegroundColor Green $xmlFilePath "saved"

    }
    catch
    {
        Write-Host "Error : $($_)";
        #return;
    }
    finally
    {
        if($xmlWriter -ne $null)
        {
            $xmlWriter.Dispose();
        }

        $ctx.Dispose();
    }
 }

function WriteProperties(
    [System.Xml.XmlTextWriter]$writer, 
    [Microsoft.SharePoint.Client.ClientContext]$context, 
    [Microsoft.SharePoint.Client.Taxonomy.TermSetItem]$termSet
)
{
    if (0 -lt $termSet.CustomProperties.Count)
    {        

        foreach ($property in $termSet.CustomProperties.GetEnumerator())
        {            
            $writer.WriteAttributeString($property.Key,$property.Value);       
        }       
    }
}

function WriteLocalProperties(
    [System.Xml.XmlTextWriter]$writer, 
    [Microsoft.SharePoint.Client.ClientContext]$context, 
    [Microsoft.SharePoint.Client.Taxonomy.Term]$term
)
{
    if (0 -lt $term.LocalCustomProperties.Count)
    {       

        foreach ($property in $term.LocalCustomProperties.GetEnumerator())
        {            
            $writer.WriteAttributeString($property.Key,$property.Value);                                        
        }       
    }
}

function WriteTerm( 
    [System.Xml.XmlTextWriter]$writer, 
    [Microsoft.SharePoint.Client.ClientContext]$context, 
    [Microsoft.SharePoint.Client.Taxonomy.Term]$term
)
{
    $writer.WriteStartElement("term");
    $writer.WriteAttributeString("name", $term.Name);   

    $context.Load($term.Terms);
    $context.ExecuteQuery();

    WriteProperties $writer $context $term;
    WriteLocalProperties $writer $context $term

    if(0 -lt $term.Terms.Count)
    {

        $writer.WriteStartElement("terms");

        foreach ($child in $term.Terms)
        {
            WriteTerm $writer $context $child;
        }

        $writer.WriteEndElement();
    }

    $writer.WriteEndElement();
}
profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+