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: 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