June 5, 2009

MOSS: Unable to import custom SPFields of type User

Problem:
Imagine the following scenario: You have created custom SPField for your Content Type. SPField is of type "User", i.e., a PeopleEditor field where you can select a one or several Active Directory users or groups.

In my case the SPField definition is something like this:

<Field ID="{UNIQUE_GUID}"
Name="ProjectOwner"
Group="MyGroup"
Type="User"
DisplayName="Project Owner"
SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
StaticName="ProjectOwner"
Required="FALSE">
</Field>

Then you do a STSADM EXPORT and IMPORT for the SPWeb using Content Type using this SPField.

STSADM EXPORT succeeds, but STSADM IMPORT throws error:

Progress: Importing ListItem /a/windows-hardening/Pages?id=1.
FatalError: Value cannot be null.
Parameter name: g
at System.Guid..ctor(String g)
at Microsoft.SharePoint.Deployment.ListItemSerializer.UpdateFieldData(SPListItem listItem, ImportObjectManager objectManager, Guid docId, String fieldName, String value, String value2, Guid gFieldId, Boolean& bCreated, Dictionary`2 brokenFields)
at Microsoft.SharePoint.Deployment.ListItemSerializer.UpdateFieldData(SPListItem listItem, Guid docId, Boolean& bCreated, SPContentTypeId contentTypeId, ImportObjectManager objectManager, Object data)
at Microsoft.SharePoint.Deployment.ListItemSerializer.SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
at Microsoft.SharePoint.Deployment.XmlFormatter.ParseObject(Type objectType,Boolean isChildObject)
at Microsoft.SharePoint.Deployment.XmlFormatter.DeserializeObject(Type objectType, Boolean isChildObject, DeploymentObject envelope)
at Microsoft.SharePoint.Deployment.XmlFormatter.Deserialize(Stream serializationStream)
at Microsoft.SharePoint.Deployment.ObjectSerializer.Deserialize(Stream serializationStream)
at Microsoft.SharePoint.Deployment.ImportObjectManager.ProcessObject(XmlReader xmlReader)
at Microsoft.SharePoint.Deployment.SPImport.DeserializeObjects()
at Microsoft.SharePoint.Deployment.SPImport.Run()

Thoughts:
If you use -nofilecompression parameter when exporting and importing using STSADM, you can look at the exported Manifest.xml and see the difference between fields that have List="UserInfo" and the ones that don't.

OOB field PublishingContact value is exported like this:
<Field Name="PublishingContact" Value="2;UserInfo" FieldId="6677cf63-f416-4e1e-9e6e-f77f243ef4a6" />

while custom ProjectOwner field value without List="UserInfo" in its definition is exported like this:
<Field Name="ProjectOwner" Value="2;691e9a30-76c4-42b6-869f-294c2c68267d" FieldId="6677cf63-f416-4e1e-9e6e-f77f243ef4a6" />

You can see that the custom field contains the GUID of the list where the item is located in, while the OOB field contains clear reference to UserInfo list.

Solution:
First of all, remember to always include List="UserInfo" when defining SPFields of type "User", like this:

<Field ID="{UNIQUE_GUID}"
Name="ProjectOwner"
Group="MyGroup"
Type="User"
DisplayName="Project Owner"
SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
StaticName="ProjectOwner"
Required="FALSE"
List="UserInfo">

</Field>

If you realize this requirement too late, you can modify the exported Manifest.xml and replace the values of the fields affected from:

<Field Name="ProjectManager" Value="2;GUID_OF_PAGES_LIST" FieldId="6677cf63-f416-4e1e-9e6e-f77f243ef4a6" </>

to

<Field Name="ProjectManager" Value="2;UserInfo" FieldId="6677cf63-f416-4e1e-9e6e-f77f243ef4a6"</>

and import after that.

In addition you need to have custom application that enumerates all existing items and fix field definition on them using API.

6 comments:

  1. Thank you - this is a very good post and solved my issue. Powlo

    ReplyDelete
  2. Thanks, good catch!

    ReplyDelete
  3. I am unable to resolve this issue. I had a few questions about the manifest.xml file changes.
    Thanks,

    ReplyDelete
  4. Rock'n'roll!!!! Thank you for the info! Do you know exactly for what this info is needed by MOSS or what does it mean?

    ReplyDelete
  5. Very helpfull article, keep it up.

    ReplyDelete
  6. Even now, in 2015, It's still working and this info is very hard to find, thank you for sharing !!

    ReplyDelete