How To: Get User IDs from SharePoint Group with Nintex Workflow
In a recent conversation with a client, who uses Nintex Workflow 2010, I was asked if it is possible to send personalised emails to individual users in a SharePoint group. In Nintex’ Send Email action you can choose to send an email to the whole group or an email to each member of the group. This however doesn’t meet the requirement of sending truly personalised emails to individuals, since the email body will be the same for all of the recipients.
So what can you do to get the members of a group and then send each of them an email? Easy enough since SharePoint provides a web method as part of the UserGroup.asmx web service. Then we package that up in a UDA and everyone can use it without the hassle of implementing it over and over again. Note that the logic of the UDA can also be implemented in Nintex Workflow 2007, it just can’t be wrapped into a UDA.
How does it work?
The workflow within the UDA consists of 2 parts
First we call the UserGroup (http://[SITEURL]/_vti_bin/usergroup.asmx) web service and use its GetUserCollectionFromGroup web method, which will bring back an XML with information about every user in the specified SharePoint group.
The XML looks something like this:
1 2 3
. . .
Next we use the “Query XML” action to extract the users’ ID and put them in a collection. The query will look like that:
Note the “xml” node at the start. This is because I selected “Force top level XML node” in the “Call web service” action. You can find that option under “Store result in” when you expand the “Result processing” option.
That is all we do. Of course you can query any of the other properties too. The returned properties for each user are:
- User ID
How do I use the UDA?
Drop the UDA into the right spot within your workflow logic. The UDA requires only one input parameter:
- Group Name – provide the name for the group you want to get the members from
In return the UDA will spit out a collection of all the IDs of all the users in the group:
- Users – collection in the following format: DOMAIN\USERID1; DOMAIN\USERID2; DOMAIN\USERID3;
In the event of an error within the UDA (connection issues, group already exists, etc.) there are 2 additional output parameters:
- Error Message – The message returned by the web service that errored
- Error Occurrence – Indicates that an error occurred within the UDA
Using the output parameters will help you to handle the exception appropriately within your workflow.
The web service in the UDA uses a workflow constant (Site Collection Level) called “SharePoint Web Service Account” of the type credential. You will have to create this constant before you can use the UDA as it currently stands. The account will have to be able to read web services across the site collection, so read only access to all sites for this account will be fine.
Alternatively you can modify the UDA in a way that the credentials are passed to the UDA as part of the input parameters.
What’s next? After the UDA you simply drop in a “For each” action and a “Send notification” action. That is it. The workflow should now have a section as shown below.
Now we need to configure the actions.
Get SharePoint Group Members UDA:
- Group Name: provide the name of the group you want to get the members from;
- Error Message: create a variable “ErrorMessage” of type Multiple lines of text for this;
- Error Occurred: create a variable “IsError” of type Yes/No for this;
- Users: create a variable “UserIDCollection” of type Collection for this;
- Target Collection: Select the “UserIDCollection” variable;
- Store result in: Create a variable “tempUserID” for this option; and
- We don’t need to configure the last 2 options.
- Use the “tempUserID” as the variable in the “To” field; and
- Customise the rest of the action as you wish.
So what does that have to do with personalised notifications? Well, within the “For each” action, just before the “Send notification” action you can use the variable “tempUserID” to get personlised information about the current user. For example you could query the user profile to get his/her name, address, phone number, etc. BUT, this I will leave up to you, but I think you can see now what my client was trying to do J