Powershell – Changing List’s Column type from Lookup in Sharepoint

powershellsharepointsharepoint-2007

I am using the ilovesharepoint Lookup Field with Picker that is on codeplex on a Moss 2007 standard environment. I ran the setup project, and then in Powershell issued the appropriate command to change a Lookup field to the Lookup with Picker (ConvertLookupToLookupFieldWithPicker.ps1) which is shown below.

# http://www.iLoveSharePoint.com
# by Christian Glessner

param([string]$webUrl=$(throw 'Parameter -webUrl is missing!'), 
    [string]$listName=$(throw 'Parameter -listName is missing!'), 
    [string]$fieldTitle=$(throw 'Parameter -fieldTitle is missing!'))

$ErrorActionPreference = "Stop";

$env:12HivesDir = "$env:CommonProgramFiles\Microsoft Shared\web server extensions\12\";
$null = [System.Reflection.Assembly]::LoadFrom("$env:12HivesDir\ISAPI\Microsoft.SharePoint.dll");
$site =  New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList $webUrl;
$web = $site.OpenWeb();
$list = $web.Lists[$listName];

$field = $list.Fields[$fieldTitle];
$schema = [xml]$field.SchemaXml;

$schema.Field.Type = "LookupFieldWithPicker"

if($schema.SelectSingleNode("//@SearchFields") -eq $null)
{
    $searchFields = $schema.CreateAttribute("SearchFields");
    $searchFields.PSBase.Value = [Microsoft.SharePoint.SPBuiltInFieldId]::ID.ToString();
    $schema.Field.PSBase.Attributes.Append($searchFields);
}

$field.SchemaXml = $schema.PSBase.OuterXml;

Write-Output "Done."

I keep seeing this error come up

1d22ea11-1e32-424e-89ab-9fedbadb6ce1
Exception setting "SchemaXml": "Non-supported field type change.
The field cannot be changed to the new type. Please check the new type and try again."
At C:\Documents and Settings\xgrmmart\Desktop\icgpm\deploy\ConvertLookupToLookupFieldWithPicker.ps1:29 char:8
+ $field.S <<<< chemaXml = $schema.PSBase.OuterXml;

No idea why its not working….ran perfectly in the test environment. I'm not an expert on PowerShell and any help is much appreciated. Thanks.

Best Answer

Ok I fixed this issue. The problem occurs if you run the PowerShell script before running the batch file (I was unaware that someone had run it before). So, you need to use the converse powershell script file ConvertLookupFieldWithPickerToLookup.ps1 and apply it against the columns. Once you are done, reapply the first file above and it should all work.

The code is as follows:

# http://www.iLoveSharePoint.com
# by Christian Glessner

param([string]$webUrl=$(throw 'Parameter -webUrl is missing!'), 
    [string]$listName=$(throw 'Parameter -listName is missing!'), 
    [string]$fieldTitle=$(throw 'Parameter -fieldTitle is missing!'))

$ErrorActionPreference = "Stop";

$env:12HivesDir = "$env:CommonProgramFiles\Microsoft Shared\web server extensions\12\";
$null = [System.Reflection.Assembly]::LoadFrom("$env:12HivesDir\ISAPI\Microsoft.SharePoint.dll");
$site =  New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList $webUrl;
$web = $site.OpenWeb();
$list = $web.Lists[$listName];

$field = $list.Fields[$fieldTitle];
$schema = [xml]$field.SchemaXml;

if ($schema.Field.AllowMultipleValues -eq $true)
{
    $schema.Field.Type = "LookupMulti"
}
else
{
    $schema.Field.Type = "Lookup"
}

$schema.Field.RemoveAttribute("SearchFields");
$schema.Field.RemoveAttribute("EntityEditorRows");
$schema.Field.RemoveAttribute("MaxSearchResults");

$field.SchemaXml = $schema.PSBase.OuterXml;

Write-Output "Done."
Related Topic