We can copy a List from one SharePoint site to another using List Template Exporting & PowerShell. In this article we will see the Server Object Model way of doing that.
The SPList class is the core type involved in this.
The following are the activities involved in our application.
Create a List in a SharePoint site based on the Tasks template and name it as Tasks 1.
Create a console application.
Add a reference to the Microsoft.SharePoint assembly.
Set the Build > Platform Taget to Any CPU; see:
In the Program.cs add the following code:
static void Main(string args)
using (SPSite site = new SPSite("http://localhost"))
using (SPWeb web = site.OpenWeb())
SPList sourceList = web.Lists["Tasks 1"];
Guid destGuid = web.Lists.Add("Tasks 2", sourceList.Description, sourceList.BaseTemplate);
SPList destList = web.Lists[destGuid];
destList.OnQuickLaunch = sourceList.OnQuickLaunch;
The above code performs the following:
- Create the SPSite & SPWeb objects
- Access the Tasks 1 list
- Create a new Tasks 2 list using the Template of first list
- Set the Quick Launch property to true
- Update the new List
- Disposes the SPSite & SPWeb objects
View the new List created.
Now you can go back to the SharePoint site and see the new list was created.
The SPList server object model contains the BaseTemplate instance property as the List Template. We are creating the new list using this property.
web.Lists.Add("Tasks 2", sourceList.Description, sourceList.BaseTemplate);
In this article we have explored the Server Object Model of creating List based on another List. The source code is attached along with this article.