I create a column "TitleLookUp" of type LookUp, how can I access the column and configure the information via PNP PowerShell?
3 Answers
Example, we can configure the lookup list and show field for lookup field using the PowerShell below.
Connect-PnPOnline –Url https://tenant.sharepoint.com/sites/lz –Credentials (Get-Credential)
$listName="CustomList"
$fieldName="TitleLookUp"
$lookupListTitle="LookupList"
$lookupList=Get-PnPList -Identity $lookupListTitle
$field = Get-PnPField -List $listName -Identity $fieldName
[xml]$schemaXml=$field.SchemaXml
$schemaXml.OuterXml
$schemaXml.Field.Attributes["List"].Value = "{" + $lookupList.Id + "}"
$schemaXml.Field.Attributes["ShowField"].Value = "Title"
Set-PnPField -List $listName -Identity $fieldName -Values @{SchemaXml=$schemaXml.OuterXml}
-
How can I add this column to another site? I already have the column on
site A
and I want to add onsite B
.– KmDroidCommented Jan 15, 2019 at 10:18 -
If you want to add lookup field to new site, we can use Add-PnPFieldFromXml to achieve it.– LZ_MSFTCommented Jan 16, 2019 at 1:19
-
I tried but is not receiving the values from the list, as you can see here: sharepoint.stackexchange.com/questions/255795/…– KmDroidCommented Jan 16, 2019 at 14:19
-
You need add lookup list in another site, and then add lookup field to list in this site.– LZ_MSFTCommented Jan 17, 2019 at 1:35
Here is documentation for Set-PNPListItem with examples of set LookUp or MultiLookUp values: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/set-pnplistitem?view=sharepoint-ps
-
The site that you provided is for insert data in column and not to configure it.– KmDroidCommented Jan 14, 2019 at 15:52
-
Sorry bad reading from my side :D Here: $list = "listname" $ctx = Get-PnPContext $lkField = Add-PnPField -List $list -DisplayName "fieldname" -InternalName "fieldname" -Type Lookup -AddToDefaultView $lkField = $lookupField.TypedObject $lkField.LookupList = "Lookup List ID" $lkField.LookupField = "Lookup List field name" $lkField.update() $ctx.ExecuteQuery() Commented Jan 15, 2019 at 7:35
So you're trying to create a new lookup field via pnp powershell? While add-pnpfield supports adding basic fields, it does not support lookups (yet). See add-pnpfieldfromxml, which allows us to specify an xml schema. It's more work, but it supports the more advanced options. For an example of how to do this for a lookup field, see this answer.