Please upgrade your web browser now. Internet Explorer 6 is no longer supported.
Zac Smith
SharePoint, WSS and MOSS development.

Programming in WSS - Use SPUtility

by Zac Smith 30-Apr-07, 7 Comments

The more I work with SharePoint the more I realise that it's not just a product; it's a framework for web development. The static SPUtility class provides a number of helper methods that are very useful when doing any SharePoint development. I use this class at least once in every piece of code I write these days.

In order to use it, just include the following namespace and you're away.
using Microsoft.SharePoint.Utilities;

Here are a couple of examples of how it can be used.

  1. Send Email.
    This is handy as it uses the outgoing email settings that you configure in Central Admin.

     // get web reference
    
SPWeb web = SPContext.Current.Site.OpenWeb();

// email subject
string subject = "Message from " + web.Title;

      // construct message body
      string body = "Hello from SharePoint";

           // send email 
     
SPUtility.SendEmail(web, false, false, "someone@somewhere.com", subject, body);

        2. Get a 12 Hive directory path.
Previous to discovering this method I was hard coding in the path.

     // get path to features directory
     string featurePath = SPUtility.GetGenericSetupPath("template\\features");

    // returns C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES

        3. Redirect to the SharePoint Error Page.
            This is a nice way of handling errors cleanly and consistently.

    try 
   

        // some code that might fail
    } 
   
catch (SPException ex)
    { 
        SPUtility.TransferToErrorPage("An error occurred.\n" + ex.Message);
    }

        4. Redirect to any page.
            I find this really handy as you can provide flags that let you redirect relative to the layouts directory (if you have some custom layout pages)

    // send user to login page
    SPUtility.Redirect("login.aspx", SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);

I tend to live by the theory that the less code you write yourself, the more stable your application will be. The SPUtility class is great in that respect. There are heaps of other useful things you can do with this class, I'm sure that I have only just scratched the surface.

Categories:
7 responses so far:
  • Sunday, 31 May 2009 08:19 by Teresa
    When I use SPUtility.TransferToErrorPage, I get the Error page, but the message is "An Error has occurred on the server". Have you seen this before?
  • Sunday, 31 May 2009 08:19 by chandrika
    Hi Ayman, even iam working on same concept,i want to redirect to error page when exception occurs in workflow. iam using SPUtility.TransferToErrorPage("An error occurred.\n" + ex.Message); but its not working. did you solve the issue? please help if u have solved this.
  • Sunday, 31 May 2009 08:19 by chandrika
    Hi Ayman, even iam working on same concept,i want to redirect to error page when exception occurs in workflow. iam using SPUtility.TransferToErrorPage("An error occurred.\n" + ex.Message); but its not working. did you solve the issue? please help if u have solved this.
  • Sunday, 31 May 2009 08:19 by chandrika
    hi Zac Smith, as you have said use event handler.i am using the itemupdating event handler.i will explain u the process. on workflow activated i am creating a custom column and setting its value to zero by default when an exception occurs iam trying to update that value just to call the updating event and when that event is fired then iam cancelling the event by using properties.cancel=true and setting the error message.this works when iam updating the value manually that is going to the library and editing the valued of the custom column but when i am trying to do the same thing through code then event is not redirecting to error page. i have understood the problem also:as we are trying to update the custom column value in workflow code error message is passed to workflow and not redirected to error page. please help if have solved this issue.
  • Sunday, 31 May 2009 08:19 by Daniel
    thx 4 SPUtility.GetGenericSetupPath(); I'm in time need and this is very helpfully. I can finally my project part now :) regards Daniel
  • Sunday, 31 May 2009 08:19 by Ayman
    Hello, How can I use the SPUtility from a sharepoint workflow to redirect to another page. Ayman
  • Sunday, 31 May 2009 08:19 by Zac Smith
    I'm not sure this is possible as there is no HttpContext ie the workflow runs outside the web context. This probably wouldn't be very reliable anyway as you can't be guarenteed that the workflow will run straight away. Maybe an event handler would work better?

 

Post a Comment:
Name:
URL:
Email:
Comments: