Sunday, September 22, 2013

Sitecore Integrated with Ace Code Editor

Check out my latest video showing you how to configure the EditHtml dialog with the Ace Code Editor!
(function($){
	$('head').append("<style>div[id$=RibbonPanel],textarea[id$=Html] { display: none; } #CodeEditor { width: 100%; height: 100%; } </style>");

	$(function() {
		var html = $('textarea[id$=Html]');
		var ce = $("<div id='CodeEditor' />");
        html.after(ce);

        var codeeditor = ace.edit(ce[0]);
        codeeditor.setTheme("ace/theme/monokai");
        codeeditor.session.setMode("ace/mode/html");
        codeeditor.setShowPrintMargin(false);

        codeeditor.session.setValue(html.val().trim());
        codeeditor.session.on('change', function () {
                html.val(codeeditor.session.getValue());
        });

        ace.config.loadModule("ace/ext/emmet", function () {
            ace.require("ace/lib/net").loadScript("/Scripts/ace/emmet-core/emmet.js", function () {
                codeeditor.setOption("enableEmmet", true);
            });

            codeeditor.setOptions({
                enableSnippets: true,
                enableBasicAutocompletion: true
            });              
        });

        ace.config.loadModule("ace/ext/language_tools", function (module) {
            codeeditor.setOptions({
                enableSnippets: true,
                enableBasicAutocompletion: true
            });
        });
	});
}(jQuery));

Links:
  • http://ace.c9.io
  • https://github.com/ajaxorg/ace-builds/
  • http://michaellwest.blogspot.com

Wednesday, September 11, 2013

Sitecore Generic Shortcodes

I really enjoyed reading Sitecore Junkie's post about using Shortcodes in Sitecore. Here's an example of one I built for generic item shortcodes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Sitecore;
using Sitecore.Data;

namespace Concentra.Web.Configuration.Pipelines.ExpandShortcodes
{
    /// <summary>
    /// Expands shortcodes with the following format:
    /// [item id=110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9 fieldname="Title"]
    /// </summary>
    public class ExpandItemShortcodes : ExpandShortcodesProcessor
    {
        public override IEnumerable<Shortcode> GetShortcodes(string content)
        {
            if (String.IsNullOrWhiteSpace(content))
            {
                return new List<Shortcode>();
            }

            var shortcodes = new List<Shortcode>();
            var matches = Regex.Matches(content, @"\[item id=(?<id>.*) fieldname=(?<fieldname>.*)\]", 
                RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (var match in matches.OfType<Match>().Where(m => m.Success))
            {
                shortcodes.Add(new Shortcode
                               {
                                   Unexpanded = match.Value,
                                   Expanded = GetItem(
                                       match.Groups["id"].Value,
                                       match.Groups["fieldname"].Value)
                               }
                    );
            }

            return shortcodes;
        }

        public string GetItem(string id, string fieldName)
        {
            if (!String.IsNullOrEmpty(id) && ID.IsID(id))
            {
                var item = Context.Database.GetItem(ID.Parse(id));
                if (item != null)
                {
                    var name = fieldName.Replace("\"", String.Empty).Replace("'", String.Empty);
                    if (item.Fields.Any(field => field.Name == name))
                    {
                        return item.Fields[name].Value;
                    }
                }
            }

            return String.Empty;
        }
    }
}

Saturday, September 7, 2013

Sitecore PowerShell Extensions Mixing C# and PowerShell



$code = @"
using System;

namespace AwesomeNamespace {
    public enum AwesomeActivityType {
        Nothing,
        Sleeping,
        Eating,
        UsingSPE
    }
    public class AwesomeClass {
        public string DoSomethingAwesome(AwesomeActivityType activity) {
            return String.Format("Your awesome activity is {0}. That's awesome!", activity);
        }
        
        public static string DoSomethingAwesomeAnytime(){
            return "There, their, the're";
        }
    }
}
"@
Add-Type $code

$awesome = New-Object AwesomeNamespace.AwesomeClass
$awesome.DoSomethingAwesome([AwesomeNamespace.AwesomeActivityType]::UsingSPE)
[AwesomeNamespace.AwesomeClass]::DoSomethingAwesomeAnytime()

Wednesday, September 4, 2013

Sitecore PowerShell Extensions Unlock Items

Below is an example of how to unlock all items under the Content tree.
 
# Find all the items under content recursively, then only return the properties you want. Here we only want items that are locked.
gci master:\content -rec | where { $_.Locking.IsLocked() } | 
    select Name, Id, @{n="IsLocked";e={$_.Locking.IsLocked()}}
 

The following aliases or shortened commands were used:
  • gci = Get-ChildItem
  • where = Where-Object
  • select = Select-Object
Note: To autosize the table pipe the output to the following command: ft -auto
 
# Unlock all the items.
gci master:\content -rec | where { $_.Locking.IsLocked() } | % { $_.Locking.Unlock() }
 

Tuesday, September 3, 2013

Sitecore PowerShell Extensions Kick Users

Here's a quick way to kick users :)
# Use the static class to get the list of sessions then for each session kick the user using the session id.
[Sitecore.Web.Authentication.DomainAccessGuard]::Sessions | 
    % { [Sitecore.Web.Authentication.DomainAccessGuard]::Kick($_.SessionId) }