Monday, April 4, 2011

Start a SharePoint Designer Workflow using C# and Workflow Status keeps appearing as 'Starting' - SharePoint 2010

Back to SharePoint programming again. I needed to programmatically start a workflow from a InfoPath form using C#, which was designed using Microsoft SharePoint Designer 2010. So started writing the code using Windows SharePoint Services. Wrote the code and it was triggering the workflow nicely.

Code snippet for starting the workflow, I am writing down.

//oListItem is the item I am running the workflow on and "Create Items" is the name of my workflow
oListItem.Web.AllowUnsafeUpdates = true;
SPWorkflowManager workflowManager = oListItem.Web.Site.WorkflowManager;
SPWorkflowAssociationCollection workflowAssociation = oListItem.ParentList.WorkflowAssociations;
foreach (SPWorkflowAssociation Association in workflowAssociation)
{
    if (Association.Name == "Create Items")
    {
         workflowManager.StartWorkflow(oListItem, Association, Association.AssociationData, true);
         break;
    }
}

Of course it was triggering the workflow nicely, so I waited for the workflow to complete. And that's when I noted that the status of workflow keeps appearing as 'Starting'. First I thought it's because of the performance in my Virtual Machine and after waiting for around 2,3 minutes I knew, there is something else that is going wrong. I clicked on the workflow status and in that page there was a message "Due to heavy load, the latest workflow operation has been queued. It will attempt to resume at a later time". I went back to the list and I kept refreshing the page and after around 5 minutes status changed into 'In Process' and then after 2 or 3 minutes status changed into 'Completed'. Then I was pretty sure that something is not working properly. I did some googling and found out some possible reasons.

Possible reasons for this is,
  • SharePoint 2010 Timer service in Windows Services is not running.
  • You are kicking off the workflow from the user account Administrator.
  • The Start option in settings and the poor design of the workflow in SharePoint Designer.
First I checked SharePoint 2010 Timer service in Windows Services to see whether it is down or not. It was up and running. Then my second option was to check workflow kick off user. I checked it, I am not logged in as Administrator, it is some other user(lets say user1) I have created in Active Directory and granted permission to SharePoint server. Then I checked the workflow Start option and the design of the workflow. I have checked 'Allow this workflow to be manually started' and I checked the design of the workflow, to me it is nicely designed and no bottlenecks.

Apart from my situation, I thought I might give you some idea about how a poor design of workflow can decrease the execution speed of workflows. Lets say that you want to trigger off the workflow each time an Item is updated. So in SharePoint designer you can set under the Start options in the workflow to 'Start workflow automatically when an item is changed'. Now you have designed the workflow to Update List Items and you are giving the parameters to update(set field in Current Item). Here there is a important thing, that you will have to consider when designing the workflow. When the first parameter has updated, it has done another change to the item. And now the workflow fires again. So if you have not been careful enough, this will happen repetitively. Because of that your workflow status will be "Starting" when you check the workflow status in the browser. After a timeout it will stop updating that particular field and go to update the next field and the same process repeats again and again for all parameters. So for these kind of situations, the best practice is to test the field for whether has it already been changed to the new value that you are about to change it to. If yes do not update it again, move to the next field. I think now you might have some idea about increasing the execution speed of a workflow specially when the Start option is set to 'Start workflow automatically when an item is changed'.

And back to my situation, Everything seems perfect and still I am getting this status of workflow as 'Starting' for some long time. I was having enough of this and again I started rechecking everything. SharePoint 2010 Timer service is doing good and again checked the kicking off user of the workflow. No, I am not logged in as Administrator. And here something came to my mind that, I might not be logged in as the Administrator. But in the code I have written, I did not give any Authentication details. So there is a possibility that it uses the Administrator privileges. To check that I clicked on the workflow status 'Starting' for the item. I was astonished by one single thing from the page that appeared. That is under Workflow Information the Initiator of workflow is still 'System Account'. First I felt happy, because I knew that this can be the reason for my problem and then my problem was when I am logged in not as the Administrator and I am kicking off the workflow from a different user account and still how can be the Initiator of workflow is 'System Account'? To me, my only explanation is I am not giving the Authentication details in the code, so thought to add the Authentication part for the code to access the list from the user account User1.

Wrote the code and again added a new item to the list from InfoPath form which I have written all the code in. Kept looking at status of the workflow. Again it was 'Starting' and this time it was around for 30 seconds. Then 'In Progress' and then 'Completed'. For the whole process it took less than 1 minute to complete. I felt so happy and at last it did solve my problem.

I am writing the code down for Authentication part.

SPSite oSPSiteGetToken = new SPSite("http://ravanaserver/");
SPUserToken token = oSPSiteGetToken.RootWeb.EnsureUser("user1").UserToken;

using (SPSite oSPSite = new SPSite("http://ravanaserver/", token))
{
     using (SPWeb oSPWeb = oSPSite.OpenWeb())
         {
               //your code
          }
}

So from what I have learned, there is one thing we all should consider when writing codes.
  • Always use Authentication. For example if you are writing a code for CRUD actions on a list item and if you are not doing it as the Administrator, always use the specific user authentication in the code. Because default authentication is the System Account.
So hope someone gets something from this. Feel feel to correct me and give me your feedback.

Happy Coding.

Regards,
Jaliya

IMPORTANT NOTE :

Thursday, June 16, 2011

I just found out another reason that could keep Workflow Status appearing as 'Starting' for long time.
  • In your computer's Services "SharePoint 2010 Timer" service should be started, Startup Type should be "Automatic" and Log On As should be your SharePoint Administrator.

Happy Coding.

Regards,
Jaliya

3 comments:

  1. How do I Hard Code a backround Image on Microsoft Sharepoint Server? Using c#

    ReplyDelete
  2. If you can provide me more information on your requirement, It would be great.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
  3. This is very help full information.I have the same problem.In my case it is custom WF.WF creates 1..10 sites ,configure web parts and web part connections for each site. Creating sites is resource killing process.

    Usually workflow, Timer job code executes under SharePoint\System Account .That means execution of the code has full access to SharePoint environment. When we are running our code under elevated privileges (SPSecurity.RunWithElevatedPrivileges(…) ) same thing happen .Code runs under SharePoint\System . I’m wondering how this would cause an issue. But some times I also face problems when the code runs under SharePoint\System .

    ReplyDelete