1

I am working on a sharepoint server 2013 on-premise. and i run the following power-shell script to create a lookup column :-

#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXML) 



$site = Get-SPSite -Identity "http://***/"
$web = $site.RootWeb


#Assign fieldXML variable with XML string for site column
$fieldXML = '<Field Type="Lookup" 
Name="testcol"
Description=""
DisplayName="test col"
StaticName="testcol"
Group="custom columns"
Hidden="FALSE"
Required="TRUE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="FALSE"
List="Lists/dept" 
ShowField="Title">
</Field>'

#Output XML to console
write-host $fieldXML

#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXML)

now the column was created successfully. but the Get information from: for the lookup column was emty, as follow:- enter image description here

so can anyone adivce on this please?

Now i read this article http://innersharepoint.blogspot.com/2013/09/fixing-blank-lookup-columns-in.html which propose a fix to this problem. so i run the following powershell script:-

$site = Get-SPSite -Identity "http://***/"
$web = $site.RootWeb

$column = $web.Fields["test col"]
$lookupList = $web.Lists["dept"]
#Updating the column properties
$column.SchemaXml = $column.SchemaXml.Replace($column.LookupWebId.ToString(), $web.ID.ToString())
$column.SchemaXml = $column.SchemaXml.Replace($column.LookupList.ToString(), $lookupList.ID.ToString())
$column.Update()
write-host "Column properties have been updated"
$Web.Dispose()

now the site column have referenced the dept lookup list.. so can anyone advice if my above script is valid ? or i am doing things wrongly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.