Nintex Workflow – Update XML Action
Having the ability Query XML in Nintex Workflow, using XPath or XSLT to translate it has been a great part of the product, but with the latest release (Build 11000), we can finally Update XML. So to get my head around using this action, I created a small XML file :
<root> <Users> <User> <ID>1</ID> <Name>John Smith</Name> <Code>101</Code> </User> <User> <ID>2</ID> <Name>Mary Jones</Name> <Code>102</Code> </User> <User> <ID>3</ID> <Name>Homer Simpson</Name> <Code>103</Code> </User> </Users> </root>
and added it to a document library. My plan was to update some XML node in every User node in my document. If this was an InfoPath form, we might be updating some value such as a date field or an amount that was calculated through a workflow. In this particular case, I’m reading the Code node, adding some number and then updating that Code node. You can see from the workflow screen shot that it isn’t really a complicated workflow. When you are planning on performing a simple update on some XML, all you really need to the XPath to get at the node that you want to update, and then provide the value you want to set. In order to update just the Code node for every User in my XML file, I first needed to find out how many users I have. This will be used later, to loop through each user and update their Code node based on some calculation.
I use the Query XML action, point it to my XML file (Current Item) as the input XML, and am using XSLT to retrieve a count of the User nodes. The XSLT looks more complicated than it actually is. I’m storing the count in a Text variable (you can use a Number variable, but in order to get my head around the Convert Value new action, I thought I’d do this the long way).
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:value-of select="count(//root/Users/User)" /> </xsl:template> </xsl:stylesheet>
Next is to go through a loop of every User in the XML. The Loop action will continue to loop through while the numIndex variable is less than the count of the users that we retrieved earlier. Though each iteration of the loop, we need to extract the value of the Code node for that particular user. Given that the indexing in XPath is 1-based (instead of the good old 0-based), we increment the numIndex value first and then use it in our Query XML, using the following XPath expression :
To change the value of the Code that retrieved for the user, we perform a Math Operation action, and add the current index to the retrieved code. Last but not least, the Update XML action. Now that we have our new code, we want to update the Code node for the current User. We can use the same XPath expression that we used to get the original code, and use that in the Update XML. This will let the Update XML action know exactly which XML node we want to update.
The action “Add child node” is used the same way, but instead of providing a flat value, you would build up some XML using the Build Dynamic String action or some other method. All be aware of the “Add XML update” link which adds a new section into the configuration window, which allows you to perform multiple Adds or Updates in the one action. This makes it easier maintain your workflow in the future, as you don’t need multiple Update XML actions in your workflow. (bravo Nintex Workflow developers). Finally, you need to set the “Store result in” drop down. In my case, I am storing the data back in the Current Item. I am looking forward to using the Update XML action in a project soon. The simplified spec is that the customer wants to store some configuration files in SharePoint. They will be straight XML. The team is very much into golf, and are looking at updating their Team Site to use Nintex Workflow. Certain data entered will cause a Workflow to start, part of the workflow, will be to take the data entered, update the XML files, so that a 3rd party app will be able to read the latest information and perform some work on that. Prior to Build 11000 of Nintex Workflow, this would have been an impossibility without writing custom actions (loads more dev). Now, this will be extremely easy. Below is a link to the Workflow file that I mentioned in this blog.