Posts Tagged ‘workflow’

SharePoint 2010 State Machine workflow onWorkflowItemChanged firing multiple times

Friday, September 28th, 2012

If you’re designing a SharePoint 2010 workflow, particularly a state machine, you may base the workflow activities around an item in a list. Particularly, you may want the workflow to carry out activities when the item is changed by the end user.

This is a relatively straight-forward task, but one thing you will quickly encounter is that if you have multiple states, and each one handling the onWorkflowItemChanged event, you’ll notice that the event will fire once for each occurrence of your handler. So if you had 3 states, and each one has an onWorkflowItemChanged event, then your state that is handling the event will receive it 3 times. This is clearly undesirable and quite frankly strange behaviour.

Microsoft have acknowledged this behaviour, and in typical style, have said it is by design – whilst offering up a workaround.

SharePoint 2010 State Machine delay activity (and why it may not fire)

Sunday, September 23rd, 2012

A common problem in designing workflows is how to make the workflow “sleep” for a period whilst users do (or don’t do) something. A common scenario is where you wait for some user input, but if you don’t get any by a certain time to continue the workflow in a different stream. You achieve this with a delay activity where you specify a timeout in the form of a Timespan.

The delay activity was broken in SP2007 which was then fixed with a Hotfix. Surprisingly, this remains kind of broken in SP2010 as well. That is to say, the delay activity never wakes up.

This is due to a configuration value which is not set by default. Therefore, if you find that your delay activity doesn’t fire, you can issue the following commands:

stsadm -o getproperty -pn job-workflow -url http://yoursiteurl

This will show you the value of the property which will most likely be that it doesn’t exist. To fix this, issue:

stsadm -o setproperty -pn job-workflow -pv “Every x minutes between 0 and 59″ -url http://yoursiteurl

Source.

SharePoint workflow + list item edit + value cannot be null

Thursday, May 26th, 2011

You may encounter a random error when using a custom Visual Studio SharePoint 2010 workflow. Everything appears OK, but when you go to edit the item that the workflow is running on, the EditForm fails to load and you see an error like:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: s

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentNullException: Value cannot be null.
Parameter name: s]
System.IO.StringReader..ctor(String s) +10151478
System.Xml.XmlDocument.LoadXml(String xml) +51
Microsoft.SharePoint.Publishing.Internal.WorkflowUtilities.FlattenXmlToHashtable(String strXml) +90

(more…)

SharePoint 2010 + Associate workflow to Content Type in a feature

Thursday, May 5th, 2011

Quick tip: this plagued me for a while. You may develop a custom workflow which should only be associated with particular content types. You can deploy it to your site and then manually associate it to your content type. It’s possible to do this in code but the process isn’t as straight forward as it should be. Not to mention the fact that it’s infuriatingly hard to debug EventReceivers (“no symbols have been loaded” misery!).

Here are some steps to try/follow:

1) Creating the workflow. (Duh.)
2) Add a feature to your project.
3) Add an Event Receiver to your feature, e.g. Feature1.EventReceiver.cs
4) Uncomment the two blocks for FeatureActivated and FeatureDeactivating
5) Leave the GUID intact.
6) Your FeatureActivated code might look like this:

Note: this assume the Feature is deployed at the Site Collection level. If you’re at a different level, update the casting on properties.feature.Parent

  private string WorkflowName = "My Great Workflow";
        private string ContentTypeName = "My Great Content Type";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = ((SPSite)properties.Feature.Parent))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPContentType theCT = web.ContentTypes[ContentTypeName];

                    SPWorkflowTemplate theWF = null;
                    foreach (SPWorkflowTemplate tpl in web.WorkflowTemplates)
                    {
                        if (tpl.Name == WorkflowName)
                        {
                            theWF = tpl;
                        }
                    }

                    SPWorkflowAssociation wfAssociation = theCT.WorkflowAssociations.GetAssociationByName(WorkflowName, web.Locale);
                    if (wfAssociation != null)
                    {
                        theCT.WorkflowAssociations.Remove(wfAssociation);
                    }
                    theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);

                    wfAssociation = SPWorkflowAssociation.CreateWebContentTypeAssociation(theWF, WorkflowName, "New Business Tasks", "New Business History");

                    if (theCT.WorkflowAssociations.GetAssociationByName(wfAssociation.Name, web.Locale) == null)
                    {
                        theCT.WorkflowAssociations.Add(wfAssociation);
                    }
                    else
                    {
                        theCT.WorkflowAssociations.Update(wfAssociation);
                    }
                    theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);
                }
            }

7) Your FeatureDeactivating code might look like this:

 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = ((SPSite)properties.Feature.Parent))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPContentType theCT = web.ContentTypes[ContentTypeName];

                    SPWorkflowTemplate theWF = null;
                    foreach (SPWorkflowTemplate tpl in web.WorkflowTemplates)
                    {
                        if (tpl.Name == WorkflowName)
                        {
                            theWF = tpl;
                        }
                    }

                    SPWorkflowAssociation wfAssociation = theCT.WorkflowAssociations.GetAssociationByName(WorkflowName, web.Locale);
                    if (wfAssociation != null)
                    {
                        theCT.WorkflowAssociations.Remove(wfAssociation);
                    }
                    theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);
                }
            }
        }

8) This is the key bit! In your Feature1.template.xml file, ensure you set the ReceiverAssembly and ReceiverClass parts correctly. That is: the ReceiverAssembly is the compiled assembly of your e.g., workflow You can check this by browsing to the GAC (e.g., C:\Windows\assembly) and checking the version/publickeytoken, etc. The ReceiverClass is the entry point to your EventReceiver. So if the namespace in your Feature1.EventReceiver.cs file is MyNameSpace.MyGreatProject.Features.Feature1 and the class name was Feature1EventReceiver, and this was all compiled in to an assembly called MyGreatProject (v1.0.0.0) then your Feature.xml might look like:

<Feature ReceiverAssembly="MyGreatProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3d25ed43b57ce9e"
ReceiverClass="NyNameSpace.MyGreatProject.Features.Feature1.Feature1EventReceiver">
<Properties>
<Property Key="GloballyAvailable" Value="true"/>
<Property Key="RegisterForms" Value="*.xsn" />
</Properties>
</Feature>

Deploy and all should be OK.

SharePoint + Visual Studio + Get Current User

Wednesday, May 4th, 2011

A frequently asked question in the MSDN forums is “how you can get access to the user who is interacting with your workflow?”. For example, the user modifying a task. The workflow is likely running in a different context and/or session to your browser session so there’s not an obvious tie-up.

However, in this scenario, you can get the login name of the person who modified the SharePoint task, via the Executor property of the OnTaskChanged event. Simply bind the Executor property to a string (e.g., “taskLastModifiedBy”) and whenever the task changes, SharePoint will copy the user ID to this property in the format of DOMAIN\\LoginName. You can then get an SPUser object for that login name with, e.g.

SPUser user = workflowProperties.Web.AllUsers[taskLastModifiedBy];

Note: On a related subject, and the thing that prompted this post – if you’re trying to update a Person or Group field on your workflowProperties.Item, then you must pass it an SPUser object! This is bizarre, because other types of list (e.g, the task list) you can pass it a string and SharePoint will do the rest. I spent ages and all kinds of different things and always getting the “Invalid data has been used to update the list item. The field you are trying to update may be read only.” error. Annoying.

(more…)