Sitecore Swamp

Dive in the Sitecore Swamp

NAVIGATION - SEARCH

Frequently used Regex

^(.*)(\r?\n\1)+ 

find duplicated lines in text, to replace with only one instance use \1\

 

\((.+) 

find string which start with (

 

((\bScript\b)|(\bJavascript\b)) 

find lines which has either Script or Javascript

Sitecore MVP nomination ends today!

Want to become Sitecore MVP? Want to go Las Vegas for next year's MVP Summit? Do it now, or have to wait for another year.

You need the nomination and recommendation, and it's closing today! Go to http://www.sitecore.net/mvpnomination. If not already a Sitecore MVP, you also need a Sitecore employee or MVP to recommend you to qualify the nomination. 

I am going to re-nominate for Sitecore MVP 2017, wish me luck! 

Object Exporter - Visual Studio Extension

Has it happened to you that you want see and save a variable value when you doing debug in visual studio. For example, for a particular exception, you want save and share the detailed information, or you have a list of objects too difficult to watch. You can type the variable in immediate command window, and copy/paste the output, or you can use Object Exporter.

Start your debug mode, and make a break point. Now highlight the variable, and tools > Export Objects:


You can choose output format as C# Object Initialization Code, JSON and XML. Pretty cool!

Citrix Shortcuts

Citrix receiver is widely adopt as remote application access by enterprise level IT department. It enables IT deploys security polices, but compared with Microsoft Remote Desktop, it's not that easy to use with (personal feeling). Here're some shortcuts to share:

- Ctrl+F1: Ctrl+Alt+Del

- Shift+F2: exit/back to full screen mode. But, once exits from full screen mode, and back to it, you will lose all the shortcuts. So do not use this one, if you want back to the host.

- Ctrl+Alt+Del: it brings host's quick menu, and now be able to select the application icon from task bar. Click the Citrix icon in task bar to back to Citrix Receiver full screen mode, and the shortcuts still works!

The last one make need do twice if you have dual monitors, not sure if need 3 times for 3 monitors.

Glass Cast an Image Field

Today, I just encounter a scenario that I have to get the Glass Image from a Sitecore Image Field. Here's code to achieve it:

        public static Glass.Mapper.Sc.Fields.Image GlassCast(this ImageField field)
        {
            if (field == null || field.Value.IsNullOrEmpty())
            {
                return null;
            }
                
            var glassImage = new Glass.Mapper.Sc.Fields.Image();
            Glass.Mapper.Sc.DataMappers.SitecoreFieldImageMapper.MapToImage(glassImage, field);
            return glassImage;
        }

Happy coding.

Remove/Replace the line ending

It's quite an old problem that we have to remove the line ending and replace with <br /> somethings for web development. Everything time, I have to google the solution and trail it, and today I am going to note down the final, and no more search for the future.

First, we need understand what's line ending. It has many names: newline, end of line, line break, .. The most common we need to deal with are:

LF: \n, unix world

CR: \r some MacOS

CR+LF: \r\n, Windows

LS: 0x2028, line separator Unicode

PS: 0x2029, paragraph separator Unicode

https://en.wikipedia.org/wiki/Newline

To remove all of above instances:

        public static string ReplaceLineEndings(string value, string replaceWith)

        {

            if (String.IsNullOrEmpty(value))

            {

                return value;

            }


            string pattern = @"\r\n?|[\n\u2028\u2029]";


            return Regex.Replace(value, pattern, replaceWith);

        }

C Sharp Refreshments - Assignment Operator (=)

Coding in C#, if the most frequently typed is ;, the second should =. There's something behind the most simple operator may not known for everyone. Let's start with a simple statement:

var b = 1;

This statement will assign the value 1 to b.

int b;

var a = b = 1;

This statement will assign the value to a and b. 

How about this one:

int b;

var a = ((b = 1) == 1);

Don't question it, this is a valid c# statement, and the value of a is True. Now you may get the sense that "b=1" not only assign the value, and itself has a value. In the official C# reference of = Operator

The assignment operator (=) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result.

Cool, now we know this, so what?




The hiding gem - Sitecore Retryer [TBC]

If you have done long enough, you must noticed there's a retryer for Sitecore data connection to survive from failover for database calls. But can we reuse this retry mechanism for other purpose?

Let's have a peek:


 public interface IRetryable
  {
    /// <summary>Gets the interval.</summary>
    TimeSpan Interval { get; }

    /// <summary>Gets the repeat number.</summary>
    int RepeatNumber { get; }

    /// <summary>Executes the specified action.</summary>
    /// <typeparam name="T">Return type.</typeparam>
    /// <param name="action">The action.</param>
    /// <returns>T object.</returns>
    T Execute<T>(Func<T> action);

    /// <summary>Executes the specified action.</summary>
    /// <typeparam name="T">Return type.</typeparam>
    /// <param name="action">The action.</param>
    /// <param name="recover">The recover.</param>
    /// <returns>T object.</returns>
    T Execute<T>(Func<T> action, Action recover);

    /// <summary>Executes the no result.</summary>
    /// <param name="action">The action.</param>
    void ExecuteNoResult(Action action);

    /// <summary>Executes the no result.</summary>
    /// <param name="action">The action.</param>
    /// <param name="recover">The recover.</param>
    void ExecuteNoResult(Action action, Action recover);
  }

Sitecore has a retryer implemented already: Sitecore.Data.DataProviders.Retryer. It's actually quite good, and serves for generic purpose
- Execute: fire and forget
- Execute and return result (async)

Only one problem, there is only one configuration for this retryer, and you have to share the same with other retryer uses. This is not good.
<!--
RETRYER When enabled, the Retryer resends failed database requests a specified number of times. For example, this is useful if you configure a Sitecore instance to support hot failover for database calls. Default value: disabled="true"
-->
<retryer disabled="true" type="Sitecore.Data.DataProviders.Retryer, Sitecore.Kernel">
<param desc="Number of tries">6</param>
<param desc="Interval between tries">00:00:00.500</param>
<param desc="Log each exception (should be used for debug only)">true</param>
</retryer>

Let's see what we can do.

Factory.GetRetryer() is the static method to get the retryer with the configuration above. How about create another method to allow Factory.GetRetryer(string configName) to use different configuration. 

TBC



Setup Sitecore Dev Box - 2016 July version

  • - Windows 10/Server 2012 R2
  • - Visual Studio (2013 Ultimate, 2015 Enterprise, or 15. Y2K bug is back! lol)
    • - TFS Power Tools
    • - Test .Driven
    • - Sock Rocks
  • - SQL Server 2016. Please note that SSMS is separated for downloading and installation
  • - Resharper, how can I code c sharp without resharper!
  • - Notepad++, the favorite log viewer + config file editor
  • - DotPeek, the 2nd most frequent tool for Sitecore development. (the 1st is VS)
  • - MongoChef
  • - That's it for now.