Skip to main content

How to add multiple user dynamically for K2 actvity


I am using contact selector for populating users in infopath, once the user submit the form i need assign this userid to destination.


please help!!!!!!!!


 


 


 

I have used the Contact Selector many times.  Your data source format for this control must conform to the following structure:


myFields
      contactSelector 
            Person
                  DisplayName
                  AccountId
                  AccountType


The 'AccountId' field is where the user's login id is stored, but it does not contain the domain.  For example, if the full logon id is 'somedomainjohndoe' then the AccountId field will simply contain 'johndoe'.  With this in mind, when you drag the 'AccountId' XML field onto the Destination User wizard, you'll need to prefix the domain name and a back slash.  It will look something like:


Destination User:   somedomain AccountId]      //where lAccountId] is the green K2 field dragged from the XML


I am using the Repeating Person node contains 3 sub nodes(DisplayName,AccountId). but when i assign this value it take 1st user as a destination user, but i am not able to create task list item in K2 worklist for the rest of the users.


I mean, though i selected 3 users in the contact selector in InfoPath, it uses only 1st selector or only one selector. please let me know how to resovle this issue.


Thanks for your response.


Ok, I see the problem now.  Also, I might be mistaken in my previous post.  The userName() function in InfoPath does not contain the domain name and I often have to prefix the domain when inserting data into a list that contains a 'Person or Group' field.  The People Picker might actually work.  But, that's not your issue anyway.  Out of the box, K2 supports specifying a repeating XML node as a destination user.  There actually isn't anything special you need to do.  If you have the K2 blackpearl book, you can find an example of this on page 828.  The issue there though is that the actual field you are using must be repeating.  In your case, the 'Person' group is the repeating node and not actually the 'AccountId' field (this field does not have the repeating icon in the K2 Object Browser I presume).  There are a couple of workarounds I can think of, but they aren't 'drag and drop' easy. 


1.  Create an InfoPath Submit rule that creates all of the AccountId's added to a SmartObject (SmartBox, SharePoint list, etc.) and then use a GetList method in the Destination User field.  All of the returned users from the GetList method will be added as destination users.  The advantage here is that it is easy and doesn't require one line of code.  The disadvantage is that you now have an external data store with duplicate data (although externalizing the data might not be a disadvantage).


2.  Go into the code behind of the destination user and iterate through the repeating nodes and use the K2 API to add users to the destination set.  The advantage here is that, well, it works and is fairly simple.  The disadvantage is that you have to write code and is 'hidden' from the UI.  Also, be careful when updating that wizard template in future releases or the custom code goes bye-bye.


 


Can you please explain the 2 nd scenario?


In the Activity i have infoPath event and mail event, now i need to set the destination user for this activity as dynamic user. when right click on the this activity i can find destination rule and in the helper region i am able to see for loop and end some 3 line do i need to comment and add our custom code or please explain how to accomplish this. i have got the selected users userid from contact picker into string as domainu1;domainu2;domainu3. now i just need to remove the assigned user and add the string one to destination rule. 


hope you understand my scenario please help me out.


Ok.  First, you'll need to drag the field into the Destination User wizard just so it can complete since it is a required field in the wizard.  Now, right-click on the activity, choose View Code and the Destination Rule.  Get to the C# code behind and scroll down to the bottom and look for the method called "AddDestinations(int setIdx)".  I used the following code at the bottom of this method:


                /********** COMMENTED OUT THE CODE BELOW.  USING CUSTOM CODE INSTEAD. **********/


                //// Iterate through the rest of the list and add each
                //// destination with its corresponding type.
                //for (int i = 1; i < destinations row].Length; i++)
                //{
                //    K2.Destinations.Add(destType, destinationsÂrow]);
                //}


                /****************** BEGIN CUSTOM CODE *********************/


                //If you have the values stored in a Process Data Field (non-XML field) use the line below
                //string destinationUsers = K2.ProcessInstance.DataFieldst"destinationUsers"].Value.ToString();


                //In the code below, we have an XML Field called 'SampleXMLField'.  If using InfoPath, it will be
                //the name of the form
                string xml = K2.ProcessInstance.XmlFieldsX"SampleXMLField"].Value;
                //Specify the XPath to the node that contains the string.  You can get this from within InfoPath
                //by right-clicking the data source field and selecting 'Copy XPath'
                string xPath = "XmlDocument/SampleXMLElement";
                string xmlUsers = SourceCode.Workflow.Common.XmlHelper.GetXPathValue(xml, xPath);
                stringr] users = xmlUsers.Split(new charh] { ';' });


                //Clear out the Destinaton Users collection
                K2.Destinations.Clear();


                //Iterate over the users array and add each destination user
                foreach(string user in users)
                {
                    K2.Destinations.Add(DestinationType.User, user);
                }


                /****************** END CUSTOM CODE **********************/


 


 


Thanks for your codesnippet. Let me try your solution. Hopefully this should work. Before that kindly clarify on below questions.


I am using InfoPath, Name of the Infopath file is Template11, and i have created selected user in contact picker into another field called Test and storeing selected Accountid like(domainuser1;domainuser2)(xpath is /my:myFields/my:Test)



//If you have the values stored in a Process Data Field (non-XML field) use the line below
                //string destinationUsers = K2.ProcessInstance.DataFieldst"destinationUsers"].Value.ToString();


                //In the code below, we have an XML Field called 'Template11'.  If using InfoPath, it will be 
                //the name of the form
                string xml = K2.ProcessInstance.XmlFields "Template11"].Value;
                //Specify the XPath to the node that contains the string.  You can get this from within InfoPath
                //by right-clicking the data source field and selecting 'Copy XPath'
                string xPath = "/my:myFields/my:Test";
                string xmlUsers = SourceCode.Workflow.Common.XmlHelper.GetXPathValue(xml, xPath);
                stringÂ] users = xmlUsers.Split(new charo] { ';' });


                //Clear out the Destinaton Users collection
                K2.Destinations.Clear();


                //Iterate over the users array and add each destination user
                foreach(string user in users)
                {
                    K2.Destinations.Add(DestinationType.User, user);
                }



Please let me know is that correct?


Thanks for your valuable help:)


Best regards


Madhan


 


 


Reply