Skip to content

Instantly share code, notes, and snippets.

View adamhopkinson's full-sized avatar

Adam Hopkinson adamhopkinson

View GitHub Profile
@adamhopkinson
adamhopkinson / gist:7791592
Created December 4, 2013 17:19
Defaulting a sitecore query to a blank list
List<Item> items = (Sitecore.Context.Item.Axes.SelectItems(query) ?? new Item[] { }).ToList();
@adamhopkinson
adamhopkinson / gist:7792041
Created December 4, 2013 17:40
Quick c# list foreach
ids.ForEach(id => {
itineraries.Add(Sitecore.Context.Database.GetItem(id));
});
@adamhopkinson
adamhopkinson / gist:8026627
Last active December 31, 2015 18:28
Getting a Sitecore item url in a C# repeater
<ul>
<asp:Repeater ID="rpFooterDestinations" runat="server">
<ItemTemplate>
<li>
<a href="<%# Sitecore.Links.LinkManager.GetItemUrl(
(Sitecore.Data.Items.Item) Container.DataItem
) %>"><%# Eval("DisplayName") %></a>
</li>
</ItemTemplate>
</asp:Repeater>
@adamhopkinson
adamhopkinson / gist:8036875
Last active August 10, 2017 12:43
C# Globablisation - getting the market instance from the visitors' origin country (via GeoIP)
var market = MarketInfo.Markets.SingleOrDefault(m => m.Code == Visitor.CountryCode);
if (market == null)
market = Visitor.Market;
@adamhopkinson
adamhopkinson / gist:1c5ca5eae193ad72d9dd
Created November 4, 2014 15:51
Accessing Sitecore render parameters
var p = Html.Sitecore().CurrentRendering.Parameters;
bool fullWidth = (p["Full width"] == "1") ? true : false;
string widthClass = (fullWidth) ? "fullwidth" : "indented";
@adamhopkinson
adamhopkinson / gist:2b2421c9eb5ff5ad5514
Last active August 29, 2015 14:14
Using a Sitecore ImageField
ImageField field = context.Fields["Img"];
if (field != null && field.MediaItem != null)
{
var ImgItem = new Sitecore.Data.Items.MediaItem(field.MediaItem);
var ImgUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(ImgItem);
var ImgCaption = field.Alt;
}
@adamhopkinson
adamhopkinson / gist:0ef7bfca02678f12a841
Created June 24, 2015 11:13
Breaking a list into chunks
// define how many items we want per chunk
var itemsPerChunk = 3;
// break the list of items into chunks
var chunks = items.Select((s, i) => new { Value = s, Index = i }).GroupBy(item => item.Index / itemsPerChunk, item => item.Value);
// iterate through each chunk
foreach (var chunk in chunks)
{
<div class="row">
@adamhopkinson
adamhopkinson / gist:d239937c56b29cc1da2e
Last active April 5, 2019 08:58
Converting a TSQL DateTimeOffset to UTC in Excel
/*
* if C2 contains a DateTimeOffset eg 2015-08-13 09:31:12.0000000 +01:00
* parse the date and time out and add them together
* then subtract the hours offset (which - in Excel time storage is hours/24)
* note that this ignores the minute-part of an offset (eg Venezuela)
*/
=DATE(LEFT($C2,4), MID($C2,6,2),MID($C2,9,2)) + TIME(MID($C2,12,2),MID($C2,15,2),MID($C2,18,2)) + (MID($C2,29,3)/24)
@adamhopkinson
adamhopkinson / gist:82b3fec9583946cb5e2d
Created November 16, 2015 14:45
Outputting a table of contents to the console
$('#article-content').find('h1,h2,h3,h4,h5,h6').each(function(i,el) {console.log(i + "\t" + el.tagName + "\t" + $(el).text())})
@adamhopkinson
adamhopkinson / link.cshtml
Created January 12, 2016 14:54
Using a sitecore General Link field