1. 使用方法
SPLongOperation类是SharePoint提供的一个很好用的类,对于那些执行时间较长的操作,使用SPLongOperation的方法可以提供一个友好的界面。SharePoint本身在创建站点等一些长时间的操作中也使用SPLongOperation.
使用方法也很简单,声明一个SPLongOperation的对象,将需要长时间操作的代码放在Begin和End方法中就行了,注意这个对象实现了IDisposable接口,所以要使用using:
string resultPageUrl = "page url"; //运行结束之后导向的页面地址 using (SPLongOperation operation = new SPLongOperation(this.Page)) { operation.Begin(); //长时间运行的代码 operation.End(resultPageUrl); }
2. 超时问题的解决
虽然SPLongOperation用于处理长时间的操作,但是这个操做在运行过程中是与服务器保持连接的,是会超时的!超时时间定义在SharePoint的layout目录下的web.config文件中,默认的时间是360秒:
因此如果在6分钟之内,操作没有完成,会抛出超时的错误。解决办法是在使用SPLongOperaion的时候,使用Page.Server.ScriptTimeout指定超时时间:
string resultPageUrl = "page url"; //运行结束之后导向的页面地址 Page.Server.ScriptTimeout = 3600; //指定超时时间为3600秒 using (SPLongOperation operation = new SPLongOperation(this.Page)) { operation.Begin(); //长时间运行的代码 operation.End(resultPageUrl); }