using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using System.Windows.Forms;
namespace tzSPStartWorkflow
{
class Program
{
static string surl;
static string sweb;
static string swf;
static string sl;
static string sSource = "tzSPStartWorkflow";
static string sLog = "Application";
static void Main(string[] args) {
try { Execute(args); }
catch (Exception ex) {
if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, ex.Message);
EventLog.WriteEntry(sSource, ex.Message, EventLogEntryType.Error, 100);
Console.WriteLine(ex.Message);
}
}
static void Execute(string[] args)
{
//arguments are separated by space
if (args.Length < 0)
{
Console.WriteLine("Please enter arguments /s=siteurl /w=website /wf=workflowname /l=listname");
}
else {
for (int i = 0; i < args.Length; i++)
{
switch (args[i].Split('=')[0]){
case "/s" :
surl = args[i].Split('=')[1];
break;
case "/w" :
sweb = args[i].Split('=')[1];
break;
case "/wf":
swf = args[i].Split('=')[1];
break;
case "/l" :
sl = args[i].Split('=')[1];
break;
}
}
if (swf != null && sl != null && surl != null)
{
if (sweb != null)
{
using (SPSite spsite = new SPSite(surl))
{
using (SPWeb spweb = spsite.AllWebs[sweb])
{
SPList splist = spweb.Lists[sl];
SPWorkflowManager wfm = spsite.WorkflowManager;
SPWorkflowAssociationCollection wfassc = splist.WorkflowAssociations;
bool bfind = false;
Console.WriteLine(spweb.Url + ": searching for workflow association for " + sl);
foreach (SPWorkflowAssociation wfass in wfassc)
{
Console.Write("workflow association " + wfass.Name);
if (wfass.Name == swf)
{
Console.WriteLine(" activated ");
//MessageBox.Show(wfass.Name);
Console.WriteLine ( sl + " contains " + splist.Items.Count + " item(s)");
foreach (SPItem spitem in splist.Items)
{
SPWorkflow _workflow = wfm.StartWorkflow(spitem, wfass, wfass.AssociationData, SPWorkflowRunOptions.Asynchronous);
Console.WriteLine("Executing workflow for item " + spitem.ID + " " + spitem[21].ToString());
}
bfind = true;
}
else {
Console.WriteLine("disregarded");
}
}
if (!bfind) {
//MessageBox.Show("Rien de trouvé !");
string sinfo = " Workflow " + swf + " for list " + sl + " not found !";
Console.WriteLine(sinfo);
if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sinfo);
EventLog.WriteEntry(sSource, sinfo, EventLogEntryType.Warning, 110 );
}
}
}
}
else {
using (SPSite spsite = new SPSite(surl))
{
using (SPWeb spweb = spsite.RootWeb)
{
SPList splist = spweb.Lists[sl];
SPWorkflowManager wfm = spsite.WorkflowManager;
SPWorkflowAssociationCollection wfassc = splist.WorkflowAssociations;
Console.WriteLine(spweb.Url + ": searching for workflow association for " + sl);
bool bfind = false;
foreach (SPWorkflowAssociation wfass in wfassc)
{
Console.Write("workflow association " + wfass.Name);
if (wfass.Name == swf)
{
Console.WriteLine(" activated ");
Console.WriteLine(sl + " contains " + splist.Items.Count + " item(s)");
foreach (SPItem spitem in splist.Items)
{
wfm.StartWorkflow(spitem, wfass, wfass.AssociationData, SPWorkflowRunOptions.Asynchronous);
Console.WriteLine("Executing workflow For item " + spitem.ID + " " + spitem[21].ToString());
}
bfind = true;
}
else
{
Console.WriteLine("disregarded");
}
}
if (!bfind) {
string sinfo = " Workflow " + swf + " for list " + sl + " not found !";
Console.WriteLine(sinfo);
if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sinfo);
EventLog.WriteEntry(sSource, sinfo, EventLogEntryType.Warning, 110);
}
}
}
}
}
else { Console.WriteLine("Please enter arguments /s=siteurl /w=website /wf=workflowname /l=listname"); }
}
}
}
}