Using Sitecore.MainUtil – Part 1

Cet article en francais

Sitecore has a relatively unknown helper class which contains lots of cool functions that are very useful for developers. In this article, I will list those functions that are present in Sitecore 10.

Those functions will be later moved to StringUtil and FileUtil classes.

Other utilities classes are:

  • Sitecore.StringUtil
  • Sitecore.IO.FileUtil
  • Sitecore.DateUtil
  • Sitecore.Web.HtmlUtil
  • Sitecore.Web.WebUtil
  • Sitecore.Xml.XmlUtil

WorkingSetSize

Gets the size of the working set

Parameters: none

Returns: The size of the working set

Example:

long setSize = Sitecore.MainUtil.WorkingSetSize

AddArrays

Concatenates two arrays, doing explicit casting

Parameters:

  • Array1: The first array
  • Array2: The second array
  • elementType: The type of the element

Returns: the concatened resulting array

Example:

string[] array1 = "hello world".Split(' ');
        string[] array2 = "Je suis sitecore".Split(' ');
        Array result = Sitecore.MainUtil.AddArrays(array1, array2, typeof(string));

AppendMissingIDBrace

Appends a mssing closing brace “}” if a path starts with a brace “{“

Parameters:

  • sPath: A path

Returns: The path ending with a closing brace “}”

This function is usually called to ensure that a GUID ends with a closing brace “}”.
This can occur if a web page is opened from Outlook by a pasted link as Outlook does not automatically include the ending brace “}”

Example

string path = StringUtil.GetString(Request.QueryString["path"]);
string id = MainUtil.AppendMissingIDBrace(path);

BoolToString

Converts a boolean to “true” or “false”.

Parameters:

  • b: A boolean value

Returns: If the boolean is true, “true”, otherwise “false”

Example

string trueString = MainUtil.BoolToString(1 = 1); // "true"
string falseString = MainUtil.BoolToString(1 = 0); // "false"

BuildHashtable

Easily builds the hashtable from a set of objects

Parameters:

  • objects: an array of objects to build the hashtable with

Returns: A hashtable

Example

Hashtable table = Sitecore.MainUtil.BuildHashtable(new object[] {1, 3, "jj"});

Cast

Casts the specified obj to passed type

Parameters:

  • Typeparam T: The type to cast to.
  • obj: The object
  • type: The type

Returns: Object to specified type

The parameter “type” is actually not used in the implementation

Example

string one = Sitecore.MainUtil.Cast<string>(1, "string");

ClearBit

Resets a bit in an integer

Parameters:

  • bit: The bit
  • bits: The bits

Returns: bits & ~bit;

ColorToString

Converts a color to a string

Parameters:

  • color: the color

Returns: The name of the color, if possible, otherwise a string with the RGB values of the color

Example

string color = MainUtil.ColorToString(Color.Aqua)

CombineArrays

Combines an array and a value.

Parameters:

  • Typeparam: the type of the arrays
  • values: The values
  • value: the value to add to the array

Returns: The resulting array

CombineArrays

Combines two arrays.

Parameters:

  • Typeparam: the type of the arrays
  • values1: The first array of values
  • values2: The second array of values

Returns: The resulting array

CompactArray

Compacts an object array by removing null values.

Parameters:

  • array: An array of objects
  • elementtype: the type of the elements

Returns: The compacted array of object

CompactArray

Compacts a string array by removing empty strings.

Parameters:

  • array: An array of strings

Returns: The compacted array of string

Example

string[] texts = new string[] {"My", "", "Text, ""};
string[] compact = MainUtil.CompactArray(texts): // { "My", "Text" }

ContainsUnicodeCharacters

Determines whether the specified string contains unicode characters.

Parameters:

  • str: The string

Returns: true if the specified string contains unicode characters; otherwise, false

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: