Export Sharepoint listitem versions
In this code am exporting sharepoint list item versions.
Here is the code below.
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!String.IsNullOrEmpty(Request.QueryString["ItemId"]) && !String.IsNullOrEmpty(Request.QueryString["ListId"]))
{
string itemID = Request.QueryString["ItemId"];
string listID = Request.QueryString["ListId"];
listID = listID.Substring(1, listID.Length - 2);
DataTable dt = new DataTable();
bool firstTime = true;
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
Guid gid = new Guid(listID);
SPList DocLib = web.Lists[gid];
if (DocLib.EnableVersioning == true)
{
foreach (string itemstr in itemID.Split('|'))
{
if (!String.IsNullOrEmpty(itemstr))
{
SPListItem item = DocLib.GetItemById(Convert.ToInt32(itemstr));
SPListItemVersionCollection versionCollection = item.Versions;
if (versionCollection.Count > 0)
{
foreach (SPListItemVersion versionItem in versionCollection)
{
if (firstTime == true)
{
foreach (SPField fieldName in versionItem.Fields)
{
dt.Columns.Add(fieldName.StaticName);
}
firstTime = false;
}
DataRow dr = dt.NewRow();
foreach (SPField fieldValue in versionItem.Fields)
{
dr[fieldValue.StaticName] = versionItem[fieldValue.StaticName];
}
dt.Rows.Add(dr);
}
}
}
}
ExporToExcel(dt);
}
if(!String.IsNullOrEmpty(Request.QueryString["Purl"]))
{
Response.Redirect(Request.QueryString["Purl"]);
}
else
{
Response.Redirect(web.Lists[gid].DefaultViewUrl);
}
}
}
}
}
catch (Exception ex)
{
}
}
private void ExporToExcel(DataTable dt)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Versions.xls");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
"borderColor='#000000' cellSpacing='0' cellPadding='0' " +
"style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
int columnscount = dt.Columns.Count;
for (int j = 0; j < columnscount; j++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write("<B>");
HttpContext.Current.Response.Write(dt.Columns[j].ColumnName);
HttpContext.Current.Response.Write("</B>");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
foreach (DataRow row in dt.Rows)
{
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < dt.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
HttpContext.Current.Response.Write("</font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
Here is the code below.
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!String.IsNullOrEmpty(Request.QueryString["ItemId"]) && !String.IsNullOrEmpty(Request.QueryString["ListId"]))
{
string itemID = Request.QueryString["ItemId"];
string listID = Request.QueryString["ListId"];
listID = listID.Substring(1, listID.Length - 2);
DataTable dt = new DataTable();
bool firstTime = true;
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
Guid gid = new Guid(listID);
SPList DocLib = web.Lists[gid];
if (DocLib.EnableVersioning == true)
{
foreach (string itemstr in itemID.Split('|'))
{
if (!String.IsNullOrEmpty(itemstr))
{
SPListItem item = DocLib.GetItemById(Convert.ToInt32(itemstr));
SPListItemVersionCollection versionCollection = item.Versions;
if (versionCollection.Count > 0)
{
foreach (SPListItemVersion versionItem in versionCollection)
{
if (firstTime == true)
{
foreach (SPField fieldName in versionItem.Fields)
{
dt.Columns.Add(fieldName.StaticName);
}
firstTime = false;
}
DataRow dr = dt.NewRow();
foreach (SPField fieldValue in versionItem.Fields)
{
dr[fieldValue.StaticName] = versionItem[fieldValue.StaticName];
}
dt.Rows.Add(dr);
}
}
}
}
ExporToExcel(dt);
}
if(!String.IsNullOrEmpty(Request.QueryString["Purl"]))
{
Response.Redirect(Request.QueryString["Purl"]);
}
else
{
Response.Redirect(web.Lists[gid].DefaultViewUrl);
}
}
}
}
}
catch (Exception ex)
{
}
}
private void ExporToExcel(DataTable dt)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Versions.xls");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
"borderColor='#000000' cellSpacing='0' cellPadding='0' " +
"style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
int columnscount = dt.Columns.Count;
for (int j = 0; j < columnscount; j++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write("<B>");
HttpContext.Current.Response.Write(dt.Columns[j].ColumnName);
HttpContext.Current.Response.Write("</B>");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
foreach (DataRow row in dt.Rows)
{
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < dt.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
HttpContext.Current.Response.Write("</font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
Comments