Monday, April 27, 2015

SharePoint – Back to basics – SPLongOperation for lengthy operations

We have many hidden features in SharePoint in look and feel in Out of Box as well as custom. SPLongOperation One of the hidden feature in custom coding side. This feature exists from SharePoint but many of the users may not be aware of this. SPLongOpearation allows us to inform end users on processing screen while operating lengthy operations like moving documents from one site other, deleting sites etc... And at the end we can have options to close/ redirect the page.

Here is the sample code on usage of SPLongOperation,

               try{
                              using (SPLongOperation longOpearation = new SPLongOperation(this.Page))
                              {
                                             longOpearation.LeadingHTML = "Title of the long operation";//title message
                                             longOpearation.TrailingHTML = "this will take few sec”//sub message
                                              longOpearation.Begin();
                                             //Code start
                                             //Write the code that to be processed in long operation
                                             //code ends
                                               HttpContext context = HttpContext.Current;
                                             if (context.Request.QueryString["IsDlg"] != null)
                                             {
                                                            context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                                                            context.Response.Flush();
                                                            context.Response.End();
                                              }
                                              else
                                              {
                                                             string url = SPContext.Current.Web.Url;
 longOpearation.End(url, SPRedirectFlags.CheckUrl, context, string.Empty);
                                             }

                              }
               }
               catch (Exception ex)
              {
                              SPUtility.TransferToErrorPage(ex.ToString());//transfores us to the error page with error message
               }

In the Begin and End methods for long operation we can have different overloaded methods. By using SPLongOperation.Begin method (String, String, SPLongOperation.BeginOperation) method we can pass title and sub message. Here in the script end method, iam passing redirection URL and checking flags and current context. For more details, here is the full description in msdn.



Share this