Dialog boxes and long running operations
Just a quick little thing I discovered about the modal dialog boxes in SharePoint 2010 and how you can use a SPLongOperation within it. For those who don’t know, using SPLongOperation is a great way to get SharePoint to throw up a little loading screen while you run some code that will take a while to run (that way they know something is happening still), but if you want to use it in a dialog box you do things a tiny bit differently.
A basic SPLongOperation will look like this in code:
using (var operation = new SPLongOperation(Page))
{
operation.LeadingHTML = "Updating SMS project";
operation.TrailingHTML = "Please wait while additional tasks are added to the project plan";
operation.Begin();
// do your long running code here
operation.End("/MyResultPage.aspx", SPRedirectFlags.Default, HttpContext.Current, string.Empty);
}
And if you are doing it in a modal dialog, there isn’t necessarily anything needs to change if it makes sense for the dialog to stay open, just redirect out and you are fine. If you need to close the dialog though there is only one thing that needs to change. Basically you swap the End() method out for an EndScript() method, which instead of redirecting the user will run some JavaScript for you, which you can use to close the dialog if you like. So the new closing line would look like this:
operation.EndScript("window.frameElement.commonModalDialogClose(1, 'My long running op completed!');");
There you have it, hope that helps someone!
Comments
No comments yet. Be the first!