Friday, July 18, 2014

How to start workflow in PowerShell after sp2010 sp2

To start workflow in PowerShell script should be easy. Below is the one I used for long time.

$itemCurrent = $list.GetItemById($itemId) 
$wfRunOption = [Microsoft.SharePoint.Workflow.SPWorkflowRunOptions]::Synchronous 
$wf = $manager.StartWorkFlow($itemCurrent,$assoc,$data,$wfRunOption) 

However, after installing SP2010 SP2, it stopped working with "Failed on Start" workflow status.

I know "system account" cannot trigger workflow. So, it should be easy to fix: we can get the SPSite object under another user's token. Below is the script

$web = Get-SPWeb -Identity $webURL 
$user=$web.EnsureUser("domainName\anotherUser")
$token = $user.UserToken;
$impSite= New-Object Microsoft.SharePoint.SPSite($webURL, $token);
$web.Dispose()
$web = $impSite.OpenWeb()
$manager = $web.Site.WorkFlowManager 
$list = $web.Lists[$listName] 

However, nothing is changed. Still the same error: "Failed on Start".

I did some searching on Internet, which leads to pages like here and here. None of them resolve the problem.

Then I RDP to SharePoint server desktop as another user which has enough rights of database and site collection, and run PowerShell script manually. Still same. This is weird.

As last resort, I RDP to SharePoint server as a farm admin and then run the "SharePoint 2010 Management Shell" as a different user (which has enough rights, such as "domainName\SomeUser"). It worked!

I don't understand it although feel quite happy.

(I would really appreciate it if you can explain that in comments)

[update 20140718]

If still got error "Failed on Start", please check ULS log.  If the error message is similar to the one below, you need to grant windows local admin rights to "domainName\SomeUser", which is used to run "SharePoint 2010 Management Shell".

Errors in ULS:

Workflow Compile Failed while loading AuthorizedTypes from config: An error occurred creating the configuration section handler for System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes: Could not find file 'C:\Users\0efang\AppData\Local\Temp\qklxfvr3.dll'. (C:\inetpub\wwwroot\wss\VirtualDirectories\appsdev.unitingcare.local80\web.config line 654)

RunWorkflow: Microsoft.SharePoint.SPException: An error occurred creating the configuration section handler for System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes: Could not find file 'C:\Users\0efang\AppData\Local\Temp\qklxfvr3.dll'. (C:\inetpub\wwwroot\wss\VirtualDirectories\appsdev.unitingcare.local80\web.config line 654)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.SubCompiler.DoCompile(WorkflowCompilerParameters parameters, String xomlSource, String assemblyName, CompilationPacket& packet, DirectoryInfo& tempDir)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.SubCompiler.DoCompile(WorkflowCompilerParameters parameters, String xomlSource, String assemblyName, CompilationPacket& packet, DirectoryInfo& tempDir)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.<>c__DisplayClasse.b__c()    
 at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.DoCompileNewAppDomain(WorkflowCompilerParameters parameters, String xomlSource, String assemblyName, SPWeb web, CompilationPacket& packet, DirectoryInfo& tempDir)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.CompileBytes(Byte[] xomlBytes, Byte[] rulesBytes, Boolean doTestCompilation, String assemblyName, SPWeb web, Boolean forceNewAppDomain)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.LoadXomlAssembly(SPWorkflowAssociation association, SPWeb web)    
 at Microsoft.SharePoint.Workflow.SPWinOeHostServices.LoadDeclarativeAssembly(SPWorkflowAssociation association, Boolean fallback)    
 at Microsoft.SharePoint.Workflow.SPWinOeHostServices.CreateInstance(SPWorkflow workflow)    
 at Microsoft.SharePoint.Workflow.SPWinOeEngine.RunWorkflow(SPWorkflowHostService host, SPWorkflow workflow, Collection`1 events, TimeSpan timeOut)    
 at Microsoft.SharePoint.Workflow.SPWorkflowManager.RunWorkflowElev(SPWorkflow workflow, Collection`1 events, SPWorkflowRunOptionsInternal runOptions)

Microsoft.SharePoint.SPException: An error occurred creating the configuration section handler for System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes: Could not find file 'C:\Users\0efang\AppData\Local\Temp\qklxfvr3.dll'. (C:\inetpub\wwwroot\wss\VirtualDirectories\appsdev.unitingcare.local80\web.config line 654)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.SubCompiler.DoCompile(WorkflowCompilerParameters parameters, String xomlSource, String assemblyName, CompilationPacket& packet, DirectoryInfo& tempDir)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.SubCompiler.DoCompile(WorkflowCompilerParameters parameters, String xomlSource, String assemblyName, CompilationPacket& packet, DirectoryInfo& tempDir)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.<>c__DisplayClasse.b__c()    
 at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.DoCompileNewAppDomain(WorkflowCompilerParameters parameters, String xomlSource, String assemblyName, SPWeb web, CompilationPacket& packet, DirectoryInfo& tempDir)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.CompileBytes(Byte[] xomlBytes, Byte[] rulesBytes, Boolean doTestCompilation, String assemblyName, SPWeb web, Boolean forceNewAppDomain)    
 at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.LoadXomlAssembly(SPWorkflowAssociation association, SPWeb web)    
 at Microsoft.SharePoint.Workflow.SPWinOeHostServices.LoadDeclarativeAssembly(SPWorkflowAssociation association, Boolean fallback)    
 at Microsoft.SharePoint.Workflow.SPWinOeHostServices.CreateInstance(SPWorkflow workflow)    
 at Microsoft.SharePoint.Workflow.SPWinOeEngine.RunWorkflow(SPWorkflowHostService host, SPWorkflow workflow, Collection`1 events, TimeSpan timeOut)    
 at Microsoft.SharePoint.Workflow.SPWorkflowManager.RunWorkflowElev(SPWorkflow workflow, Collection`1 events, SPWorkflowRunOptionsInternal runOptions)

1 comment:

  1. Hi there! glad to drop by your page and found these very interesting and informative stuff. Thanks for sharing, keep it up!

    ReplyDelete