Magical SPUtility Class – A Hidden Jewel for SharePoint Custom Development

I have been using SPUtility ever since I have started building Custom SharePoint Applications but once in a while I come across SPUtility functions to solve problems which I normally either find myself in the corner or scratch my head to solve  issues.

With this article, I will try to outline some of my favorite SPUtility functions and which scenarios you may be able to use.

SendEmail – Sends an email to specified email address

Sending email from ASP.NET application requires understanding of location of SMTP Server and Ports. Since SharePoint email & SMTP information is configured as Outgoing Email in Central Administration. SPUtility has outstanding function which would use Current Site Collection context to send an email using Outgoing Email configuration.

private void SendEmailResults(string toAddress, string subject, string body)
{
SPUtility.SendEmail(SPContext.Current.Site.RootWeb, false, false, toAddress, subject, body);
}

MapToIcon – Returns the Icon URL for any list item (e.g. document, pages, or list item)

There are times you may need to retrieve Icon for current document or list URL. e.g. while building Custom Search applications, search API won’t return Icon URL as managed property. There is DocIcon crawled property but it would return nothing but extensions of the documents. Thanks to SPUtility MapToIcon function, we have solution which would return IconURL as long as you have access to the current document or list item URL. Thanks to I LIKE SHAREPOINT for this wonderful tip.

private string getIconUrl(string itemUrl)
{
return "/_layouts/images/" + SPUtility.MapToIcon(SPContext.Current.Web, itemUrl, string.Empty, IconSize.Size16);
}

GetLocalizedString – Get Localized String from Resource Files

While building multi-lingual applications, there are times where you would need to access localized label from the Resource Files stored in the 14-Hive Resource or IIS AppResources Folder. Additionally, if you ever deploy resource files in the subfolders (not at the root of Resources folder), SPUtility function works great even your Resource files are stored in sub folders.

private string getLocalizedString(string label)
{
return SPUtility.GetLocalizedString("$Resources:" + label.Trim(), @"Company.Webparts\ResourceFileName", Convert.ToUInt32(System.Globalization.CultureInfo.CurrentUICulture.LCID));
}
Advertisements
This entry was posted in SP2010 Code Snippets. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s