SharePoint Close methods for SP Long Operations and Redirect Error Page
Example 1 :
try
protected override void OnClick(EventArgs e)
{
try{
using (SPLongOperation longOp = new SPLongOperation(this.Page))
{
longOp.LeadingHTML = "Long Operation Title";
longOp.TrailingHTML = "This may take few seconds.";
longOp.Begin();
//--------------------------
//code for long running operation is here
//---------------------
EndOperation(longOp);
}
}
catch (ThreadAbortException) { /* Thrown when redirected */}
catch (Exception ex)
{
SPUtility.TransferToErrorPage(ex.ToString());
}
}
protected void EndOperation(SPLongOperation operation)
{
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;
operation.End(url, SPRedirectFlags.CheckUrl, context, string.Empty);
}
}
Example 2 :
try
{
SPLongOperation.Begin(
"Calling
WCF Service for adding user into External System",
"Please
wait for this process to complete. This may take a few seconds.",
delegate(SPLongOperation
longOp)
{
try
{
//Code
for calling the WCF service for adding a user into an External System goes here
//
Now end the long operation
if
(Context.Request.QueryString["IsDlg"] != null)
{
longOp.EndScript(String.Format(CultureInfo.InvariantCulture,
"<script
type=\"text/javascript\">window.frameElement.commonModalDialogClose({0},
{1});</script>",
1,
String.Format("\"{0}\"", returnValue)));
}
else
{
string
url =
Context.Request.UrlReferrer.AbsoluteUri;
longOp.End(url,
Microsoft.SharePoint.Utilities.SPRedirectFlags.DoNotEndResponse,HttpContext.Current,"");
}
}
catch
(ThreadAbortException) { /* thrown when
redirected */ }
catch
(Exception ex)
{
string
exMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
string
message = String.Format("Error occurred whilst deleting relationship:
{0}", exMessage);
RedirectToErrorPage(message);
}
});
}
catch (ThreadAbortException) { /* thrown when redirected */ }
catch (Exception ex)
{
string exMessage =
ex.InnerException != null ? ex.InnerException.Message : ex.Message;
string message =
String.Format("Error occurred when calling WCF Service add a user into
External System: {0}", exMessage);
RedirectToErrorPage(message);
}
//The RedirectToErrorPage(message) method
calls the SPUtility.TransferToErrorPage method
private void RedirectToErrorPage(string
message)
{
if
(string.IsNullOrEmpty(this.Referrer))
{
SPUtility.TransferToErrorPage(message);
}
else
{
SPUtility.TransferToErrorPage(String.Concat(message,
" {0}."), "Return to calling Web-Part page.",
this.Referrer);
}
}
Comments
Post a Comment