Compartir a través de


SharePoint useful code snippets 1/5: Workflow Report

Code Name

Workflow report

Code Functionality

        This script will run on any SharePoint server and generate a report on all subsites (SPWeb) for a given site collection.

Code Usage

        Run the script and save its results in a text file. Then copy the text file and paste them in Excel. Select the table in excel and on the insert tab, click on table.

        For example:

            WorkflowReport.exe >  WorkflowReport.txt https://MysharePointSiteUrl

Output Snapshot

image

Code Snippet

static

void Main(string[] args)

{

string siteUrl = default(string);

Console.WriteLine("This app will give a reprt on all workflows...");

Console.WriteLine("Please provide the url for the web application");

siteUrl =

Console.ReadLine();

GetReportForOneSiteCollection(siteUrl);

}

static bool writeheader = true;

private static void GetReportForOneSiteCollection(string siteUrl)

{

SPSite site =

new SPSite(siteUrl);

foreach (SPWeb subsite in site.AllWebs)

{

GetWebReport(site, subsite);

}

site.Dispose();

}

private static void GetWebReport(SPSite site, SPWeb subsite)

{

try

{

foreach (SPList list in subsite.Lists)

{

GetListReport(site, subsite, list);

}

subsite.Dispose();

}

catch

{

//Console.Write(subsite.Name + "\t\t");

//Console.Write("The Site has no usage data\r\n");

}

}

private static void GetListReport(SPSite site, SPWeb subsite, SPList list)

{

try

{

foreach (SPListItem item in list.Items)

{

GetItemReport(site, subsite, list, item);

}

}

catch

{

//Console.Write(subsite.Name + "\t\t");

//Console.Write("The Site has no usage data\r\n");

}

}

private static void GetItemReport(SPSite site, SPWeb subsite, SPList list, SPListItem item)

{

try

{

SPWorkflowCollection spwfc =

site.WorkflowManager.GetItemActiveWorkflows(item);

if (writeheader)

{

Console.Write("Site URL" + "\t");

Console.Write("Web URL" + "\t");

Console.Write("Item Url" + "\t");

Console.Write("Auther Username" + "\t");

Console.Write("Auther Email" + "\t");

Console.Write("Workflow Created" + "\t");

Console.Write("Is Completed" + "\t");

Console.Write("Is Locked" + "\t");

Console.Write("Workflow Status" + "\t");

Console.Write("Task List" + "\t");

Console.Write("Number of Taks" + "\t");

Console.Write("History List" + "\t");

Console.WriteLine("");

writeheader =

false;

}

foreach (SPWorkflow wf in spwfc)

{

Console.Write(subsite.Url+ "\t");

Console.Write(item.Url + "\t");

Console.Write(wf.AuthorUser.LoginName + "\t");

Console.Write(wf.AuthorUser.Email + "\t");

Console.Write(wf.Created + "\t");

Console.Write(wf.IsCompleted + "\t");

Console.Write(wf.IsLocked + "\t");

string s = resolveState(wf.InternalState);

Console.Write( s + "\t");

Console.Write(wf.TaskList.Title + "\t");

Console.Write(wf.Tasks.Count + "\t");

Console.Write(wf.HistoryList.Title + "\t");

Console.WriteLine("");

}

}

catch

{

//Console.Write(subsite.Name + "\t\t");

//Console.Write("The Site has no usage data\r\n");

}

}

private static string resolveState(SPWorkflowState sPWorkflowState)

{

string result = default(string);

result = sPWorkflowState.ToString();

switch (sPWorkflowState)

{

case SPWorkflowState.All:

result =

"All";

break;

case SPWorkflowState.Cancelled:

result =

"Cancelled";

break;

case SPWorkflowState.Completed:

result =

"Completed";

break;

case SPWorkflowState.Expired:

result =

"Expired";

break;

case SPWorkflowState.Expiring:

result =

"Expiring";

break;

case SPWorkflowState.Faulting:

result =

"Faulting";

break;

case SPWorkflowState.HasNewEvents:

result =

"HasNewEvents";

break;

case SPWorkflowState.Locked:

result =

"Locked";

break;

case SPWorkflowState.NotStarted:

result =

"NotStarted";

break;

case SPWorkflowState.Orphaned:

result =

"Orphaned";

break;

case SPWorkflowState.Running:

result =

"Running";

break;

case SPWorkflowState.Suspended:

result =

"Suspended";

break;

case SPWorkflowState.Terminated:

result =

"Terminated";

break;

}

return result;

}

Comments