How to start the workflow for an item using PowerShell in SharePoint 2010



In this article we will be seeing how to start a workflow for an item using c# and powershell in SharePoint 2010.

In this article

  • Start a workflow using c#
  • Start a workflow using powershell

Start a workflow using c#
  • Open Visual Studio 2010.
  • Create a new console application.
  • Add the following reference.
     
    • Microsoft.SharePoint.dll
       
  • Add the following namespaces.
     
    • Using Microsoft.SharePoint;
    • Using Microsoft.SharePoint.WorkFlow;
  • Replace the code with the following.

class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://servername:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = web.Lists["Test"];
                    SPListItem item=list.Items[0];
                    SPWorkflowManager manager = site.WorkflowManager;
                    SPWorkflowAssociation association = list.WorkflowAssociations[0];
                    string data = association.AssociationData;
                    SPWorkflow wf = manager.StartWorkflow(item, association, data, true);
                }
            }
        }
    }


Start a workflow using powershell:

$siteURL="http://servername:1111/"
$listName="Test"
$site=Get-Spsite $siteURL
$web=$site.RootWeb
$list=$web.Lists[$listName]
$item=$list.Items[0]
$manager=$site.WorkFlowManager
$association=$list.WorkFlowAssociations[0]
$data=$association.AssociationData
$wf=$manager.StartWorkFlow($item,$association,$data,$true)