Save List as a Template in SharePoint 2010 Programmatically Using VS 2010

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in SharePoint 2010. 
 

Question: What is a List Template?

 

In simple terms "It enables creation of a new copy or new template based on the existing list, where the person who wants to use the list can readily use them with columns and data as shown in existing list".

I think we are now good to go and implement this wonderful concept.

Step 1: Open SharePoint 2010 Central Administration and navigate to aspecific site.

Step 2:
Open up Visual Studio 2010 and create an empty SharePoint project:

 

Output1.jpg
 

 

Step 3: Select "Deploy as a farm solution" and click on the "Finish" button. Now the empty project will be created:

 

Output2.jpg
 

 

Step 4: Add a new visual webpart for that project.

 

Output3.jpg
 

 

Step 5: The complete code of visualwebpart1usercontrol.ascx looks like this:


<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>

<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"

    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"

    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@  Control Language="C#" AutoEventWireup="true" CodeBehind="SaveListWPUserControl.ascx.cs"

    Inherits="SaveListTempApp.SaveListWP.SaveListWPUserControl" %>

<div>

    <center>

        <table>

            <tr>

                <td colspan="2">

                    <asp:label id="Label1" runat="server" text="List Template Creation - SharePoint 2010 Programatically using VS2010"

                        font-bold="true" font-size="Large" font-names="Verdana" forecolor="Maroon"></asp:label>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:label id="Label2" runat="server" text="Please Enter List Name" font-size="Large"

                        font-names="Verdana" font-italic="true"></asp:label>

                </td>

                <td>

                    <asp:textbox id="TextBox1" runat="server"></asp:textbox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:label id="Label3" runat="server" text="Please Enter File Name" font-size="Large"

                        font-names="Verdana" font-italic="true"></asp:label>

                </td>

                <td>

                    <asp:textbox id="TextBox2" runat="server"></asp:textbox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:label id="Label4" runat="server" text="Please Enter Template Name" font-size="Large"

                        font-names="Verdana" font-italic="true"></asp:label>

                </td>

                <td>

                    <asp:textbox id="TextBox3" runat="server"></asp:textbox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:label id="Label6" runat="server" text="Please Enter Template Description" font-size="Large"

                        font-names="Verdana" font-italic="true"></asp:label>

                </td>

                <td>

                    <asp:textbox id="TextBox4" runat="server" textmode="MultiLine" height="57px" width="140px"></asp:textbox>

                </td>

            </tr>

            <tr>

                <td colspan="2">

                    <asp:button id="Button1" runat="server" text="Save as Template" font-names="Verdana"

                        width="166px" backcolor="Orange" font-bold="True" onclick="Button1_Click" />

                </td>

            </tr>

            <tr>

                <td colspan="2">

                    <asp:label id="Label5" runat="server" font-bold="true" font-names="Verdana" forecolor="Maroon"></asp:label>

                </td>

            </tr>

        </table>

    </center>

</div>

Step 6:
The complete code of visualwebpart1usercontrol.ascx.cs looks like this:

 

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

namespace SaveListTempApp.SaveListWP

{

    public partial class SaveListWPUserControl : UserControl

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            TextBox1.Focus();

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text) || string.IsNullOrEmpty(TextBox3.Text) || string.IsNullOrEmpty(TextBox4.Text)

            {

                Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                try

                {

                    SPSite site = new SPSite("http://win-5d57sl2rvt1:9107/");

                    SPWeb web = SPContext.Current.Web;

                    SPList list = web.Lists[TextBox1.Text];

                    list.SaveAsTemplate(TextBox2.Text + ".stp", TextBox3.Text, TextBox4.Text, true);

                    list.Update(); } catch (SPException ex)

                {

                        Response.Write(ex.Message);

                    }

                Label5.Text = TextBox3.Text + " - List Template Created Successfully";

                Label5.ForeColor = System.Drawing.Color.Green;

                TextBox1.Text = string.Empty;

                TextBox2.Text = string.Empty;

                TextBox3.Text = string.Empty;

                TextBox4.Text = string.Empty;

            }

        }

    }

}
 

Step 7: Deploy the solution file and add the created webpart to the SharePoint site.

 

Step 8: The output of the application looks like this:

 

Output4.jpg
 

 

Step 9: The output of the data entering application looks like this:

 

Output5.jpg
 

 

Step 10: The output of the created list template application looks like this:

 

Output6.jpg
 

 

Step 11: The output of the created list template application usage looks like this:

 

Output7.jpg
 

 

 

Step 12: The output of the list template data looks like this:

 

Output8.jpg
 

 

I hope this article is useful for you.

Up Next
    Ebook Download
    View all
    Learn
    View all
    MVC Corporation is consulting and IT services based company.