SharePoint 2010: How to Copy a List Programmatically within the Same Site Collection Using C#

SharePoint 2010: How to Copy a List Programmatically within the Same Site Collection Using C#



Introduction


Recently someone expressed his requirements to copy sharepoint lists within the same site collection. Hence to automate this process at a single place I have developed this console application. This code can be further modified as per the requirement. This needs to be executed from any WFE and the input values are self explanatory. This works with sharepoint 2007 and sharepoint 2010 as well. This tool is internally creating a template from the source list then using this template to create the destination list. Once the list is created the template created in the previous step is getting deleted so that next time this can be executed without any issues with the same source list.

C# Code Example


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
 
 
namespace CopyListInSameSiteCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This tool copies a list within the same site collection");
            Console.WriteLine("Please enter the URL of the Source Site");
            String sourceSite = Console.ReadLine();
            Console.WriteLine("Please enter the name of the source list");
            String sourceListName = Console.ReadLine();
            Console.WriteLine("Please enter the URL of the Destination Site");
            String destinationSite = Console.ReadLine();
            Console.WriteLine("Please enter the name of the Destination list");
            String destinationListName = Console.ReadLine();
            Console.WriteLine("Please enter the description of the Destination list");
            String destinationListDescription = Console.ReadLine();
            try
            {
                using (SPSite site = new SPSite(sourceSite))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList sourceList = web.Lists[sourceListName];
                        String templateName = sourceList.Title;
                        String templateFileName = sourceList.Title;
                        sourceList.SaveAsTemplate(templateFileName, templateName, sourceList.Description, true);
                        SPListTemplate listTemplate = site.GetCustomListTemplates(web)[templateName];
                         
                        String destinationWeb = destinationSite.Remove(0, site.Url.Length + 1);
                        using (SPWeb destWeb = site.OpenWeb(destinationWeb))
                        {
                            destWeb.Lists.Add(destinationListName, destinationListDescription, listTemplate);
                            destWeb.Update();
                          
                             
                        }
                        SPList listTemplates = site.RootWeb.Lists["List Template Gallery"];
                        SPListItemCollection listTemplateItems = listTemplates.Items;
                     foreach (SPListItem listTemplateItem in listTemplateItems)
                     {
                           
                          if(listTemplate.Name.Equals(listTemplateItem["Title"]))
                          {
                              listTemplateItem.Delete();
                              listTemplates.Update();
                              break;
                          }
                       }
                    }
                }
                 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("The List is copied");
        }
    }
}

See Also

Leave a Comment
  • Please add 3 and 7 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Gokan Ozcifci edited Revision 9. Comment: changed title to 2010

  • Gokan Ozcifci edited Revision 8. Comment: content and title  

  • Richard Mueller edited Revision 7. Comment: Removed (en-US) from title, added tag

  • Craig Lussier edited Revision 6. Comment: added en-US to tags and title

  • Nevin Janzen edited Revision 5. Comment: Tags Edit

  • Ed Price - MSFT edited Revision 4. Comment: Title casing and tags

  • Craig Lussier edited Revision 2. Comment: minor text edits

  • Craig Lussier edited Revision 1. Comment: added toc and post headings

  • Craig Lussier edited Original. Comment: modified title to indicate the usage of C#

Page 1 of 1 (9 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • great that you posted this in C# and PowerShell. Keep the posts coming!

  • Craig Lussier edited Original. Comment: modified title to indicate the usage of C#

  • Craig Lussier edited Revision 1. Comment: added toc and post headings

  • Craig Lussier edited Revision 2. Comment: minor text edits

  • Nice article

  • Ed Price - MSFT edited Revision 4. Comment: Title casing and tags

  • This code only copies the items in the list.

    What about Version History?

    What about metadata such as Modifier/Modified?

    What about list attributes such as workflows and properties settings?

    Any code for that?

  • Nevin Janzen edited Revision 5. Comment: Tags Edit

  • Craig Lussier edited Revision 6. Comment: added en-US to tags and title

  • Richard Mueller edited Revision 7. Comment: Removed (en-US) from title, added tag

  • Gokan Ozcifci edited Revision 8. Comment: content and title  

  • Gokan Ozcifci edited Revision 9. Comment: changed title to 2010

Page 1 of 1 (12 items)