Save List as Template in SharePoint Programmatically

Below is the function to save as template in sharepoint site programmatically.

Parameters

strSiteURL: send the site url in this parameter

strListName:send the ListName of which template you want to create.

public void CreateTemplate(string strSiteURL,string strListName)

{

SPSecurity.RunWithElevatedPrivileges(delegate

{

using (SPSite oSPsite = new SPSite(strSiteURL))

{

oSPsite.AllowUnsafeUpdates = true;

using (SPWeb oSPWeb = oSPsite.OpenWeb())

{

oSPWeb.AllowUnsafeUpdates = true;

 

SPList list = oSPWeb.Lists[strListName];

string strbkpTemplateName = "bkp" + strListName + ".stp";

list.SaveAsTemplate(strbkpTemplateName, strbkpTemplateName.Replace(".stp",""), "Description", true);

 

list.Update();

oSPWeb.AllowUnsafeUpdates = false;

}

oSPsite.AllowUnsafeUpdates = false;

}

});

}