SharePoint 2010 Timer Job UpdateProgress

SharePoint 2010 has updated some of its timer job features (see http://pathikhrawal.wordpress.com/2010/08/21/enhancement-in-timer-jobs-in-sharepoint-2010/). Now SPJobDefinition offers an UpdateProgress Method, to show the status of the timer job in a status bar.


UpdateProgress
To check the status of a timer job go to "Central Administration > Monitoring > Check Job Status".

Check Job Status link in the CA


The following code snipplet  is an example of how you could use this method. The job reads a rss feed and uploads some pdf files into a document library. The progress bar is updated by each uploaded file. The snipplet is not complete, because of copyright. I commented out some regions but you can see how you could use the updateprogress function.





public class PressListTimerJob : SPJobDefinition
{
  /// <summary>
  /// We must persist the to have in the execute function
  /// </summary>
  [Persisted]
  private String mRootSiteUrl = String.Empty;        




  public PressListTimerJob() : base() { }


  public PressListTimerJob(string jobName, SPWebApplication webApp, string featureName)
: base(jobName, webApp, null, SPJobLockType.Job)
  {
  using (SPSite rootSite = webApp.Sites[0])
  {
mRootSiteUrl = rootSite.Url;
  }
  }


  public override void Execute(Guid targetInstanceId)
  {            
LoadData();
  }


  private void LoadData()
  {
try
  {
      SaveData();
  }
  catch (Exception ex)
  {
// LOG Exception ...
  }
  }


/// <summary>
/// Writes the data into the SharePoint list
/// </summary>
/// <param name="data">The data to write into the list</param>
  private void SaveData()
  {
  try
  {
  // GET SITE & WEB
using (SPSite site = new SPSite(mRootSiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
// Get list                        
SPFolder pressList = web.Folders[mListName];
Boolean allowupdate = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;                                               
// Get Rss Items
XmlNodeList items = mData.SelectNodes("//item");
WebClient client = new WebClient();


// Create proxy if needed
if (!String.IsNullOrEmpty(mProxyUrl))
client.Proxy = new WebProxy(mProxyUrl);


#region >> Upload files into library                       


int itemCount = items.Count;
int itemCountPercent = itemCount / 100;


if (itemCount > 0)
{
int itemsUploded = 0;
foreach (XmlNode item in items)
{                               
// Get link
if (!String.IsNullOrEmpty(link))
{
// Download pdf file

// If pdf has content upload to library
if (pdfFile.Length > 0)
{
// Upload file
++itemsUploded;
this.UpdateProgress(itemCountPercent * itemsUploded);
}
}
  }
          }
web.AllowUnsafeUpdates = allowupdate;                        
  }
  }
  }
   catch (Exception ex)
  {
  // Log Exceptions
  }
  }
}

Comments

Popular posts from this blog

Ways to redirect http requests in SharePoint

Open SharePoint 2010/2013 Display, Edit, New Forms in Modal Dialogs

How to create a FAQ in SharePoint with OOB features