Geeks With Blogs
Annie Bougie
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:
Posted on Friday, January 23, 2009 6:18 AM SharePoint | Back to top


Comments on this post: Part 1: Copy a SharePoint List Programmatically

# re: Copy a SharePoint List
Requesting Gravatar...
Your article is very interesting. Don't forget to dispose SPSite and SPWeb objects or you will experience OutOfMemory Exception.
Left by Nadège DEROUSSEN on Jan 26, 2009 1:09 AM

# re: Copy a SharePoint List
Requesting Gravatar...
The code in this post is for demonstration purposes only. It is not a finished product.
Left by Anne Bougie on Jan 27, 2009 7:58 AM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting Gravatar...
I think following line should be changed from foreach (SPListItem item in announcements.Items)
to
foreach (SPListItem item in sourceList.Items)
right?
Left by Sunil on Feb 19, 2009 8:52 AM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting 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?
Left by Lou on Mar 31, 2009 2:30 AM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting 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.
Left by Joe Mack on May 29, 2009 12:14 AM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting Gravatar...
The part where you iterate through the fields of the SPListItem was an "aha"-moment for me.

Thanks a lot for this post
Left by Hans Bla on Aug 04, 2009 8:55 PM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting 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.
Left by Andrew on Nov 17, 2009 12:01 AM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting 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?
Left by Pankaj on Jan 21, 2010 5:13 PM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting 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
Left by Eric on May 03, 2011 12:35 AM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting Gravatar...
Я об этом думал. В любом случае, продвижение неизбежно. Постараюсь выполнить все инструкции.
Left by Loo on Jul 05, 2011 11:31 PM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting 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!
Left by Sebastien on Jul 07, 2011 2:31 AM

# re: Part 1: Copy a SharePoint List Programmatically
Requesting Gravatar...
Unfortunately with this approach you will loose "Created" date on your copyed items... Does not work for archiving purposes....
Left by Acid on Apr 15, 2015 8:58 AM

Your comment:
 (will show your gravatar)


Copyright © Annie Bougie | Powered by: GeeksWithBlogs.net