Symptoms
trying to add an item to a SharePoint list using the following code:
Imports Microsoft.SharePoint
Module Module1
Sub Main()
Dim site As New SPSite("siteURL")
Dim web As SPWeb = site.OpenWeb
Dim adminUser As SPUser = web.EnsureUser("userName")
Dim userTok As SPUserToken = adminUser.UserToken
Using sysSite As New SPSite(site.Url, userTok)
Using sysWeb As SPWeb = sysSite.OpenWeb
Dim list As SPList = sysWeb.Lists("TEST List")
Dim newItem As SPListItem
newItem = list.AddItem
newItem("Title") = Now.ToShortDateString " " Now.ToShortTimeString
newItem.Update()
End Using
End Using
End Sub
End Module
**************************************
K2 process doesn't get triggered when an item is added
Diagnoses
We have a number of workflows that have been working fine for ages, but have now stopped working on an item being added to a SharePoint list in code.
The code uses a special login we created specifically to start the workflows, and this is NOT a system login which we know causes issues with workflows not firing.
The workflows are both SharePoint Event Processes and SharePoint workflow integration processes, and we have tried adding brand new workflows which do not work either.
The workflows kick off fine if a new item is added manually its just when using console applications to add items, but as I say this has been working fine for years previously to the last couple of weeks!
Some of the workflows that have stopped working were only built a couple of months ago, so since we upgraded to SharePoint 2010 and K2 BlackPearl 4.6.6. As I have said these were firing successfully until the last couple of weeks.
We have already done the following
- removed the workflows and re-deployed
- built new workflows
- re-run the K2 BlackPearl configuration wizard
- checked the permissions in K2 Workspace for the server and instances
- checked the permissions in SharePoint sites
Resolution
We noticed was that when adding a list item through code, it triggers the event in SP, but the event executes in the context of the calling app - we can confirm this in the ULS logs.
The ItemAddedEvent Runs asynchronously thus the console app terminates before the K2 workflow is started.
Two possible solutions:
· The app must wait for the event to complete before terminating.
· The ItemAdded events can be set to Synchronous using SPM.
This can be done for both Events processes and Workflow integration