For those of you that have created applications on the SharePoint platform, you may have some code that takes a while to run in your environment, while the end user is staring at the screen wondering when/if the process is complete. If only you could create a status page that lets the user know their process is running. Maybe you want to find out how to utilize the SharePoint “Operation in Progress” spin wheel. The good news is, there is a class built into the SharePoint object model called SPLongOperation.
I have used this class on various occasions when needing to programmatically create sites and modify properties to various object. In the following example I have a custom Web Part that has a button on it to create a site where I need perform some custom actions.
void _createButton_Click(object sender, EventArgs e)
{
using (SPLongOperation operation = new SPLongOperation(this.Page))
{
operation.LeadingHTML = "Creating Site";
operation.TrailingHTML = "Please wait while the project site is being created for " + siteTitle;
operation.Begin();
SPWeb sub = currentWeb.Webs.Add(siteAbbrev, siteTitle, siteDesc, 1033, "STS#0", false, false);
// Do the rest of my code . . .
operation.End(sub.Url);
}
}
How the SPLongOperation works is you create a new object of type SPLongOperation then you can change the wording that will be displayed on the screen while the process is running by modifying theLeadingHTML and TrailingHTML properties. After you have everything ready just kick off the operation using the Begin method. Once you are in the Begin method, every piece of code you write all happens behind the spin wheel. And once your code has completed, call the End method and you pass a URL that the user will be re-directed to once your code has completed. Here is an example page of what it looks like when calling the SPLongOperation class to wrap your code around.
The good thing about using SPLongOperation is it can remove any issues with users being impatient when they think your code has halted, along with giving a consistent look since SharePoint already uses that spin wheel in various places.
No comments:
Post a Comment