Provisioning a Lookup Site Column using PowerShell

%3CLINGO-SUB%20id%3D%22lingo-sub-173868%22%20slang%3D%22en-US%22%3EProvisioning%20a%20Lookup%20Site%20Column%20using%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-173868%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20having%20problems%20provisioning%20a%20look-up%20column.%20I%20am%20using%20the%20following%20code%3A%3C%2FP%3E%0A%3CPRE%3E%24list%20%3D%20%24context.Site.RootWeb.lists.GetByTitle(%22Ticket%20Categories%22)%0A%24rootWeb%20%3D%20%24context.Site.RootWeb%0A%24context.Load(%24list)%0A%24fields%20%3D%20%24context.Site.RootWeb.fields%0A%24context.Load(%24fields)%0A%24context.Load(%24rootWeb)%0A%20%20%20%20%0A%23send%20the%20request%20containing%20all%20operations%20to%20the%20server%0A%24context.executeQuery()%0A%0A%24listId%20%3D%20%24list.ID%0A%24LookupWebID%3D%24rootWeb.Id%0A%0A%24fieldAsXML%20%3D%20%22%26lt%3BField%20Type%3D'Lookup'%20DisplayName%3D'Ticket%20Category'%20Required%3D'TRUE'%20EnforceUniqueValues%3D'FALSE'%20List%3D'%7Be3b055fc-e4b0-43d9-b8da-ef4ae0165c56%7D'%20WebId%3D'd5d274fa-2a1f-4da5-86a4-155431b126af'%20ShowField%3D'Title'%20UnlimitedLengthInDocumentLibrary%3D'FALSE'%20Group%3D'Custom%20Columns'%20ID%3D'%7B2CD129D4-6BD8-4029-928D-0D1551F6DC15%7D'%20SourceID%3D'%7Bd5d274fa-2a1f-4da5-86a4-155431b126af%7D'%20StaticName%3D'TicketCategory'%20Name%3D'TicketCategory'%20Version%3D'1'%20%2F%26gt%3B%22%0A%0A%24fieldOption%20%3D%20%5BMicrosoft.SharePoint.Client.AddFieldOptions%5D%3A%3AAddFieldInternalNameHint%0A%24field%20%3D%20%24fields.AddFieldAsXML(%24fieldAsXML%2C%20%24true%2C%20%24fieldOption)%0A%24context.load(%24field)%0A%24context.executeQuery()%3C%2FPRE%3E%0A%3CP%3EThe%20problem%20now%20is%20that%20the%20column%20gets%20created%20but%20doesn't%20work%20properly.%20The%20screenshot%20below%20shows%202%20lookup%20columns%2C%20the%20%3CSTRONG%3ELookup%20Categor%3C%2FSTRONG%3E%3CSTRONG%3Ey%20%3C%2FSTRONG%3Ecolumn%20was%20created%20using%20Browser%20GUI.%20The%20%3CSTRONG%3ETicket%20Categor%3C%2FSTRONG%3Ey%20column%20was%20created%20using%20PowerShell.%20As%20can%20be%20seen%20in%20the%20screenshot%2C%20there%20are%20no%20choices%20available%20for%20the%20%3CSTRONG%3ETicket%20Category%20%3C%2FSTRONG%3Ecolumn%2C%20which%20seems%20like%20it%20was%20not%20provisioned%20properly.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20689px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F30642i560A17BE20059D2F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221242148.png%22%20title%3D%221242148.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ELooking%20at%20the%20Schema%20XML%20of%20both%20columns%2C%20I%20can't%20see%20any%20issue%20though%2C%20apart%20from%20having%20different%20IDs.%3C%2FP%3E%0A%3CPRE%3E%26lt%3BField%20Type%3D%22Lookup%22%20DisplayName%3D%22Ticket%20Category%22%20Required%3D%22TRUE%22%20EnforceUniqueValues%3D%22FALSE%22%20List%3D%22%7Be3b055fc-e4b0-43d9-b8da-ef4ae0165c56%7D%22%20WebId%3D%22d5d274fa-2a1f-4da5-86a4-155431b126af%22%20ShowField%3D%22Title%22%20UnlimitedLengthInDocumentLibrary%3D%22FALSE%22%20Group%3D%22Custom%20Columns%22%20ID%3D%22%7B2cd129d4-6bd8-4029-928d-0d1551f6dc15%7D%22%20SourceID%3D%22%7Bd5d274fa-2a1f-4da5-86a4-155431b126af%7D%22%20StaticName%3D%22TicketCategory%22%20Name%3D%22TicketCategory%22%20Version%3D%221%22%20%2F%26gt%3B%0A%0A%26lt%3BField%20Type%3D%22Lookup%22%20DisplayName%3D%22Lookup%20Category%22%20Required%3D%22TRUE%22%20EnforceUniqueValues%3D%22FALSE%22%20List%3D%22%7Be3b055fc-e4b0-43d9-b8da-ef4ae0165c56%7D%22%20WebId%3D%22d5d274fa-2a1f-4da5-86a4-155431b126af%22%20ShowField%3D%22Title%22%20UnlimitedLengthInDocumentLibrary%3D%22FALSE%22%20Group%3D%22Custom%20Columns%22%20ID%3D%22%7B705eab0d-a97e-4acf-a95f-315cc55cf197%7D%22%20SourceID%3D%22%7Bd5d274fa-2a1f-4da5-86a4-155431b126af%7D%22%20StaticName%3D%22Lookup_x0020_Category%22%20Name%3D%22Lookup_x0020_Category%22%20Version%3D%221%22%20%2F%26gt%3B%3C%2FPRE%3E%0A%3CP%3EWhat%20seems%20to%20be%20wrong%3F%20Is%20there%20a%20correct%20way%20or%20better%20way%20to%20provision%20a%20Lookup%20Site%20Column%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-174089%22%20slang%3D%22en-US%22%3ERe%3A%20Provisioning%20a%20Lookup%20Site%20Column%20using%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-174089%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%0A%3CP%3EIt%20seems%20the%20problem%20is%20because%20of%20the%20static%20%3CSTRONG%3EID%3C%2FSTRONG%3E%20property.%20If%20that%20is%20omitted%2C%20the%20error%20doesn't%20happen%20anymore.%3C%2FP%3E%0A%3CP%3EThanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-173932%22%20slang%3D%22en-US%22%3ERe%3A%20Provisioning%20a%20Lookup%20Site%20Column%20using%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-173932%22%20slang%3D%22en-US%22%3E%3CP%3EI%20tried%20to%20reproduce%20the%20issue%20but%20I%20wasn't%20able%20to.%20Could%20you%20try%20what%20happens%20when%20you%20remove%20the%20%22Version%22%20attribute%20from%20your%20Field%20XML%3F%20When%20I%20include%20this%20attribute%20in%20my%20tests%20I%20received%20the%20error%20messsage%20that%20the%20field%20was%20modified%20by%20another%20user.%20I%20do%20get%20a%20working%20lookup%20field%20however...%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20attribute%20%22SourceID%22%20could%20also%20be%20omitted...%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20little%20side%20note%3A%20are%20you%20familiar%20with%20PnP-PowerShell%3F%20It%20includes%20a%20lot%20of%20usefull%20cmdlets%2C%20for%20example%20Add-PnPFieldFromXml%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fadd-pnpfieldfromxml%3Fview%3Dsharepoint-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fadd-pnpfieldfromxml%3Fview%3Dsharepoint-ps%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I am having problems provisioning a look-up column. I am using the following code:

