Constellation.Foundation.Data is a “base library” in the Constellation framework, consisting of “must have” extension methods for Sitecore development. It is a completely standalone class library, and requires no prerequisites other than a reference to Sitecore.Kernel.
Installation
Constellation.Foundation.Data is managed via NuGet.
In Visual Studio, fire up the Package Manager console and install into any of your Sitecore projects:
PM>Â Install-Package Constellation.Foundation.Data
Source code available on GitHub.
Features
Constellation.Foundation.Data includes Item extension methods and String Extension methods.
Item Extension Methods
These methods provide syntax candy for Sitecore.Data.Item
. They are used for a few essential Sitecore development tasks:
- Establishing an Item’s position in the Content Tree
- Establishing the Context of an Item relative to a Site or HttpRequest
- Establishing an Item’s Template ancestry
- Retrieving the Item’s URL in a context-responsible manner.
Methods
ChildrenDerivedFrom
returns an IEnumerable<Item>
of the Children of the supplied Item where each Child implements the Template supplied as an argument.
FindAncestorByTemplateId
Returns the first ancestor of the supplied Item that is derived from the Template identified in the argument.
Overrides support a string or a Sitecore ID for the argument.
FindOrCreateChildItem (Deprecated)
returns an Item that is a child of the supplied Item, where the child’s Name property matches the supplied argument and the child implements the supplied Template. The child Item may be created if missing when accessing this call.
GetAncestorId
Returns the ID of the first ancestor of the supplied Item that is derived from the Template identified in the argument.
GetSite
returns a SiteInfo
instance where the supplied Item can be determined to be a descendant of the Site’s “root” Item.
GetUrl
Returns the Url of the supplied Item using the active LinkProvider, UrlOptions and SiteResolving options. This call should be site and language-safe when called within an HttpRequest. If calling from an out-of-Context routine (a Pipeline Processor, for example) this method should be tested for trustworthiness.
An override of this method supports CustomItemBase, allowing use of Synthesis or Constellation strongly-typed Items.
IsDerivedFrom
Returns a boolean indicating that the supplied Item implements the supplied Template.
Overrides of this method support Template names, TemplateItem instances, or Tempate IDs as the argument.
IsInContextSite
returns a boolean indicating that the supplied Item is a descendant of the Sitecore.Context.Site
‘s “root” Item. Note that this call requires a valid Sitecore.Context
and cannot be used in out-of-context routines.
IsTemplate
Returns a boolean indicating that the supplied Item is actually a Sitecore Template.
TemplateIsDerivedFrom
returns a boolean indicating that the supplied TemplateItem implements the Template argument.
String Extension Methods
Constellation philosophy is dominated by “convention over configuration”. As such Constellation often needs to convert strings between human-legible, file-system compatible, XPath compatible, and C# compatible identifiers. These extension methods provide a central location for these services. The current method collection is focused on taking Sitecore Item or Field names and converting them to C# compatible words.
Normally, one would only access the extension methods that begin with “As” ex: Item.Name.AsInterfaceName()
Methods
AsClassName
returns the string converted into a legal C# class name.
AsInterfaceName
returns the string converted into a legal C# interface name. This includes prefixing with “I”, which should not be supplied.
AsPropertyName
returns the string converted into a legal C# property name.
AsFieldName
returns the string converted into a legal C# “private” variable name.
AsNamespace
returns a single string representing a namespace based upon the IEnumerable<string>
provided, where the first string in the list is the highest point in the namespace.
AsValidWord
returns the string converted into something that could be a C# identifier
AsValidWords
returns an IEnumerable<string>
of valid C# identifiers based upon the supplied IEnumerable<string>
CamelCase
returns a string converted from Human Legible to “Camel Case” or “Pascal Case” where the words are run together and the first word’s first letter is lowercase.
IsIntefaceWord
returns a boolean indicating that the supplied string starts with two uppercase characters in a row, and the first character is “I”, per Microsoft naming conventions for Interfaces.
TitleCase
returns a string converted from Human Legible to “Title Case” suitable for use in Class, Property or Method names.