开发者

Moving SPListItems between different ContentDatabases

开发者 https://www.devze.com 2022-12-12 20:01 出处:网络
I have several SiteCollections, each SiteCollection with their own ContentData开发者_如何学Cbase associated. Now a few SPListItems are added to one of the SiteCollections that have be be moved into an

I have several SiteCollections, each SiteCollection with their own ContentData开发者_如何学Cbase 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.


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();
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消