tags:

views:

95

answers:

1

I have several SiteCollections, each SiteCollection with their own ContentDatabase associated. Now a few SPListItems are added to one of the SiteCollections that have be be moved into another SiteCollection and therefore into another ContentDatabase.

The questions is: How do I move ListItems between these different Collections into another ContentDatabase?

One way could be to export the item with SPExport and import it into the target database. But that's pretty ugly and there are a lot of ListItems coming in the hole time.

+1  A: 

This is the only way I found to effectively move a list item. Unfortunatly you loose all the workflow and version history associated to it. Naturally if you were to use this code it would need a little modification as I only shift the list item within the same SPWeb. Additionally you would need to do a content type check to ensure the same fields are available in the destination list.

        private void CopyItem(SPListItem sourceItem, string destinationListName)
        {
            SPList destinationList = sourceItem.Web.Lists[destinationListName];
            SPListItem targetItem = destinationList.Items.Add();
            foreach (SPField field in sourceItem.Fields)
            {
                if (!field.ReadOnlyField && field.InternalName != "Attachments")
                {
                    targetItem[field.Title] = sourceItem[field.Title];
                }
            }
            foreach (string fileName in sourceItem.Attachments)
            {
                SPFile file = sourceItem.ParentList.ParentWeb.GetFile(
                    sourceItem.Attachments.UrlPrefix + fileName);
                byte[] imageData = file.OpenBinary();
                targetItem.Attachments.Add(fileName, imageData);
            }
            targetItem.Update();
        }
Dan Revell