Skip to content

Instantly share code, notes, and snippets.

@jmyrland
Last active December 16, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmyrland/5460547 to your computer and use it in GitHub Desktop.
Save jmyrland/5460547 to your computer and use it in GitHub Desktop.
Episerver 7 - Dynamic page for vomiting all dynamic data store objects

Episerver 7 - Dynamic page for vomiting all dynamic data store objects

Provides functionality for:

  • Exporting dynamic data stores to excel
  • Deleting single dynamic data store objects.

Set the stores you want to list in the stores, for example:

protected static List<object> stores = new List<object>()
    {
        new DynamicDataObjectA(),
        new DynamicDataObjectB()
    };

You can also add translations for the property names in the dictionary called PropertyTranslations, for example:

public static readonly Dictionary<string, string> PropertyTranslations = new Dictionary<string, string>(){
    { "PropertyInDynamicDataObject", "Nicely translated value"}
};
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="store.aspx.cs" Inherits="Example.Web.store" %>
<%@ Import Namespace="EPiServer.Data.Dynamic" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Skjema Data</title>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<style>
.Id { display: none }
</style>
<script type="text/javascript" src="/Static/Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
function exportToExcel(title, $table) {
var uri = 'data:application/vnd.ms-excel,' + encodeURIComponent($table.wrap('<div />').parent().html());
var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = title;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
function deleteRow(storeIndex, itemId) {
if (!confirm("Er du sikker på at du vil slette denne raden?"))
return;
$.ajax({
type: "POST",
url: "store.aspx/DeleteRow",
data: "{ 'storeIndex' : " + storeIndex + ", 'itemId': '" + itemId + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d == "ok") {
return window.location.href = window.location.href;
}
alert(msg.d);
}
});
}
$(document).ready(function() {
$('h1 .btn').click(function(e) {
e.preventDefault();
var $btn = $(this),
$header = $btn.parents('h1'),
$table = $header.next('table');
exportToExcel($header.attr('data-title'), $table);
});
$('.delete.btn').click(function (e) {
e.preventDefault();
var storeIndex = $(this).attr('data-storeId');
var itemId = $(this).attr('data-itemId');
deleteRow(storeIndex, itemId);
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<% for (var i = 0; i < stores.Count; i++)
{
var emptyStoreObject = stores[i];
var properties = emptyStoreObject.GetType().GetProperties();
var store = emptyStoreObject.GetType().GetStore();
var storedObjects = store.LoadAll();
if (storedObjects == null || !storedObjects.Any())
continue;
%>
<h1 data-title="<%= TranslateName(emptyStoreObject.GetType().Name)%>">
<%= TranslateName(emptyStoreObject.GetType().Name)%>
<small>Antall: <%= storedObjects.Count()%> <a href="#" class="btn">Eksporter til Excel</a></small>
</h1>
<table class="table table-bordered table-striped">
<tr>
<% foreach (var propertyInfo in properties) { %>
<th class="<%= propertyInfo.Name %>"><%= TranslateName(propertyInfo.Name)%></th>
<% } %>
<th style="width: 28px;">&nbsp;</th>
</tr>
<% foreach (IDynamicData obj in storedObjects){ %>
<tr>
<% foreach (var propertyInfo in properties)
{ %>
<td class="<%= propertyInfo.Name %>"><%= GetPropertyValue(obj, propertyInfo)%></td>
<% } %>
<td style="text-align: center">
<a class="delete btn btn-mini btn-danger" href="#" data-storeId="<%= i %>" data-itemId="<%= obj.Id %>">
<i class="icon icon-trash icon-white"></i>
</a>
</td>
</tr>
<% } %>
</table>
<% } %>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.Core;
using EPiServer.Data.Dynamic;
namespace Example.Web
{
public partial class store : System.Web.UI.Page
{
protected bool HasAccess
{
get { return EPiServer.Security.PrincipalInfo.HasAdminAccess; }
}
protected static List<object> stores = new List<object>()
{
new DynamicDataObjectA(),
new DynamicDataObjectB()
};
protected void Page_Load(object sender, EventArgs e)
{
if (!HasAccess)
{
Response.Clear();
Response.StatusCode = 403;
Response.End();
}
}
public static readonly Dictionary<string, string> PropertyTranslations = new Dictionary<string, string>(){
{ "CreatedDate", "Dato"},
{ "Name", "Navn"},
{ "Address", "Adresse"},
{ "PostalNumber", "Postnr"},
{ "City", "By"},
{ "Phone", "Telefon"},
{ "Email", "E-post"},
{ "Comments", "Kommentar"},
{ "Dealer", "Forhandler"},
{ "Company", "Bedrift"},
{ "County", "Fylke"},
};
protected string GetPropertyValue(object obj, PropertyInfo propertyInfo)
{
var value = obj.GetType().GetProperty(propertyInfo.Name).GetValue(obj, null);
if (value == null)
return string.Empty;
string res = value.ToString();
var pageReference = value as PageReference;
if (pageReference != null)
{
var page = EPiServer.DataFactory.Instance.GetPage(pageReference);
res = page.PageName;
}
else if (value is DateTime)
{
var date = (DateTime) value;
res = string.Format("{0:dd/MM/yy H:mm:ss}", date);
}
else if (value is bool)
{
res = (bool) value ? "Ja" : "Nei";
}
return res;
}
protected string TranslateName(string source)
{
return PropertyTranslations.ContainsKey(source) ? PropertyTranslations[source] : string.Join(" ", Regex.Split(source, @"(?<!^)(?=[A-Z])"));
}
[WebMethod]
public static string DeleteRow(int storeIndex, string itemId)
{
if (!EPiServer.Security.PrincipalInfo.HasAdminAccess)
return "No access!";
var store = stores[storeIndex].GetType().GetStore();
var identity = EPiServer.Data.Identity.Parse(itemId);
store.Delete(identity);
return "ok";
}
protected string FormatStoreIdStuff(int i, object o)
{
return string.Format("{0};{1}", i, (o as IDynamicData).Id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment