Friday, May 30, 2008

How to use SPUtility.TransferToErrorPage and SPUtility.TransferToSuccessPage?

Hi All,

I would like to sharean interesting think about SPUtility.TransferToErrorPage and SPUtility.TransferToSuccessPage.

Let’s take an example. Let us assume that we are working on webpart or any workflow aspx page and we want to navigate to Success Page of sharepoint Or Error Page of sharepoint.

Now if you use following:

SPUtility.TransferToErrorPage("Hi this is an Error Page", "this is link", "{your_url}");

All you can see is this :





Click on the image to view in large scale.

Here observe that "Hi this is an Error Page" is the only option that set there, there is nothing related to the "this is link" and "your_url"

I wonder why, so i went down to MSDN to find out this :

Click Here

But :( didnt find anything helpful as there is no help given. Only thing given is paramters information.

So i digged in that and thought of a String.Format option just like we replaced {0},{1} with paramters passed (Remember Good old console application days !!!)

So i changed the code to :

SPUtility.TransferToErrorPage("Hi this is an Error Page {0}, {1}", "click here to Go to Top site", "{your_url}");

And it Worked. Here is a snapshot which depicts this :


That's it. Your job is done.

But i wonder how SPUtility.TransferToSuccessPage works. It workes fine for its own sake. It will redirect to Following Page :




But here i find hole.

Even if you use simple single overloaded mthod,
SPUtility.TransferToSuccessPage("Hi this is an Sucess Page");

Then also the above results come.

and second overloaded method :

SPUtility.TransferToSuccessPage("Hi this is an Sucess Page {0},{1}", "{url}", "This is Link","{url}");

SPUtility.TransferToSuccessPage("Hi this is an Sucess Page {0},{1},{2}", "{url}", "This is Link","{url}");

don't work fine except it navigates to same above figure , but not having any meaning of other paramters.

If you do come accorss, let me know also.

8 comments:

Svetlana said...

Hi!
Thanks for the post!
I have a question.
My code:
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
SPWeb RubWeb = workflowProperties.Web;
SPListItem currentItem = workflowProperties.Item;

if (SubFold(fold, currentItem) != 0)
{
SPUtility.TransferToErrorPage("error!");
}
else
{
...
}
But the Error page isn't display.
Could you please tell me where is bug?
Thanks!

Malay Vasavada said...

Hi Svetlana,

you have to modify your code.

See, it works like this:
SPUtility.TransferToErrorPage("Hi this is an Error Page {0}, {1}", "click here to Go to Top site", "{your_url}");

This is an Error Page will come on the page and after that First paramter is {0} which will be replaced by First String "click here to go to top", replace this string with whatever you want to write. and other parameter which is {1} will be replaced by {your_url}. replace this {your_url} with the siteURL on which you want to redirect the user.

Thanks,
Malay

Svetlana said...

Thank you for reply! Anyway workflow doesn't work! I have the workflow status "completed".
But redirect to the Error page doesn't happen.

David said...

Hi Malay,

Thanks so much for your information! It helped me a lot. However, your code isn't entirely correct. It turns out the {0} is required but not the {1}. This is true for TransferToSuccessPage also.

This code works great for me:
SPUtility.TransferToSuccessPage("Message. {0}", "/OkButtonURL", "linkText", "/linkURL");

Similarly, this works:
SPUtility.TransferToErrorPage("Error Message. {0}", "link text", "linkURL");

The {0} specifies where the link is placed within the error or success text.

Thanks again for your help!

Anonymous said...

Thank you for your post. This got me in the right direction. Here is what I have found on the parameters for SPUtility.TransferToSuccessPage("Success! {0}", "{url}", "{LinkText}", "{LinkTextUrl}");

{url} = url of the "OK" button redirect

{LinkText} = Text for link diplayed in message

{LinkTextUrl} = Url used for href of {LinkText}

Aroh said...

Hi Malay,

Your post was very informative. I have an OOTB Approval workflow and manipulated the workflow's task list and checks if the user can approve the items.

If the user has Contribute permission, then event handler shows that user can not approve the document.

I am using ItemUpdating code to manipulate the permission.

But, SPUtility.TransferToErrorPage is not working for me. It just skips the break point.


can you please advice.

Thanks.
--aaroh

Aroh said...

My Event Handler:
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
using (SPWeb currentTeamSite = properties.OpenWeb())
{
try
{

this.DisableEventFiring();

// Delcare variables/Objects
bool isApprover = false; //Flag to check permission
string currentUser = string.Empty; //Get current user

HttpContext currentContext;
currentContext = HttpContext.Current;

// Get the current task item
SPListItem currenttaskItem = properties.ListItem;

// Get the value display name of the current logged in user and Created by Field of the current item
currentUser = currenttaskItem["Author"].ToString();
SPUser user = currentTeamSite.AllUsers.GetByID(properties.CurrentUserId);

// Get the Parent list (such as Business Services, Development etc. currentItem is from Task lists)
SPList parentList = currenttaskItem.ParentList.ParentWeb.Lists[new Guid(currenttaskItem[SPBuiltInFieldId.WorkflowListId].ToString())];

// Get the item (document) within folder
SPListItem sourceItem = parentList.GetItemById(int.Parse(currenttaskItem[SPBuiltInFieldId.WorkflowItemId].ToString()));
//SPListItem sourceItem = parentList.GetItemById(int.Parse(currenttaskItem["ID"].ToString()));

// Get the count of all the roles for current user
int rolesForCurrentUser = sourceItem.AllRolesForCurrentUser.Count;

this.EnableEventFiring();

// Check for all roles for current user
if (rolesForCurrentUser != 0)
{
// Get the permission of current user for the "parentList"
foreach (SPRoleDefinition definition in sourceItem.AllRolesForCurrentUser)
{
Console.WriteLine(definition.Name);
if ((definition.BasePermissions & SPBasePermissions.ApproveItems) != 0)
{
isApprover = true;
break;
}
}

// if user is NOT an approver show the error message
if (!isApprover)
{
properties.Cancel = true;
properties.ErrorMessage = "You dont have access to this operation because of your access rights";

string url = properties.WebUrl + @"/_layouts/AccessDenied.aspx";

//SPUtility.Redirect(properties.WebUrl + @"_layouts/AccessDenied.aspx" , SPRedirectFlags.Trusted, _currentContext);
SPUtility.Redirect(url, SPRedirectFlags.Trusted, _currentContext);
SPUtility.TransferToErrorPage("Access Denied here {0}{1}", "Link Text", url);

//HttpContext.Current.Server.Transfer(url);

}
}
}
catch (Exception exception)
{
throw new SPException("An error occured in Approval Workflow");

}
}
}

SharePoint Kings said...

Aroh,

we wonder whether TransferToErrorPage error page is working from event handler... or not

first try with simple list whether its working or not.

if its working then there can be some other scenario like may be workflow will work under elevated privileges or may be Item.Systemupdate() or some other way its updating.

but we are suspicious about the first point that whether its working in event handler or not




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com