Last active
December 10, 2016 12:11
-
-
Save mguster/a04207ccdf618c84c9a9c79372ba059d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" AutoEventWireup="true" %> | |
<%@ Import Namespace="Sitecore.Sites" %> | |
<%@ Import Namespace="System.Globalization" %> | |
<%@ Import Namespace="Sitecore.Data" %> | |
<%@ Import Namespace="Sitecore.Data.Items" %> | |
<%@ Import Namespace="Sitecore.Configuration" %> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<title>Sitecore Admin Page CSV Export</title> | |
<link rel="shortcut icon" href="/sitecore/images/favicon.ico" /> | |
<link rel="Stylesheet" type="text/css" href="/sitecore/shell/themes/standard/default/WebFramework.css" /> | |
<script type="text/javascript" src="/sitecore/shell/controls/lib/jQuery/jquery.js"></script> | |
<script type="text/javascript" src="/sitecore/shell/controls/lib/jQuery/jquery.watermark.js"></script> | |
<script type="text/javascript" src="/sitecore/shell/controls/webframework/webframework.js"></script> | |
<style> | |
h1 { | |
margin-top:20px; | |
} | |
h3 { | |
margin-top: 10px; | |
color: #0033BC; | |
font-weight: normal; | |
} | |
hr { | |
margin-top: 30px; | |
margin-bottom: 10px; | |
} | |
.summary { | |
color: blue; | |
} | |
.preview { | |
color: green; | |
border: 1px inset; | |
} | |
.buttons{ | |
margin:1em 0; | |
} | |
</style> | |
</head> | |
<body> | |
<form id="ScAdminPageForm" class="wf-container" runat="server"> | |
<div class="wf-content"> | |
<h1>CSV Exporter</h1> | |
<p> | |
Item Path or ID of an item | |
</p> | |
<ul> | |
<li>/sitecore/system</li> | |
<li>{13D6D6C6-C50B-4BBD-B331-2B04F1A58F21}</li> | |
</ul> | |
<div> | |
<asp:TextBox ID="TargetTextBox" runat="server" Placeholder="/sitecore/system" Width="100%"/> | |
</div> | |
<div class="buttons"> | |
<asp:Button ID="ExportButton" runat="server" Text="Export Starten" OnClick="ExportButtonClick" /> | |
<asp:Button ID="PreviewButton" runat="server" Text="Preview" OnClick="PreviewButtonClick" /> | |
</div> | |
<p class="summary"> | |
<asp:Literal ID="SummaryLiteral" runat="server"/> | |
</p> | |
<div class="preview"> | |
<table> | |
<tr> | |
<th>Item Name</th> | |
<th>Display Name</th> | |
<th>Created</th> | |
<th>Created by</th> | |
</tr> | |
<asp:Literal ID="PreviewLiteral" runat="server"/> | |
</table> | |
</div> | |
<hr/> | |
</div> | |
<script runat="server"> | |
/// <summary> | |
/// Page Load event handler. | |
/// </summary> | |
protected override void OnLoad(EventArgs e) | |
{ | |
base.OnLoad(e); | |
CheckSecurityPermission("/sitecore/admin/ScAdminPageCsvExport.aspx{0}"); | |
InitControls(); | |
} | |
/// <summary> | |
/// Checks if the current user has the appropriate permissions to access the admin page. If not user is redirected to the login page. | |
/// </summary> | |
/// <param name="adminPagePath">The admin page path to which the user gets redirected after successful login.</param> | |
protected void CheckSecurityPermission(string adminPagePath) | |
{ | |
if (Sitecore.Context.User.IsAdministrator) | |
{ | |
return; | |
} | |
SiteContext site = Sitecore.Context.Site; | |
string url = (site != null) ? site.LoginPage : string.Empty; | |
string pageUrl = string.Format(CultureInfo.InvariantCulture, adminPagePath, string.IsNullOrEmpty(Request.QueryString.ToString()) ? string.Empty : "?" + Request.QueryString); | |
url += "?returnUrl=" + Server.UrlEncode(pageUrl); | |
if (url.Length > 0) | |
{ | |
Response.Redirect(url, true); | |
} | |
} | |
/// <summary> | |
/// Inizialises the asp controls | |
/// </summary> | |
private void InitControls() | |
{ | |
SummaryLiteral.Text = string.Empty; | |
PreviewLiteral.Text = string.Empty; | |
} | |
private const string CsvDelimiter = ";"; | |
private const string CsvNewLine = "\n\r"; | |
/// <summary> | |
/// Gets executed by the ExportButton OnClick event | |
/// </summary> | |
protected void ExportButtonClick(object sender, EventArgs e) | |
{ | |
try | |
{ | |
List<Item> childItemList = GetChildrenFromTargetItem(TargetTextBox.Text, SummaryLiteral); | |
if (!childItemList.Any()) | |
{ | |
SummaryLiteral.Text = "No Child items for specified target item"; | |
return; | |
} | |
ExportButton.Enabled = false; | |
Response.Clear(); | |
Response.Buffer = true; | |
Response.AddHeader("content-disposition", "attachment;filename=UserExport-" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".csv"); | |
Response.Charset = ""; | |
Response.ContentType = "application/text"; | |
Response.Output.Write(GetCsv(childItemList)); | |
ExportButton.Enabled = true; | |
SummaryLiteral.Text = "Successfully finished export."; | |
Response.Flush(); | |
Response.End(); | |
} | |
catch (System.Threading.ThreadAbortException) | |
{ | |
// exception can be ignored | |
} | |
catch (Exception ex) | |
{ | |
Response.Write(ex.ToString()); | |
SummaryLiteral.Text = "Error occured during export."; | |
} | |
} | |
private string GetCsv(List<Item> itemList) | |
{ | |
if (!itemList.Any()) | |
{ | |
return string.Empty; | |
} | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendFormat("Item Name{0}Display Name{0}Created{0}Created by{1}", CsvDelimiter, CsvNewLine); | |
foreach (Item item in itemList) | |
{ | |
sb.Append(item.Name).Append(CsvDelimiter); | |
sb.Append(item.DisplayName).Append(CsvDelimiter); | |
sb.Append(item.Statistics.Created.ToString("dd.MM.yyyy HH:mm:ss")).Append(CsvDelimiter); | |
sb.Append(item.Statistics.CreatedBy).Append(CsvNewLine); | |
} | |
return sb.ToString(); | |
} | |
/// <summary> | |
/// Gets executed by the PreviewButton OnClick event | |
/// </summary> | |
protected void PreviewButtonClick(object sender, EventArgs e) | |
{ | |
try | |
{ | |
PreviewLiteral.Text = GetPreviewRecordString(GetChildrenFromTargetItem(TargetTextBox.Text, SummaryLiteral)); | |
} | |
catch (System.Threading.ThreadAbortException) | |
{ | |
// exception can be ignored | |
} | |
catch (Exception ex) | |
{ | |
Response.Write(ex.ToString()); | |
} | |
} | |
private static string GetPreviewRecordString(List<Item> itemList) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
foreach (Item item in itemList) | |
{ | |
sb.Append("<tr>"); | |
sb.Append("<td>").Append(item.Name).Append("</td>"); | |
sb.Append("<td>").Append(item.DisplayName).Append("</td>"); | |
sb.Append("<td>").Append(item.Statistics.Created.ToString("dd.MM.yyyy HH:mm:ss")).Append("</td>"); | |
sb.Append("<td>").Append(item.Statistics.CreatedBy).Append("</td>"); | |
sb.Append("</tr>"); | |
} | |
return sb.ToString(); | |
} | |
private List<Item> GetChildrenFromTargetItem(string target, Literal summaryLiteral) | |
{ | |
Item targetItem; | |
if (!GetTargetItemFromPathOrId(target, summaryLiteral, out targetItem)) | |
{ | |
return new List<Item>(); | |
} | |
return targetItem.Children.ToList(); | |
} | |
private bool GetTargetItemFromPathOrId(string targetString, Literal literal, out Item targetItem) | |
{ | |
targetItem = null; | |
if (string.IsNullOrWhiteSpace(targetString)) | |
{ | |
literal.Text = "Please enter a valid target path or ID."; | |
return false; | |
} | |
Database database = Factory.GetDatabase("master"); | |
if (database == null) | |
{ | |
literal.Text = "Master Database couldn't get fetched."; | |
return false; | |
} | |
if (Sitecore.Data.ID.IsID(targetString)) | |
{ | |
targetItem = database.GetItem(new ID(targetString)); | |
} | |
else | |
{ | |
targetItem = database.GetItem(targetString); | |
} | |
if (targetItem != null) | |
{ | |
return true; | |
} | |
literal.Text = string.Format("'{0}' isn't a valid ID or item path. Please enter a valid item path or ID to the target.", targetString); | |
return false; | |
} | |
</script> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment