Monday, December 21, 2009

SharePoint DataViews don’t work after site export and import

Frequently Found Problem (FFP):

When using stsadm –o export and stsadm –o import, or SharePoint Desiger backup/import site for that matter, you will find that many of your DataViews and DataForms stop working on the newly imported site. The pages are riddled with web part errors complaining about not being able to access the data source.

Cause
By default, DataViews aka DataForms use the unique GUID of the lists they refer to, to receive data. When re-importing your site, the system reassigns new GUIDs for all the lists but does not clean up all existing references.

Solutions
Not so fast and dirty:
Manually fix the GUIDS after the import. Best option here is to open up the pages which stopped working (Views, WebPart pages, New, Edit and Display Forms of customised lists) and look for the keyword ListID in the document. You will find between 1 and 4 occurrences (normally 3-4) closely followed by the GUID of the list or document library in question. Simply update this GUID to the new one (extracted from the URL of the list settings page of the list) and you’re back up and running

Faster and not quite so dirty:
Change the references from absolute GUID references to named references. Open the pages at fault and look for ListID again. But this time replace the actual occurrence with ListName and replace the GUID that follows with the actual name of the list. This is best done before you do the export, or even better right after you created the custom view so you don’t have to worry about it later again.

Fast and clean
Instead of using the OOB stsadm –o import command to import the data, use one of the wonderful stsadm extensions a guy called Gary Lapointe developed. Although not officially supported by Microsoft, you will find them used by the MS pros themselves in their solutions. (Just look at the AdventureWorks Sample Site package)
One of these is the gl-import2 command which allows you to retain the GUIDS when importing the data.
stsadm -o gl-import2 -url "http://localhost" -retainobjectidentity -filename exportpackage.cmp

Using the above command you do not need to worry about mucking with the Views and Forms in SharePoint Designer at all. Notice the added magic keyword of –retainobjectidentity.