Tuesday 15 January 2013

How to Fix lookup columns in SharePoint

I have deployed some list templates and found that Lookups are got broken,
After googling, I found that here, that using PowerShell we can fix the Lookup:

//We can create PowerShell Function as follows:

Function Fix-LookupColumn ($webURL, $listName, $columnName, $lookupListName)
{
    #Get web, list and column objects
    $web = Get-SPWeb $webURL
    $list = $web.Lists[$listName]
    $column = $list.Fields[$columnName]
    $lookupList = $web.Lists[$lookupListName]
 
    #Change schema XML on the lookup column
    $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 confirmation to console and dispose of web object
    write-host "Column" $column.Title "in list" $list.Title "updated to lookup list" $lookupList.Title "in site" $web.Url
    $web.Dispose()
}

By paasing parameters, we can execute the above function

Open SharePoint Powershell command prompt and execute above function
followed by this command to call the function for the particular list and column that you wish to fix:

Fix-LookupColumn -webURL <URL> -listName "<List name containing the lookup column>" -columnName "<Lookup column name>" -lookupListName "<List used by the lookup column>"

For example:

Fix-LookupColumn -webURL http://portal -listName "Vendor" -columnName "Company" -lookupListName "Company"

Where,
List Name :- Vendor
Lookup Column:- Company
Lookup List: Company


1 comment :