Monday, December 14, 2009

Windows Service That Starting Workflow by Code every X Time



   1:   
   2:   
   3:   
   4:  using System;
   5:  using System.Collections.Generic;
   6:  using System.ComponentModel;
   7:  using System.Data;
   8:  using System.Diagnostics;
   9:  using System.Linq;
  10:  using System.ServiceProcess;
  11:  using System.Text;
  12:  using System.Configuration;
  13:  using System.Threading;
  14:  using Microsoft.SharePoint;
  15:  using Microsoft.SharePoint.Workflow;
  16:  using System.Collections.Specialized;
  17:  namespace AlertWinService
  18:  {
  19:      public partial class AlertWinService : ServiceBase
  20:      {
  21:   
  22:          #region Class Members
  23:          private bool m_bFirstLoop = true;
  24:          private string m_LogLevel = "";
  25:          public DataTable Mainresultdt = null;
  26:          SPListItem currentListitem = null;
  27:          System.Timers.Timer timer1 = new System.Timers.Timer();
  28:          #endregion
  29:          public AlertWinService()
  30:          {
  31:              InitializeComponent();
  32:          }
  33:   
  34:          
  35:          private string querybuilder(StringCollection andClauses)
  36:          {
  37:              StringBuilder sb = new StringBuilder();
  38:              sb = sb.Append("<Where>");
  39:   
  40:              //only 1 item
  41:              if (andClauses.Count == 1)
  42:              {
  43:   
  44:                  sb = sb.Append(andClauses[0].ToString());
  45:   
  46:              }
  47:              //When the count is 2, we need to add one ‘And’ clause and add both item to be searched for
  48:              //two items
  49:              else if (andClauses.Count == 2)
  50:              {
  51:                  sb = sb.Append("<And>" + andClauses[0].ToString() + andClauses[1].ToString() + "</And>");
  52:              }
  53:              //When the item count is greater than 2, we will add the ‘And’ clauses based on the count – 1.
  54:              else if (andClauses.Count > 2)
  55:              {
  56:                  for (int i = andClauses.Count - 1; i > 0; i--)
  57:                  {
  58:                      sb = sb.Append("<And>");
  59:                  }
  60:                  //After adding the proper number of clauses we will add the first two items and 
  61:                  //close the first ‘And’ clause off. 
  62:                  //Then, as we loop through the remaining items, we add another ‘And’ clause after each 
  63:                  //iteration and close then set.  
  64:                  // If you were to add ‘Or’ clauses, you could do the same.
  65:                  sb = sb.Append(andClauses[0].ToString() + andClauses[1].ToString() + "</And>");
  66:                  for (int j = 2; j < andClauses.Count; j++)
  67:                  {
  68:                      sb = sb.Append(andClauses[j].ToString() + "</And>");
  69:                  }
  70:              }
  71:              //Finally we close the entire set off by adding the closing ‘Where’ clause.
  72:   
  73:              sb = sb.Append("</Where>");
  74:   
  75:              return sb.ToString();
  76:          }
  77:   
  78:          protected override void OnStart(string[] args)
  79:          {
  80:              NetvisionLogger.WriteTolog("onstart");
  81:              SetTimerInfo();
  82:   
  83:   
  84:   
  85:   
  86:          }
  87:   
  88:          private void SetTimerInfo()
  89:          {
  90:              int iTimerIntervalMinutes = 60 * 24; // default is 24 hours interval
  91:              string sTimerIntervalMinutes = string.Empty;
  92:             
  93:              if (m_bFirstLoop)
  94:              {
  95:                  //m_LogLevel = ConfigurationManager.AppSettings["ExportLogLevel"].ToString().ToUpper();
  96:                  sTimerIntervalMinutes = ConfigurationManager.AppSettings["ExportServiceStartDelayInMinutes"];
  97:              }
  98:              else
  99:              {
 100:                  this.timer1.Stop();
 101:                  sTimerIntervalMinutes = ConfigurationManager.AppSettings["ExportTimerIntervalMinutes"];
 102:              }
 103:              
 104:              //    System.Diagnostics.EventLog.WriteEntry("Train_PublicApplicationsExport", "SetTimerInfo - setting timer interval to: " + sTimerIntervalMinutes, EventLogEntryType.Information, 888);
 105:              // to do Update the splist item  
 106:   
 107:   
 108:             NetvisionLogger.WriteTolog("SetTimerInfo - setting timer interval to:" + sTimerIntervalMinutes + " Minutes");
 109:             if (!string.IsNullOrEmpty(sTimerIntervalMinutes))
 110:              {
 111:                  iTimerIntervalMinutes = Convert.ToInt32(sTimerIntervalMinutes);
 112:              }
 113:             this.timer1.Interval = (1000 * 60) * iTimerIntervalMinutes;
 114:              this.timer1.Enabled = true;
 115:              this.timer1.Elapsed+=new System.Timers.ElapsedEventHandler(timer1_Elapsed);
 116:             
 117:              this.timer1.Start();
 118:              
 119:          }
 120:   
 121:          void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 122:          {
 123:              NetvisionLogger.WriteTolog("Alert Windows Service WorkFlow Start");
 124:                     
 125:         
 126:           
 127:   
 128:              // workflow based id 
 129:              /*Guid wfBaseId = new Guid("{32601603-2149-447A-BD73-E64AF9307D6F}"); 
 130:              SPSite site = new SPSite(ConfigurationManager.AppSettings["SharepointSite"].ToString()); 
 131:              SPWeb web = site.OpenWeb(ConfigurationManager.AppSettings["SharepointWeb"].ToString());
 132:              SPList list = web.Lists["genericDocs"]; 
 133:              SPListItem item = list.Items[0];*/
 134:              /*site.AllowUnsafeUpdates = true;
 135:              item["Title"] = "Alert Windows Service";
 136:              item.Update();
 137:              site.AllowUnsafeUpdates = false;*/
 138:   
 139:   
 140:   
 141:              /*SPWorkflowAssociation associationTemplate= list.WorkflowAssociations.GetAssociationByBaseID(wfBaseId); 
 142:              site.WorkflowManager.StartWorkflow(item, associationTemplate, "",true);*/
 143:   
 144:              SPSecurity.RunWithElevatedPrivileges(delegate()
 145:                 {
 146:                     UpdateSharepointDocumentLibrary();
 147:                 });
 148:   
 149:   
 150:          }
 151:   
 152:   
 153:          private void UpdateSharepointDocumentLibrary()
 154:          {
 155:   
 156:              using (SPSite site = new SPSite(ConfigurationManager.AppSettings["SharepointSite"].ToString()))
 157:              { 
 158:              
 159:                  using (SPWeb web= site.OpenWeb(ConfigurationManager.AppSettings["SharepointWeb"].ToString()))
 160:                  {
 161:                     //SPDocumentLibrary doclib= new SPDocumentLibrary();
 162:                      SPFolder doclib = web.Folders[ConfigurationManager.AppSettings["SharepointDocLib"].ToString()];
 163:                      StringCollection andClauses = new StringCollection();
 164:   
 165:                      SPList list = web.Lists["genericDocs"]; 
 166:                      // if therer is a keyword         
 167:                      andClauses.Add("<Contains><FieldRef Name='Title'/><Value Type='Text'>AlertWeb</Value></Contains>");
 168:   
 169:   
 170:   
 171:   
 172:   
 173:   
 174:   
 175:                      SPQuery query = new SPQuery();
 176:                      query.Query = querybuilder(andClauses);
 177:   
 178:                      SPListItemCollection currentlistitemcoll = list.GetItems(query);
 179:                      foreach (SPListItem item in currentlistitemcoll)
 180:                      {
 181:                          currentListitem = item;
 182:                          break;
 183:   
 184:                      }
 185:                      // resultdt
 186:                      Mainresultdt = list.GetItems(query).GetDataTable();
 187:                      Random rr=new Random();
 188:                      site.AllowUnsafeUpdates = true;
 189:                      currentListitem["numberofworkflow"] = rr.Next();
 190:                      currentListitem.Update();
 191:                      site.AllowUnsafeUpdates = false;
 192:   
 193:                  }
 194:              }
 195:          }
 196:   
 197:          void timer1_Tick(object sender, EventArgs e)
 198:          {
 199:             
 200:              /*this.timer1.Stop();*/
 201:              NetvisionLogger.WriteTolog("Tick");
 202:              /*this.timer1.Start();*/
 203:          }
 204:         
 205:          
 206:          protected override void OnStop()
 207:          {
 208:   
 209:              timer1.Stop();
 210:          }
 211:      }
 212:  }
 213:   

0 comments:

Post a Comment