Thursday, October 11, 2018

Copying list items with metadata (history) to another list (SharePoint 2010, 2013, 2016)

In this article, I continue to sort out the tasks associated with transferring data from one source to another while preserving all the information. This time I published a sample code where I copy the list items with the whole story (metadata) into another list.

The main advantage of this code is the use of C#, which means such wonderful features are available as Custom Timer Job, Event Reciever and much more. In my console project I using following reference and code:

using System;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

1. Current site:
using (SPSite oSPsite = new SPSite("http://sp/sites/test"))
            {
                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                {
                    //List source
                    SPList srcList = oSPWeb.Lists["Users1"];
                    //List destination
                    SPList destList = oSPWeb.Lists["Users2"];
                    foreach (SPListItem sourceItem in srcList.Items)
                    {
                        SPListItem targetItem = destList.AddItem();
                        for (int i = sourceItem.Versions.Count - 1; i >= 0; i--)
                        {
                            SPListItemVersion sourceField = sourceItem.Versions[i];
                            targetItem["Author"] = sourceField["Author"];
                            targetItem["Title"] = sourceField["Title"];
                            targetItem["Editor"] = sourceField["Editor"];
                            targetItem[SPBuiltInFieldId.Modified] = GetFieldValueAsDate(sourceField["Modified"]);
                            targetItem[SPBuiltInFieldId.Created] = GetFieldValueAsDate(sourceField["Created"]);
                            targetItem.Update();
                        }
                    }
                }
  private static String GetFieldValueAsDate(object sourceField)
        {
            string result = string.Empty;
            if (sourceField != null)
            {
                DateTime date = Convert.ToDateTime(sourceField);
                if (date.Year > 1900)
                    result = SPUtility.CreateISO8601DateTimeFromSystemDateTime(date);
            }
            return result;
        }
    }
}
2. Another site:
//Site source
using (SPSite oSPsite = new SPSite("http://sp/sites/test"))
 {
   using (SPWeb oSPWeb = oSPsite.OpenWeb())
    {
      //List source
      SPList srcList = oSPWeb.Lists["Test1"];
       //Site destination
       using (SPSite SPsite = new SPSite("http://sp/sites/RU_test"))
         {
           using (SPWeb SPWeb = SPsite.OpenWeb())
            {
              //List destination 
              SPList destList = SPWeb.Lists["Test2"];
                foreach (SPListItem sourceItem in srcList.Items)
                  {
                    SPListItem targetItem = destList.AddItem();
                      for (int i = sourceItem.Versions.Count - 1; i >= 0; i--)
                        {
                           SPListItemVersion sourceField = sourceItem.Versions[i];
                            targetItem["Author"] = sourceField["Author"];
                            targetItem["Title"] = sourceField["Title"];
                            targetItem["Editor"] = sourceField["Editor"];
                            targetItem[SPBuiltInFieldId.Modified] = GetFieldValueAsDate(sourceField["Modified"]);
                            targetItem[SPBuiltInFieldId.Created] = GetFieldValueAsDate(sourceField["Created"]);
                            targetItem.Update();
                        }
                    }
                }
            }
        }
     }
  }
private static String GetFieldValueAsDate(object sourceField)
        {
            string result = string.Empty;
            if (sourceField != null)
            {
                DateTime date = Convert.ToDateTime(sourceField);
                if (date.Year > 1900)
                    result = SPUtility.CreateISO8601DateTimeFromSystemDateTime(date);
            }
            return result;
        }
    }
}
Happy Coding!

No comments:

Post a Comment