2009-12-01 85 views
1

我有几个SiteCollections,每个SiteCollection都有自己的ContentDatabase关联。现在,一些SPListItems被添加到SiteCollection中的一个SiteCollection中,该SiteCollection已被移入另一个SiteCollection中,因此被移入另一个ContentDatabase中。在不同的ContentDatabases之间移动SPListItems

问题是:如何将这些不同集合之间的ListItems移动到另一个ContentDatabase?

一种方法是使用SPExport导出项目并将其导入到目标数据库中。但是这非常难看,并且有很多ListItems会在洞中出现。

回答

2

这是我发现有效移动列表项的唯一方法。不幸的是,你放弃了与之相关的所有工作流程和版本历史记录。当然,如果您要使用此代码,则需要稍作修改,因为我只在相同的SPWeb内移动列表项。此外,您需要进行内容类型检查以确保目标列表中可用的字段相同。

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