Monday 15 April 2013

c# - Moving SPListItems between different ContentDatabases -


I have many SiteCollections, each SiteCollection is associated with its own ContentDatabase, now some SPListItems is added to one of the SiteCollections Which can be moved into another site collection and so in other content databases.

The question is how can I change the list between these different collections into another content database?

One way could be to export items with SPExport and import it into the target database. But it is very ugly and there are so many in the list that comes in hiding time.

This is the only way I could move a list item effectively. Unfortunately, you remove all workflows and version history associated with it. Naturally if you use this code then it will need a little bit change because I only move the list item within a single SPWB. In addition, you will need to check the content type to ensure that only one area is available in the destination list.

  Private Zero copyItem (SPListItem sourceItem, string destinationListName) {SPList Destination List = sourceItem.Web lists [destinationListName]; SPListItem targetItem = Destination list. ITEM Add (); Foreach (SPField field in sourceItem.Fields) {if (field.ReadOnlyField & Field.InternalName! = "Attachments") {targetItem [field.Title] = sourceItem [field.Title]; }} Foreach (string filename sourceItem.Attachments) {SPFile file = sourceItem.ParentList.ParentWeb.GetFile (sourceItem.Attachments.UrlPrefix + fileName); Byte [] imageData = file.OpenBinary (); TargetItem.Attachments.Add (filename, image data); } TargetItem.Update (); }  

No comments:

Post a Comment