Fixing blank lookup columns in SharePoint when creating new site from a site template

One of the common issues when creating a new site from a site Template is that the lookup columns values are blanks. The full scenario is as below :
  • Create a SharePoint site
  • Add lists with lookup columns
  • Insert some data in the lists
  • Save the site as Template with its content
  • Create a new site from the Template
When you access the list which contains the lookup column, you will notice that the lookup values are empty. The lookup values still exist but they are not rendered. We have to fix the list schema by updating some properties of the lookup column. Two properties have to be updated :
  • The Web Id
  • The List Id
There are two ways to fix the list schema :
  • Using C#
  • Using Powershell
We will see how to fix the error using Powershell. The script below will let you fix the column.

$web = Get-SPWeb "Your web url"
$list = $web.Lists["Your list name"]
$column = $list.Fields["Your column name"]
$lookupList = $web.Lists["Your source list name"]
#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()
  
More details about the lookup columns on MSDN.



 

Fixing Error : GetUserListSchema(): Failed to get the list schema XML for feature

One of the common error that you may encounter when you try deploy a SharePoint solution is :

Error 36 Error occurred in deployment step 'Activate Features': Cannot complete this action.
Please try again.

After analyzing the SharePoint log files I found out the error bellow :

GetUserListSchema(): Failed to get the list schema XML for feature

The error above has been caused by the manipulation bellow :

In this project I had only one feature that contained the list definition and the list instance and then I have created a new feature scoped site and move the list definition to it. I kept the list instance in the scoped web feature.

In order to fix the problem, you have to edit the element.xml of the list instance and add the featureid attribute. The feature id attribute should be the id of the list definition's feature.

Here is a sample element.xml file.


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="
http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="ListInstanceName"
                OnQuickLaunch="TRUE"
                TemplateType="10000"
                FeatureId="644d32f3-30d8-441f-a24c-84cd3931cdf0"
                Url="Lists/ListInstanceName"
                Description="My ListInstanceName">
  </ListInstance>
</Elements>