Wednesday, January 25, 2012

SPLongOperation - SharePoint Spin Wheel

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 the LeadingHTML 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

No comments:

Post a Comment