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


Other Languages

This article is also available in the following languages: