.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Create a List in C# based on another List

Posted By:Jean Paul       Posted Date: November 30, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we will explore the Server Object Model of creating List based on another List
 

We can create copy of a List from one SharePoint site to another using List Template Exporting & PowerShell. In this article we can see the Server Object Model way of achieving the same.

Classes Involved

The SPList class is the core type involved in our functionality.

Activities Involved

Following are the activities involved in our application.

Step 1: Create a List in SharePoint site based on the Tasks template and name it as Tasks 1.

 

Step 2: Create a console application

Add reference to the Microsoft.SharePoint assembly

clip_image003

Set the Build > Platform Taget to Any CPU

clip_image004

Step 3: Add the following code

In the Program.cs add the following code.

 

class Program
{
    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;
                destList.Update();
            }
        }
    }
}

The above code performs the following:

1. Create the SPSite & SPWeb objects

2. Access the Tasks 1 list

3. Create a new Tasks 2 list using the Template of first list

4. Set the Quick Launch property to true

5. Update the new List

6. Disposes the SPSite & SPWeb objects

Step 4: View the new List created

Now you can go back to the SharePoint site and see the new list was created.

clip_image008

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);

References

http://tinyurl.com/sp2010-crlst

Summary

In this article we have explored the Server Object Model of creating List based on another List


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend