Pages

Jan 27, 2011

Useful SPUtility tips and tricks

Here are many useful functions in SPUtility class that you may want to use in your code. When I was looking at how my custom solutions are going to be migrated to SharePoint 2010 – I noticed few obvious problems and the most obvious of them was the fact that some of the resources (3rd party license files etc) I reference by hard-coded path. Well, SPUtility has GetGenericSetupPath, a function that prefixes passed path parameter with current12 hive location. Pretty usefull if all of the sudden your code is going to run under 14 hive.

One other useful method, GetAllAuthenticatedUsers allows you to enumerate authenticated users allowed to access the web. One of the most common uses is to show list of users on a particular web as `the team` in case of a project site. The syntax is pretty basic:

SPWeb thisWeb = SPControl.GetContextWeb(Context);

string allUsers;

allUsers = SPUtility.GetAllAuthenticatedUsers(thisWeb);

writer.Write(allUsers);

SPWeb thisWeb = SPControl.GetContextWeb(Context);

string allUsers;

allUsers = SPUtility.GetAllAuthenticatedUsers(thisWeb);

writer.Write(allUsers);


Here is another useful method, EnsureSiteAdminAccess , in some cases the piece of the functionality you intend to use on the site requires administrative privileges. For example, you created an `admin` site on your portal where all of the administrative lists and other controls are available. By using EnsureSiteAdminAccess you can enforce admin access and automatically prompt a logic screen if user needs to be elevated.

I definitely recommend taking a look and making a mental note of some of the functionality available here

SPUtility : Useful Functions

To access the object, you need to add the following using statement:

using Microsoft.SharePoint.Utilities;

Following are some of the more useful functions this object provides

SPUtility.FormatDate

Allows you to format a given date to any of the SPDateFormat types
DateTime curDate = DateTime.Now();
DateTime regionDate = web.RegionalSettings.TimeZone.UTCToLocalTime
(web.ParentWeb.RegionalSettings.TimeZone.LocalTimeToUTC(curDate));
return Convert.ToDateTime(SPUtility.FormatDate(web, regionDate, SPDateFormat.ISO8601));

Get the 12-Hive filesystem path

Returns the filesystem path for the 12-Hive, or any of the folders beneath it. This is typically
(though not always) going to be C:\Program Files\Common Files\Microsoft Shared\web server extensions\12

string featurePath = SPUtility.GetGenericSetupPath("template\\features");

Get Full (absolute) URL

Converts a relative Url into an absolute Url
//Get url to list item
SPListItem item = spList.Items[1];
string itemUrl = SPUtility.GetFullUrl(spSite, item.Url);

Redirect to page

Send a HTTP redirect to the client's browser
//Redirect to specified page, adding querystring
string url = "http://portal/TestResults/Pages/results.aspx";
string queryString = "successflag=passed";

SPUtility.Redirect(url, SPRedirectFlags.Default, Context, queryString);

Send email

Lets you send an email from the context of the given SPWeb
//Send email from current SPWeb
SPWeb web = SPContext.Current.Site.OpenWeb();
string subject = "Email from the " + web.Title + " web";
string body = "The body of the email";
SPUtility.SendEmail(web, false, false, "someone@somewhere.com", subject, body);

Transfer to SharePoint success/error page

Allows you to transfer the browser to the ootb error and success pages
//Transfer to Error Page
SPUtility.TransferToErrorPage(ex.Message);
//Transfer to success page, and specify url to move onto after "Ok" clicked
SPUtility.TransferToSuccessPage("Operation was completed", @"/Docs/default.aspx", "", "");