SharePoint 2010 Copy or fill a list by using c#

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.



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

}


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.



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


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.



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


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.



//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;
}


That are some things i found out which might be interesting. I hope this helps you a little bit.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.