There a plenty of cool functions, but sometimes you just need to code your own solution. So i would like to summarize some things i think it might be necessary to summarize.
At first you should look at my previous post about console application if you want to build a console application.
Tip #1: Do not forget the SPWeb.AllowUnsafeUpdates = true.
Tip #2: Copy the items with versions
Tip #3: Copy the items with attachments
Tip #4 Copy the items and do not change the modifier and creator and the dates
Tip #5: Copy dates from list A to B
That’s my 5 Tips i can give you. Let’s have a look at them with more details.
Tip #2: Copy the items with versions
If you want to copy the items with versions, you should iterate the items normally but the versions in the other way around, cause in the version array the version 1.0 has the highest ID.
[sourcecode language=”csharp”]
//Iterate trough each item in source list
foreach (SPListItem sourceItem in sourceList.Items)
{
//foreach item we have to create a item in target list
SPListItem targetItem = targetList.AddItem();
//also save versions but in the other way round:
//Version 2.0 has the counter = 0 in the array -> the highest counter is the first version
for (int i = sourceItem.Versions.Count – 1; i >= 0; i–)
{
SPListItemVersion sourceField = sourceItem.Versions[i];
targetItem[“MyTextField”] = sourceField[“MyTextField”];
targetItem.Update();
}
}
[/sourcecode]
Tip #3: Copy the items with attachments
If you like to copy also the attachments from List A to List B you should add some code after the versioning.
[sourcecode language=”csharp”]
//Iterate trough each item in source list
foreach (SPListItem sourceItem in sourceList.Items)
{
//foreach item we have to create a item in target list
SPListItem targetItem = targetList.AddItem();
//also save versions but in the other way round:
//Version 2.0 has the counter = 0 in the array -> the highest counter is the first version
for (int i = sourceItem.Versions.Count – 1; i >= 0; i–)
{
SPListItemVersion sourceField = sourceItem.Versions[i];
targetItem[“MyTextField”] = sourceField[“MyTextField”];
targetItem.Update();
}
//Add Attachments if are available
if (sourceItem.Attachments.Count > 0)
{
foreach (string attachment in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + attachment);
targetItem.Attachments.Add(attachment, file.OpenBinary());
targetItem.SystemUpdate(false);
}
}
}
[/sourcecode]
Tip #4 Copy the items and do not change the modifier and creator and the dates
And if you do not want to update the modified by, created by, modified and created dates you should do it better manually. I tried it with System.Update(True) but it does not work as i expected. Then the versioning was not correct or anything was not the same as in the source list.
[sourcecode language=”csharp”]
//Iterate trough each item in source list
foreach (SPListItem sourceItem in sourceList.Items)
{
//foreach item we have to create a item in target list
SPListItem targetItem = targetList.AddItem();
//also save versions but in the other way round:
//Version 2.0 has the counter = 0 in the array -> the highest counter is the first version
for (int i = sourceItem.Versions.Count – 1; i >= 0; i–)
{
SPListItemVersion sourceField = sourceItem.Versions[i];
targetItem[“MyTextField”] = sourceField[“MyTextField”];
targetItem[“Author”] = sourceField[“Author”];
targetItem[SPBuiltInFieldId.Modified] = GetFieldValueAsDate(sourceField[“Modified”]);
targetItem[SPBuiltInFieldId.Created] = GetFieldValueAsDate(sourceField[“Created”]);
//Update Item and create a version if necessary
//Update of Author and Modified is done above manually
targetItem.Update();
}
}
[/sourcecode]
You can overwrite the SPBuiltInFields and the list will not save the current dates.
Tip #5: Copy dates from list A to B
Dates are something special. If you want the correctly to be copied you should write a small function and the check if they are available and then copy the dates.
[sourcecode language=”csharp”]
//Iterate trough each item in source list
foreach (SPListItem sourceItem in sourceList.Items)
{
//foreach item we have to create a item in target list
SPListItem targetItem = targetList.AddItem();
//also save versions but in the other way round:
//Version 2.0 has the counter = 0 in the array -> the highest counter is the first version
for (int i = sourceItem.Versions.Count – 1; i >= 0; i–)
{
SPListItemVersion sourceField = sourceItem.Versions[i];
if (!string.IsNullOrEmpty(GetFieldValueAsDate(sourceField[“MyDateField”])))
targetItem[“MyDateField”] = GetFieldValueAsDate(sourceField[“MyDateField”]);
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;
}
[/sourcecode]
That are some things i found out which might be interesting. I hope this helps you a little bit.
..:: I LIKE SHAREPOINT ::..
Leave a Reply