I have a feature that upgrades and updates an existing site to a new and improved version of that site. One of the tasks of the feature is to upgrade existing data in the site. For simple test/dev sites the process is quick enough, but some existing sites have loads of data. The feature activation takes a long time when process all the existing data on the site.

Can I use SPLongOperation in a FeatureActivating-method? What page should I redirect to?

2 Answers
2

Through GUI - in this case you have HttpContext, SPContext and all other stuff ready for you and SPLongOperation will work

By using stsadm or PowerShell - in this case you don't have anything of above and obviously SPLongOperation will not work (DotPeek also shows it depends on HttpContext).

You can determine which scenario you're running simply by comparing HttpContext.Current to null.

For the stsadm/PS scenario, you can consider creating a separate timer job or something like this. For GUI, you are able to use SPLongOperation and redirect to the "Site Features" or "Site Collection Features" page after it finishes, depending of which scope you're using for the feature. The redirection url will be /_layouts/ManageFeatures.aspx?Scope=Web or /_layouts/ManageFeatures.aspx?Scope=Site correspondingly.

P.S. I don't have any real experience of using SPLongOperation from within feature receiver, but I have experience of determining the GUI/stsadm from within feature for some other purposes.

Thanks! It works very nice. I do check if the current context is valid, in order to differentiate between the GUI and PowerShell/stsadm. And i make use of wss.boman.biz/Lists/Posts/Post.aspx?ID=19 to provide feedback about the progress.
– DribbelMay 3 '12 at 10:08

SPLongOperation.Begin(
delegate(SPLongOperation longOperation)
{
// Do something that takes a long time to complete.
ContentTypeMapping CTMapping = new ContentTypeMapping(site);
CTMapping.MapContentTypesToWorkspace(null, web);
// Inform the server that the work is done
// and that the page used to indicate progress
// is no longer needed.
longOperation.End(site.Url);
});