Skip to main content

Weblog Ton Stegeman [MVP]

Go Search
Home
  

ODC 2008
If you have a question or suggestion, please contact me through Windows Live Messenger.
My status: .

If I am not online, please send me an e-mail.
Weblog Ton Stegeman [MVP] > Posts > Redirect in SharePoint using SPUtility.Redirect
Redirect in SharePoint using SPUtility.Redirect

I was looking at the Redirect method in the SPUtility class. Besides the url, this method also takes a parameter of type SPRedirectFlags. I was wondering when to use the values of this enumeration. The MSDN documentation does help a bit, but not very much. I asked on Twitter if anyone knew the meaning of these values. Within minutes, Anders answered and pointed me in the right direction. One of the examples of why I love Twitter!

The signature of the Redirect method is:

public static bool Redirect(string url, SPRedirectFlags flags, HttpContext context)
 
There is an overload that also takes the queryString as a parameter. The SPRedirectFlags enumeration has these values:
  • CheckUrl
  • Default
  • DoNotEncodeUrl
  • DoNotEndResponse
  • RelativeToLayoutsPage
  • RelativeToLocalizedLayoutsPage
  • Static
  • Trusted
  • UseSource

Source url

If UseSource is part of the flags parameter of the Redirect method, the url to which the user is redirected will be read from the querystring of the original request (context parameter). The new url will be the value of one of these querystring parameters:

  • Source
  • NextUsing
  • NextPage

If one of these parameters has a value, the new url will be validated. Validation is done by the IsUrlSafeForRedirect method of the Request property of the current SPWeb. The url in the querystring of the original request needs to be a relative url. See the samples below.

If this url is not valid, or the UseSource parameter resulted in an empty string, the url parameter that was originally passed will be used.

Static url

If Static is part of the flags parameter, the url is considered relative. Depending on the presence of RelativeToLayoutsPage in the flags parameter, the url is relative to the ´_layouts´. If this enumeration value is present, SharePoint checks the flags parameter for the presence of RelativeToLocalizedLayoutsPage. If this is present, a new absolute url to the localized ‘_layouts’ folder is constructed. If not, the url is constructed to the root of the ‘_layouts’ folder. The layouts url is the url of the current SPWeb, followed by ‘_layouts’ and the Language of the current SPWeb. If constructing this url fails for whatever reason, the url will be the url of the current SPSite. If required, the value of SPGlobal.ServerCulture.LCID is added to the url.

Absolute url

If Static is NOT part of the flags parameter, the user will be redirected to the value of the url parameter, after validating the url. If Trusted is part of the flags parameter, the url is always valid. If Trusted is not available, it depends on the outcome of the IsUrlSafeForRedirect method of the Request property of the current SPWeb whether or not the url is valid.

Encoding

The last step is before the user is redirected is the encoding. If DoNotEncodeUrl is NOT present in the flags attribute, the url is first encoded using SPHttpUtility.UrlPathEncode.

Samples

Below you will find a number of samples. Each sample starts with the sample code for SPUtility.Redirect. This code is tested in a web part. The 3 lines below that sample show the results of calling the redirect. The first column contains the page url that contains the web part. The second column contains the result of the redirect.

SPUtility.Redirect(http://newsite, SPRedirectFlags.Default, HttpContext.Current);
http://intranet/site1/Pages/default.aspx no redirect
http://intranet/site1/Pages/default.aspx?Source=http://newsite no redirect
http://intranet/site1/Pages/default.aspx?Source=/news no redirect

 
SPUtility.Redirect("/news", SPRedirectFlags.Default, HttpContext.Current);
http://intranet/site1/Pages/default.aspx http://intranet/news
http://intranet/site1/Pages/default.aspx?Source=http://newsite http://intranet/news
 http://intranet/site1/Pages/default.aspx?Source=/news http://intranet/news

 
SPUtility.Redirect(http://newsite, SPRedirectFlags.Static, HttpContext.Current);
http://intranet/site1/Pages/default.aspx?Source=http://newsite no redirect
http://intranet/site1/Pages/default.aspx http://newsite

 
SPUtility.Redirect(http://newsite, SPRedirectFlags.UseSource, HttpContext.Current);
http://intranet/site1/Pages/default.aspx no redirect
http://intranet/site1/Pages/default.aspx?Source=http://newsite no redirect
http://intranet/site1/Pages/default.aspx?Source=/news http://intranet/news
 

SPUtility.Redirect(http://newsite, SPRedirectFlags.UseSource | SPRedirectFlags.Trusted, HttpContext.Current);
http://intranet/site1/Pages/default.aspx no redirect
http://intranet/site1/Pages/default.aspx?Source=http://newsite http://newsite
http://intranet/site1/Pages/default.aspx?Source=/news http://intranet/news
 
SPUtility.Redirect(http://newsite, SPRedirectFlags.Trusted, HttpContext.Current);
http://intranet/site1/Pages/default.aspx http://newsite
http://intranet/site1/Pages/default.aspx?Source=http://newsite http://newsite
http://intranet/site1/Pages/default.aspx?Source=/news http://newsite
 
SPUtility.Redirect("settings.aspx", SPRedirectFlags.Static | SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
http://intranet/site1/Pages/default.aspx http://intranet/site1_layouts/settings.aspx
http://intranet/site1/Pages/default.aspx?Source=http://newsite http://intranet/site1_layouts/settings.aspx
http://intranet/site1/Pages/default.aspx?Source=/news http://intranet/site1_layouts/settings.aspx

 
SPUtility.Redirect("images/approve.gif", SPRedirectFlags.Static | SPRedirectFlags.RelativeToLayoutsPage | SPRedirectFlags.RelativeToLocalizedLayoutsPage, HttpContext.Current);
http://intranet/site1/Pages/default.aspx http://intranet/site1_layouts/1033/images/approve.gif
http://intranet/site1/Pages/default.aspx?Source=http://newsite http://intranet/site1_layouts/1033/images/approve.gif
http://intranet/site1/Pages/default.aspx?Source=/news http://intranet/site1_layouts/1033/images/approve.gif
 
 
There are a few members of the enumeration that I did not describe, because I was not able to find out how these are used.

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your city *


Type the name of the city you live in (making it easier to handle spam...)

CurrentDate *

Select the current date (see if this gives me fewer spam...)
Attachments

 Links

  SharePoint Object on CodePlex
  Screencast introducing SharePoint Objects
  Content by Type and Filter Web Parts on CodePlex
  Archive
  Archive (Calendar)

 My Latest Blog Posts

Scripting SharePoint 2007 setup: choices and conceptsUse SHIFT+ENTER to open the menu (new window).
Adventures in Visual Studio 2010: Migrate the Content By Type web part to SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Register SharePoint themes by using a featureUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 development on Windows 2008 Server R2 – Getting StartedUse SHIFT+ENTER to open the menu (new window).
New release SharePoint Objects: features and groupsUse SHIFT+ENTER to open the menu (new window).
Constructing the url to the SharePoint Edit Permissions pageUse SHIFT+ENTER to open the menu (new window).
Screencast: introduction to SharePoint ObjectsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 and Reporting ServicesUse SHIFT+ENTER to open the menu (new window).
SharePoint Objects – Insight in usage of your SharePoint artifactsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 Custom list schema and the Content Query Web PartUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsyncUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010, the Client Object Models and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Having fun with SharePoint 2010, Silverlight 3 and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Connecting TFS 2010 projects to SharePoint sitesUse SHIFT+ENTER to open the menu (new window).
Adding a database to the SharePoint database Server using SPDatabaseUse SHIFT+ENTER to open the menu (new window).