Saturday, November 23, 2013
Copy splistitems with folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Configuration;
namespace Mercuria.HR.CopyListItemVersion
{
class Program
{
static Dictionary fieldMapping = new Dictionary
{
//{"GlobalID","PersonalNo"}
};
static void Main(string[] args)
{
try
{
string SrcSiteUrl = ConfigurationManager.AppSettings["srcSiteUrl"];
string srclistName = ConfigurationManager.AppSettings["srcListName"];
string destSiteUrl = ConfigurationManager.AppSettings["destSiteUrl"];
string destlistName = ConfigurationManager.AppSettings["destListName"];
string FolderName = ConfigurationManager.AppSettings["folderName"];
SPListItem folderItem;
using (SPSite site = new SPSite(SrcSiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sourceList = web.Lists[srclistName];
SPWeb destweb = new SPSite(destSiteUrl).OpenWeb();
SPList destList = destweb.Lists[destlistName];
Console.WriteLine(string.Format("Total files to be move={0}", sourceList.ItemCount));
int i = 1;
folderItem = destList.Items.Add(destList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);
// set the folder name and update
folderItem["Title"] = FolderName;
folderItem.Update();
foreach (SPListItem sourceItem in sourceList.Items)
{
Console.WriteLine(string.Format(" {0} Item copied of {1}", i, sourceList.ItemCount));
Console.WriteLine(string.Format("{0} Item copied ", sourceItem["Global ID"]));
CopyItem(sourceItem, destList, folderItem);
i++;
}
Console.WriteLine(string.Format("Item copied completed."));
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
public static void CopyItem(SPListItem sourceItem, SPList destinationList, SPListItem folderItem)
{
//create a listitem object to add item in the foler
SPListItem targetItem = destinationList.Items.Add(folderItem.Folder.ServerRelativeUrl, SPFileSystemObjectType.File, null);
for (int i = sourceItem.Versions.Count - 1; i >= 0; i--)
{
//set the values into the target list
foreach (SPField sourceField in sourceItem.Fields)
{
SPListItemVersion version = sourceItem.Versions[i];
if ((!sourceField.ReadOnlyField) && (sourceField.InternalName != "Attachments") && (sourceField.Type != SPFieldType.Calculated) && !sourceField.TypeDisplayName.Contains("ESOP % 2011") && (sourceField.Type != SPFieldType.DateTime))
{
try
{
if (sourceField.InternalName == "Title0")
{
targetItem[Mapper(sourceField.InternalName)] = version[sourceField.InternalName];
}
else
{
targetItem[Mapper(sourceField.Title)] = version[sourceField.Title];
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Destination field={0} :: ", sourceField.Title));
Console.WriteLine(ex);
}
}
else if (sourceField.Title == "Created" ||
sourceField.Title == "Created By" ||
sourceField.Title == "Modified" ||
sourceField.Title == "Modified By")
{
try
{
targetItem[sourceField.Title] = version[sourceField.Title];
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
else if (sourceField.Type == SPFieldType.DateTime)
{
try
{
targetItem[sourceField.Title] = (DateTime.Parse(version[sourceField.Title].ToString(), null, System.Globalization.DateTimeStyles.AdjustToUniversal).AddDays(1));
}
catch
{
}
}
}
//update the target item and loop over the the next version
targetItem.Web.AllowUnsafeUpdates = true;
targetItem.Update();
targetItem.Web.AllowUnsafeUpdates = false;
}
// now check for attachments (not versioned) and copy
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
targetItem.Attachments.Add(fileName, file.OpenBinary());
}
targetItem.Web.AllowUnsafeUpdates = true;
// perform a systemupdate so that a new version is not created
targetItem.SystemUpdate();
destinationList.Update();
targetItem.Web.AllowUnsafeUpdates = false;
}
// updates the output fieldname if the destination list has different fields (must be the same type though!)
public static string Mapper(string fieldNameIn)
{
// if not in the mapping dictionary then use the infieldname as the field to write to.
string fieldNameOut = fieldNameIn;
//if (fieldMapping.ContainsKey(fieldNameIn)) fieldNameOut = fieldMapping[fieldNameIn];
return fieldNameOut;
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment