Part 1: Copy a SharePoint List Programmatically

I've been working with the SharePoint API lately. One of my tasks is to copy SharePoint lists from one site to another site. This can be done in code as long as the two sites are on the same machine. It's OK if they are in different web applications, but it can't be from one server to another. If you need to copy from one server to another, you either need to do an export/import, or find a way to save the settings and import them on the new machine. I believe you can use the SharePoint Web Services to accomplish this, though. I haven't worked with that yet.

Anyway, it took a lot of digging around to figure out all the things I needed to copy lists, so I thought I'd post the code that is working for me. This post is only going to cover copying a basic list. I am planning on several more posts to handle document libraries, wikis, and other special cases. Each has their own challenges.

The requirements for me are to copy the bulk of the lists of a large SharePoint site to a new site with a different structure. Therefore, content and deploy is not going to work. Also, they are redesiging the pages, so they really only wanted the lists. What I'm doing is creating a new list on the new site and copying all the properties, so any metadata, such as who last modified the list, is unfortunately lost.

Here's the code:

// Instantiate web instances
SPSite sourceSite = new SPSite(@"http://SharePointServer:31001");

SPWeb sourceWeb = sourceSite.RootWeb;
SPSite destSite = new SPSite(@"http://SharePointServer:31002");
SPWeb destWeb = destSite.RootWeb;

// Get a reference to the source list
SPList sourceList = sourceWeb.GetList("/Lists/Announcements");

 
// if the list exists on the destination site, delete it

try
{
   SPList temp = destWeb.Lists[sourceList.Title];
   destWeb.Lists.Delete(temp.ID);
}
catch { }

// create new list on the destination web with same properties
Guid newListID = destWeb.Lists.Add(sourceList.Title, sourceList.Description,
                                   sourceList.BaseTemplate);

SPList destList = destWeb.Lists[newListID];

// copy items
foreach (SPListItem item in announcements.Items)
{
   SPListItem newDestItem = destList.Items.Add();
   foreach (SPField field in sourceList.Fields)
   {
      if (!field.ReadOnlyField)
      newDestItem[field.Id] = item[field.Id];
   }
   newDestItem.Update();
}

// set quicklaunch settings
destList.OnQuickLaunch = sourceList.OnQuickLaunch;
destList.Update();
Technorati Tags:
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Print | posted on Friday, January 23, 2009 6:18 AM

Feedback

# re: Copy a SharePoint List

left by Nadège DEROUSSEN at 1/26/2009 1:09 AM Gravatar
Your article is very interesting. Don't forget to dispose SPSite and SPWeb objects or you will experience OutOfMemory Exception.

# re: Copy a SharePoint List

left by Anne Bougie at 1/27/2009 7:58 AM Gravatar
The code in this post is for demonstration purposes only. It is not a finished product.

# re: Part 1: Copy a SharePoint List Programmatically

left by Sunil at 2/19/2009 8:52 AM Gravatar
I think following line should be changed from foreach (SPListItem item in announcements.Items)
to
foreach (SPListItem item in sourceList.Items)
right?

# re: Part 1: Copy a SharePoint List Programmatically

left by Lou at 3/31/2009 2:30 AM Gravatar
I can only get this code to work for a list containing one column. When I add more then one column to the list I get an exception.
"Invalid Field name. {GUID}"

Has anyone had this problem?

# re: Part 1: Copy a SharePoint List Programmatically

left by Joe Mack at 5/29/2009 12:14 AM Gravatar
If you are programming using the SharePoint object model, then I am sure you know this...but hopefully some of your readers will benefit.

You can move a list using a list template.

1. Save the list as a template from the source list (make sure to include the contents)...save the template file on your local machine.
2. Import the template to the list templates gallery on the source site.
3. Create a list from the newly imported template.
4. Delete the template from the list templates gallery if you no longer need it.

Much easier...and like everything else that can be done through the UI, us geeks can explain it to people that cannot code, so they can move their own stuff and free us up to use our mad coding skills for other things.

# re: Part 1: Copy a SharePoint List Programmatically

left by Hans Bla at 8/4/2009 8:55 PM Gravatar
The part where you iterate through the fields of the SPListItem was an "aha"-moment for me.

Thanks a lot for this post

# re: Part 1: Copy a SharePoint List Programmatically

left by Andrew at 11/17/2009 12:01 AM Gravatar
This is a 1/10 solution... Steps are something like

Create list based on source TemplateFeatureID and BaseType
Copy Fields
Copy Content Types
Copy Views
Copy List Properties
Copy SPFile objects (if applicable)
Copy SPFolder objects (if applicable)
Copy List Items.
Copy List Item Versions.

It's not that simple, SharePoint is not that simple.

# re: Part 1: Copy a SharePoint List Programmatically

left by Pankaj at 1/21/2010 5:13 PM Gravatar
I agree with Andrew...
This is just a small piece of the complete List.
To make an "exact copy" we need to consider all those things mentioned by Andrew.
So, I feel, instead of using this technique, we should somehow find a way to save the source List as template (stp file with content), then copy it to the Dest Site Collection's "List Template gallery" and then create the list on the Dest Web using this template.
Of course, in the end you should delete the template from the Dest Site Collection's "List Template gallery" if you want to clean it up :).

Any suggestions?

# re: Part 1: Copy a SharePoint List Programmatically

left by Eric at 5/3/2011 12:35 AM Gravatar
Anne,

I'm not a programmer and I would like to copy select list item to an other site list in the same Sharepoint wss 3.0.
Where I use your code ?
I use SharePoint designer and I would like to know if with Designer I can paste your code somewhere to use it ?
The idea is from a SharePoint page to have a button to click or something like that to automaticly copy the list item to the other list in an aother site.

Thanks,
Eric

# re: Part 1: Copy a SharePoint List Programmatically

left by Loo at 7/5/2011 11:31 PM Gravatar
Я об этом думал. В любом случае, продвижение неизбежно. Постараюсь выполнить все инструкции.

# re: Part 1: Copy a SharePoint List Programmatically

left by Sebastien at 7/7/2011 2:31 AM Gravatar
Great piece of code!

As a developer, I would do the exact same thing for a one timer. But, I worked in so many project were I would need to this over and over that we finally decided to build a tool for it. Our customer wanted it so we finally put it on the market. Please look at the following tool for copying items from one list to another : http://www.fivenumber.com/copy-sharepoint-list-items-from-one-site-to-another-programmatically/

Have a nice one!
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: