<%@ 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:
Step 9: The output of the data entering application looks like this:
Step 10: The output of the created list template application looks like this:
Step 11: The output of the created list template application usage looks like this:
Step 12: The output of the list template data looks like this:
I hope this article is useful for you.