Sitecore Util Classes

February 28, 2011
Tags: Sitecore

If you've ever done a lot of Sitecore development you may have built a lot of tools only to find out that Sitecore itself has a utility that already does what you wanted. After a friend, Mark Ursino, joked about how ridiculous the Sitecore.MainUtil.True() method was I looked around and remarked how many times I bumped into a new found util class. I then asked myself just how many util classes Sitecore had so I popped open the object browser and did a quick search. The number: a staggering 31 utility classes. That's a ton of good code that you don't have to write. Some real nuggets too like the reflector utility. Here's what I found with some method highlights:

Sitecore.Data.Items.ItemUtil
AddFromTemplate(string itemName, string templateName, Item parent)
IsNull(ID id)
IsItemNameValid(string name)
ProposeValidItemName(string name, string defaultValue)

Sitecore.Data.Items.WebDAVItemUtil
IsMediaItem(Item item, string dataFieldName)
IsVersioned(Item item)

Sitecore.Data.Query.QueryUtil (I believe this is an XPath Query Util since it's trying to locate the "[" and "::" characters)
CanResolve(object obj, DataUri uri)
IsQueryPath(string path)

Sitecore.Data.Serialization.CommonUtils
IsDirectoryHidden(string path)
Uniq<t>(List<T> workset, IComparer<T> comparer) (gets the unique objects in a list)

Sitecore.Data.Serialization.ObjectModel.SerializationUtils
IsItemSerialization(string filePath)

Sitecore.Data.Serialization.PathUtils
CreateWatcher()
GetDatabaseRoot(string databaseName)
MakeItemPath(string path, string root)

Sitecore.Data.Sql.SqlUtil
ExecCommand(string sql, object[] parameters, string connection, Timespan timeout)
ExecuteScalar(SqlCommand cmd)
GetFullTextSearchSql(string search, string table, string displayFields, string searchFields)
ObfuscatePassword(string connection)

Sitecore.DateUtil
IsoDateToDateTime(string isoDate, DateTime defaultValue)
ToIsoDate(DateTime datetime, bool includeTicks)

Sitecore.Install.Files.FileKeyUtils (internal)

Sitecore.Install.Files.PathUtils
DemangleDirectoryPaths(string path)
MangleDirectoryPath(string path)
ReducePath(string root, string fullname)

Sitecore.Install.Items.ItemKeyUtils
GetKey(Item item)
Resolve(string key)

Sitecore.Install.PackageUtils
CleanupFileName(string fileName)
GetItemStream(Item item)
TryGetValue<K, V>(IDictionary<K, V> dictionary, K key)

Sitecore.Install.Serialization.IOUtils
LoadSolution(string stream)
StoreSolution(PackageProject solution)

Sitecore.Install.StreamUtil
LoadStream(Stream stream)
Copy(Stream from, Steam to, int bufferSize)

Sitecore.IO.FileUtil
AppendToFile(string filename, string text)
BackupFile(string filename)
IsAspxFile(string filename)
MatchPattern(string value, string pattern)
ReadBinaryFile(string filename)
ReadUTF8File(string filename)

Sitecore.MainUtil
AppendMissingIDBrace(string sPath)
ColorToString(Color color)
ConvertToHashTable(ArrayList list)
EncodeName(string name)
GetDotNetVersion()
GetSingleton<T>(ref T instance, Func<T> createInstance) where T: class
HexToColor(string value)
IsXPath(string path)
SendMail(MailMessage message)
SplitPath(string path, bool expand)
TerminateAspNet(string message)
True() (yeah i can't really believe it, False() either)

Sitecore.Reflection.ReflectionUtil
CallMethod(Type type, object obj, string methodsName,bool includeNonPublic, bool includeInherited, bool includeStatic, object[] parameters)
CreateObjectFromConfig(string configPath, string typeAttribute, object[] parameters)
LoadAssembly(string name)

Sitecore.Reflection.TypeUtil
SizeOfGuid()
SizeOfString(string value)

Sitecore.Security.SecurityUtil
GetUserDigestCredentials(string username, bool withoutDomain)

Sitecore.Security.SecurityUtility
GetAccount(string domainName)
GetAccountNameAndType(string domainName, out string accountName, out AccountType accountType)
GetDomainName()
IsRole(string accountName)
IsUser(string accountName)

Sitecore.Security.Serialization.SecuritySerializationUtils
CleanRoles(List<string> loadedRoles, string domainName)
CleanUsers(List<string loadedUsers, string domainName)

Sitecore.Shell.Applications.WebEdit.WebEditUtil
GetCurrentDate()
SetCurrentDate(DateTime value)

Sitecore.StringUtil
Capitalize(string text)
Combine(object part1, object part2, string divider)
EscapeJavascriptString(string text, bool surroundWithQuotes)
GetNameValues(string[] list)
Repeat(string s, int nCount)

Sitecore.Syndication.FeedUtil
GetFeedDevice()
GetFeedRendering(Itemitem)
IsConfiguredForFeed(Item item)

Sitecore.UIUtil
AddContentDatabaseParameter(UrlBuilder url)
BrowserHeightToDialogHeight(string height)
FindControlByType(Type type)
GetBrowserClassString()
GetItemFromQueryString(Database database)
IsFirefox()
IsIE()
SupportsInlineEditing
UseFlashUpload()

Sitecore.Web.HtmlUtil
CreateAnchor(string text, string href)
FindStartTag(string sTagName, string sText)
GetCheckBox(bool isChecked)
GetRootControl(Control control)
ParseTagAttributes(string tag)
RenderControl(Control ctl)

Sitecore.Web.UI.Grids.GridUtil
GetSelectedValue(string gridID)

Sitecore.Web.WebUtil
AddPageControl(Page page, Control control)
BuildQueryString(SafeDictionary<string> parameters, bool xhtml)
GetCookieDateTime(string key, DateTime defaultValue)
GetHostName()
GetHostIPAddress()
GetIISName()
IsOnAspxPage()
RedirectToErrorPage(string text)

Sitecore.WebDAVUtil
BasedOnFolderTemplate(Item item)
GetBrowseRootItem(Item item)
GetEditFileScript()
GetEditMediaScript(MediaItem item, ID optionsID)
GetWebDAVViewUrl(ID optionID)

Sitecore.WordOCX.WordOcxUtils
GetItemLink(Item item)

Sitecore.Xml.XmlUtil
AddAttribute(string name, string value, XmlNode node)
FindChildWithAttribute(string attribute, string value, XPathNavigator nav)
GetAttribute(int index, XmlNode node)
ReplaceWithChildren(XmlNode node)
TransferAttributes(XmlNode source, XmlNode target)

So there's what I got. It's certainly not an exhaustive list considering there are a whole lot more methods where those came from, but it'll give you a taste of what's out there. Let me know what you find.