$list = $context.Site.RootWeb.lists.GetByTitle("Ticket Categories")
$rootWeb = $context.Site.RootWeb
$context.Load($list)
$fields = $context.Site.RootWeb.fields
$context.Load($fields)
$context.Load($rootWeb)
    
#send the request containing all operations to the server
$context.executeQuery()

$listId = $list.ID
$LookupWebID=$rootWeb.Id

$fieldAsXML = "<Field Type='Lookup' DisplayName='Ticket Category' Required='TRUE' EnforceUniqueValues='FALSE' List='{e3b055fc-e4b0-43d9-b8da-ef4ae0165c56}' WebId='d5d274fa-2a1f-4da5-86a4-155431b126af' ShowField='Title' UnlimitedLengthInDocumentLibrary='FALSE' Group='Custom Columns' ID='{2CD129D4-6BD8-4029-928D-0D1551F6DC15}' SourceID='{d5d274fa-2a1f-4da5-86a4-155431b126af}' StaticName='TicketCategory' Name='TicketCategory' Version='1' />"

$fieldOption = [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint
$field = $fields.AddFieldAsXML($fieldAsXML, $true, $fieldOption)
$context.load($field)
$context.executeQuery()

The problem now is that the column gets created but doesn't work properly. The screenshot below shows 2 lookup columns, the Lookup Category column was created using Browser GUI. The Ticket Category column was created using PowerShell. As can be seen in the screenshot, there are no choices available for the Ticket Category column, which seems like it was not provisioned properly.

1242148.png

Looking at the Schema XML of both columns, I can't see any issue though, apart from having different IDs.

<Field Type="Lookup" DisplayName="Ticket Category" Required="TRUE" EnforceUniqueValues="FALSE" List="{e3b055fc-e4b0-43d9-b8da-ef4ae0165c56}" WebId="d5d274fa-2a1f-4da5-86a4-155431b126af" ShowField="Title" UnlimitedLengthInDocumentLibrary="FALSE" Group="Custom Columns" ID="{2cd129d4-6bd8-4029-928d-0d1551f6dc15}" SourceID="{d5d274fa-2a1f-4da5-86a4-155431b126af}" StaticName="TicketCategory" Name="TicketCategory" Version="1" />

<Field Type="Lookup" DisplayName="Lookup Category" Required="TRUE" EnforceUniqueValues="FALSE" List="{e3b055fc-e4b0-43d9-b8da-ef4ae0165c56}" WebId="d5d274fa-2a1f-4da5-86a4-155431b126af" ShowField="Title" UnlimitedLengthInDocumentLibrary="FALSE" Group="Custom Columns" ID="{705eab0d-a97e-4acf-a95f-315cc55cf197}" SourceID="{d5d274fa-2a1f-4da5-86a4-155431b126af}" StaticName="Lookup_x0020_Category" Name="Lookup_x0020_Category" Version="1" />

What seems to be wrong? Is there a correct way or better way to provision a Lookup Site Column?

2 Replies
Highlighted

I tried to reproduce the issue but I wasn't able to. Could you try what happens when you remove the "Version" attribute from your Field XML? When I include this attribute in my tests I received the error messsage that the field was modified by another user. I do get a working lookup field however...

 

The attribute "SourceID" could also be omitted...

 

A little side note: are you familiar with PnP-PowerShell? It includes a lot of usefull cmdlets, for example Add-PnPFieldFromXml

 

https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/add-pnpfieldfromxml?view=sharepoin...

Highlighted

Hello,

It seems the problem is because of the static ID property. If that is omitted, the error doesn't happen anymore.

Thanks!