Skip to content

Instantly share code, notes, and snippets.

Avatar

Eric Stafford 5up3rman

View GitHub Profile
@5up3rman
5up3rman / StringUtil.cs
Last active Sep 13, 2017
String Utilities
View StringUtil.cs
using Sitecore.Collections;
using Sitecore.Diagnostics;
using Sitecore.Reflection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@5up3rman
5up3rman / ParseQueryString.cs
Created Sep 13, 2017
Web Utility Parse Query String
View ParseQueryString.cs
var values = WebUtil.ParseQueryString("print=1&target=me&group=first");
var s0 = values["print"]; // "1";
var s1 = values["target"]; // "me";
var s2 = values["group"]; // "first";
var s3 = values["non-existing"]; // null;
@5up3rman
5up3rman / AddQueryString.cs
Created Sep 13, 2017
Web Utility - Add Query String
View AddQueryString.cs
var url1 = WebUtil.AddQueryString("http://www.mysite.net", "print", "1");
// Result: "http://www.mysite.net?print=1"
var url2 = WebUtil.AddQueryString("http://www.mysite.net", "print", "1", "lg", "print");
// Result: "http://www.mysite.net?print=1&lg=print"
View WebUtil.cs
using HtmlAgilityPack;
using Sitecore.Caching;
using Sitecore.Collections;
using Sitecore.Configuration;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.IO;
using Sitecore.Layouts;
using Sitecore.Links;
@5up3rman
5up3rman / GetQueryString.cs
Last active Sep 12, 2017
Web Utilities Get Query String Example
View GetQueryString.cs
// Returns QS or Empty String
var qsWithKey = WebUtil.GetQueryString("key");
// Returns QS or Default Value
var qsWithKeyAndDefaultValue = WebUtil.GetQueryString("key", "master");
// Returns Encoded QS or Empty String
var safeQs = WebUtil.GetSafeQueryString("key");
@5up3rman
5up3rman / OldItemRenamer.cs
Created Aug 31, 2017
An old Item Renamer from 2011. Very flawed.
View OldItemRenamer.cs
public class EnforceItemName
{
protected void OnItemSaved(object sender, EventArgs args)
{
var currentItem = Sitecore.Events.SitecoreEventArgs.GetItem(args, 0);
if (currentItem != null && currentItem.Name.Contains(" ") && (currentItem.Paths.IsContentItem || currentItem.Paths.IsMediaItem))
{
currentItem.Editing.BeginEdit();
@5up3rman
5up3rman / RegexItemUtilStringUtil.cs
Last active Aug 30, 2017
Regex + ItemUtil + StringUtil
View RegexItemUtilStringUtil.cs
static void Main(string[] args)
{
var wildItemName = "<strong class=\"color-me-red\">Name of a Product<sup>™</sup> Version 1!!!!!</strong>";
Console.WriteLine(ToValidItemName(wildItemName));
Console.WriteLine(ToValidItemName(wildItemName, true, true));
Console.ReadKey();
}
public static string ToValidItemName(string str, bool toLower = false, bool replaceSpacesWithDashes = false)
View ProposeValidItemName.cs
static void Main(string[] args)
{
var wildItemName = "<strong>Name of a Product<sup>™</sup> Version 1</strong>";
var notEntirelyFixed = ItemUtil.ProposeValidItemName(wildItemName);
Console.WriteLine(notEntirelyFixed);
Console.ReadKey();
}
// Result: strongName of a Productsupsup Version 1strong
@5up3rman
5up3rman / FlawedItemRenaming1.cs
Last active Aug 30, 2017
Flawed Item Renaming - Really Basic Regex
View FlawedItemRenaming1.cs
static void Main(string[] args)
{
var wildItemName = "<strong>Name of a Product<sup>™</sup>Version 1</strong>";
var notEntirelyFixed =
Regex.Replace(wildItemName, @"[^0-9a-zA-Z]+", " ").Trim();
Console.WriteLine(notEntirelyFixed);
Console.ReadKey();
// Result: strong Name of a Product sup sup Version 1 strong
}
@5up3rman
5up3rman / DisplayAdditionalItemInformation.cs
Last active Jul 21, 2017
Display Additional Item Information
View DisplayAdditionalItemInformation.cs
namespace EETK.Foundation.Toolkit.Rules.Actions
{
public class DisplayAdditionalItemInformation<T> : RuleAction<T> where T : CustomSectionsRuleContext
{
public override void Apply(T ruleContext)
{
var editorSection = ruleContext.EditorSection.FirstOrDefault(x => x.DisplayAdditionalItemInformation) ?? new EditorSection();
editorSection.Title = Translate.Text("Additional Item Information");
editorSection.DisplayAdditionalItemInformation = true;