Microsoft.SharePoint.Utilities namespace [1] contains many useful classes such as SPUtility, SPEncode, SPUrlUtility, etc. As you can imagine, these classes contain many useful utility methods which can save developers lots of time as well as many line of code. In this article, I will introduce two methods in the SPUtility class as a starter. Until I update this article later, please take a look at the SharePoint SDK to see what kind of utility classes and methods the Microsoft.SharePoint.Utilities namespace contains.

 

SPUtility Class

Namespace: Microsoft.SharePoint.Utilities
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)

 

 

SPUtility.CreateISO8601DateTimeFromSystemDateTime

Description

Converts a system DateTime value to ISO8601 DateTime format (yyyy-mm-ddThh:mm:ssZ).

 

User Scenario

When you need to use a CAML query against a field with DateTime type, you will need to format a date to be ISO 8601 compliant. SharePoint provides a SPUtility.CreateISO8601DateTimeFromSystemDateTime method which converts a regular System.DateTime object to a datetime string which follows the ISO 8601 format.

 

C# Code Sample

The following code will return all past due items.

 

SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Tasks"];

DateTime nextWeek = DateTime.Today.AddDays(-1);

string nextWeekDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(nextWeek);
string querystring = String.Format("<Where><Lt><FieldRef Name=\"DueDate\"/><Value Type=\"DateTime\">{0}</Value></Lt></Where>", nextWeekDate);
SPQuery query = new SPQuery();
query.Query = querystring;
SPListItemCollections items = list.GetItems(query);

 

 

 

SPUtility.SendEmail

Description

Sends an email. The key benefit of using this method instead of using the System.Web.Mail.SmtpMail.SendMail the less maintenance -- Since this API uses an outgoing email setting which is configured on the SharePoint central admin, unlike SmtpMail.SendMail, you don't need to store information of SMTP server in a web.config file. The downside of the SPUtility.SendEmail is that it does not support file attachments to emails. If you need a file attachment functionality, you will need to use the SmtpMail.SendMail method.

User Scenario

This method can be used in a variety of scenarios. You may use this method to notify a user by email when a user changes a user profile in an extranet SharePoint site. You may use this method inside a custom SharePoint TimerJob to email users friendly reminders when their assigned tasks are due, etc.

C# Code Sample

SPWeb web = SPContext.Current.Web;

string to = "[recipient's email address]";

string subject = "Test Message";

string body = "Message sent from SharePoint";

bool appendHtmlTag = true;

bool encodeHtml = true;

bool success = SPUtility.SendEmail(web, appendHtmlTag, encodeHtml, to, subject, body);

 

[1] - http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.utilities.aspx