The other day I demonstrated some code on how to copy files from one document library to another in another site collection. However, the code does not work with lists and list items. Dealing with folders and items within folders is completely different with SharePoint lists. In fact, it seems needlessly complicated. The sample code below takes a source site collection url, destination site collection url, list name (must exist in both source and destination site collection, the string of the UniqueId guid of the starting folder, and the folder’s url.
The difficult part was getting the folder url to work when creating a new folder in a SharePoint list. You must use the SPListItemCollection.Add method passing in an empty string, SPFileSystemObjectTyp.Folder, and the folder url. In order to make this work with sub folders you must construct a folder url in the form of “grandparent.folder.name +/ + parent.folder.name … + / + folder.name”. There is nothing on the SPFolder object that can give you this “container” path. So I had to do some substring(ing).
The other problem was getting at the child items of a folder in a SharePoint list. When using a document library the files collection of the SPFolder works great, unfortunately, this does not exist with SharePoint lists. So you have to make use of the SPQuery object to bring back the child items within the folder. Finally, you can use linq to filter out the list items from the folders. All in all it works great and will create an identical copy of the folder structure from one list to the other across site collections.
public static void CopyListFolderToAnotherSiteCollection(string sourceUrl, string destinationUrl,
using (SPSite sourceSite = new SPSite(sourceUrl))
SPList sourceList = sourceWeb.Lists[listName];
SPListItem destItem = destList.Items.
List<SPListItem> listItems = (from l in sourceItems.OfType<SPListItem>()
List<SPListItem> folderItems = (from l in sourceItems.OfType<SPListItem>()
foreach (SPListItem sourceItem in listItems)
foreach (SPListItem f in folderItems)