Skip to content

Instantly share code, notes, and snippets.

@sohelsd
Last active January 11, 2016 23:44
Show Gist options
  • Save sohelsd/72d8a77e5850bf030ce2 to your computer and use it in GitHub Desktop.
Save sohelsd/72d8a77e5850bf030ce2 to your computer and use it in GitHub Desktop.
Sitecore Export Content Tree to Excel
<%@ WebHandler Language="C#" CodeBehind="ExportHandler.ashx.cs" Class="HugeIncSitecore.Utilities.ExportHandler" %>
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Data;
using Sitecore;
using Sitecore.Data.Items;
namespace HugeIncSitecore.Utilities
{
public static class Export
{
public static string ContentCsv(string homeGuid)
{
StringBuilder sb = new StringBuilder();
List<string> columns = new List<string> { "ID", "Name", "TemplateIDs", "TemplateNames", "RenderingIDs", "RenderingNames" };
sb.Append(string.Join(",", columns.ToArray())).Append("\n");
var root = Sitecore.Context.Database.GetItem(homeGuid);
if (root == null) return sb.ToString();
sb.Append(JoinItemProperties(root));
List<Item> sitecoreProducts = root.Axes.GetDescendants().Where(x => x.Visualization.GetLayout(Sitecore.Context.Device) != null).ToList();
var rows = new List<string>();
foreach (var item in sitecoreProducts)
{
rows.Add(JoinItemProperties(item));
}
sb.Append(string.Join("\n", rows.ToArray()));
return sb.ToString();
}
private static string JoinItemProperties(Item item)
{
var row = new List<string>();
if (item != null)
{
row.Add(item.ID.ToString());
row.Add(item.Name);
row.Add(item.Template.ID.ToString() + "|" + string.Join("|", item.Template.BaseTemplates.Select(i=> i != null ? i.ID.ToString() : "Null")));
row.Add(item.Template.Name + "|" + string.Join("|", item.Template.BaseTemplates.Select(i => i != null ? i.Name : "Null")));
row.Add(string.Join("|",item.Visualization.GetRenderings(Sitecore.Context.Device, false).Select(i => i != null ? i.RenderingID.ToString() : "Null")));
row.Add(string.Join("|", item.Visualization.GetRenderings(Sitecore.Context.Device, false).Select(i => i == null ? "Null" : i.RenderingItem == null ? "Null" : i.RenderingItem.Name)));
}
return string.Join(",", row.ToArray());
}
}
}
using System;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
namespace HugeIncSitecore.Utilities
{
/// <summary>
/// Summary description for Export
/// </summary>
[WebService(Namespace = "http://www.site.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ExportHandler : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var _method = String.Empty;
if (!String.IsNullOrEmpty(context.Request.QueryString["m"]))
{
_method = Convert.ToString(context.Request.QueryString["m"]).ToUpper();
}
if (_method.Length == 0)
{
return;
}
var methods = typeof(ExportHandler).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var method in from method in methods let attributes = Attribute.GetCustomAttributes(method) where attributes.Where(attribute => attribute != null).OfType<WebServiceMethodAttribute>().Where(attribute => (attribute).Name != null).Any(attribute => (attribute).Name.ToUpper() == _method) select method)
{
context.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + _method + DateTime.Now.ToString("MMddyyHHmm") + ".csv");
context.Response.Write(method.Invoke(this, new object[] { context }));
context.Response.End();
return;
}
}
public bool IsReusable
{
get
{
return false;
}
}
[WebServiceMethod(Name = "content")]
private string ContentCsv(HttpContext context)
{
var rootItem = context.Request.QueryString["root"];
return HugeIncSitecore.Utilities.Export.ContentCsv(rootItem);
}
#region Attributes
public class WebServiceMethodAttribute : System.Attribute
{
#region Constants and Fields
public string Name;
#endregion
#region Constructors and Destructors
public WebServiceMethodAttribute(string name)
{
Name = name;
}
public WebServiceMethodAttribute()
{
}
#endregion
}
#endregion
}
}
Comma separated CSV file. ID, Name, Templates, Renderings
Example:
{34E3F128-5376-403B-8E58-1C1EA827BCAF}, Home, {55C00429-72BF-423F-9328-187869F2D18E}|{4E932877-1DEC-4F19-AE1A-6589AF9A4694}, {508C9A5F-BBD7-4E31-8C94-1B95B9321F96}|{C4E9416D-675F-4A33-8672-D5A876107BF8}|{1FA9049B-01AE-47F2-9A57-8B8BDF269763}|{9338BC5E-28F5-4AD8-9602-86F26BE26755}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}
{44CA18AB-8EE6-4336-A282-CB3A17BC4790}, About, {40C39ABE-4C93-4571-B51F-ED3F2B121798}|{19E5A981-E9FD-4FFF-8B8D-97736962CEB8}, {508C9A5F-BBD7-4E31-8C94-1B95B9321F96}|{C4E9416D-675F-4A33-8672-D5A876107BF8}|{76058933-9204-4FD8-8C09-D59CDAD4E808}|{EE789B35-1FE3-4E56-A292-9116A41DFA2F}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}|{980D4AC6-5B8B-4C42-8833-B5A7B78AD22A}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment