First, add this reference to the project you want to place the code.
using SourceCode.Security.UserRoleManager.Management;
Second, declare an instance of UserRoleManager like this :
private SourceCode.Security.UserRoleManager.Management.UserRoleManager _roleManager = new SourceCode.Security.UserRoleManager.Management.UserRoleManager();
Third, add a property like this (note that the connection string may vary according to your computer configuration)
public SourceCode.Security.UserRoleManager.Management.UserRoleManager RoleManager
{
get
{
string connString = string.Empty;
if (_roleManager == null)
_roleManager = new UserRoleManager();
_roleManager.CreateConnection();
if (!_roleManager.Connection.IsConnected)
{
connString = "Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=BLACKPEARL;Port=5555";//Connection string to HostServer
}
_roleManager.Connection.Open(connString);
return _roleManager;
}
}
Then , add a method to read the role items in a K2 blackpearl role (this method returns the role items in semicolon separated format, example: denallix\codi;denallix\anthony)
public string GetK2RoleUsers(string K2Role)
{
Role role = RoleManager.GetRole(K2Role);
string sRoleName = "";
foreach (UserItem UIDel in role.Include)
{
if (sRoleName.Equals(""))
sRoleName += UIDel.Name.Split(":".ToCharArray())[1];
else
sRoleName += ";" + UIDel.Name.Split(":".ToCharArray())[1];
}
return sRoleName.ToLower();
}
To make it clearer , I give a complete code how to use this in a server event code. In this example, I want to get all the role items in a K2 role named "IA" and place the result in K2 DataField named "DestinationApproval"
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using SourceCode.KO;
using SourceCode.Workflow.Common.Extenders;
using hostContext = Project_5b686e4bb3d24c9b9dfa1d2ef7e5ffb5.EventItemContext_cd0b00f67cf744188529af119ccd3f6b;
using System.Xml;
using System.Xml.XPath;
using SourceCode.Security.UserRoleManager.Management;
namespace ExtenderProject_5b686e4bb3d24c9b9dfa1d2ef7e5ffb5
{
public partial class EventItem_cd0b00f67cf744188529af119ccd3f6b : ICodeExtender<hostContext>
{
//Open Connection to RoleManager
private SourceCode.Security.UserRoleManager.Management.UserRoleManager _roleManager = new SourceCode.Security.UserRoleManager.Management.UserRoleManager();
public void Main(Project_5b686e4bb3d24c9b9dfa1d2ef7e5ffb5.EventItemContext_cd0b00f67cf744188529af119ccd3f6b K2)
{
string userIA = GetK2RoleUsers("IA");
K2.ProcessInstance.DataFields["DestinationApproval"].Value = userIA;
//you can ignore this code below, it's specific to business process of my module :)
XmlDocument doc = new XmlDocument();
string infopathFormName = K2.ProcessInstance.DataFields["InfopathFormName"].Value.ToString();
string xmlString = K2.ProcessInstance.XmlFields[infopathFormName].Value;
string xpathTempUser = K2.ProcessInstance.DataFields["XPathTempUser"].Value.ToString();
doc.LoadXml(xmlString);
XmlNamespaceManager NamespaceManager = InitNamespaceManager(doc);
XPathNavigator root = doc.CreateNavigator();
XPathNavigator node = root.SelectSingleNode(xpathTempUser, NamespaceManager);
node.SetValue(userIA);
K2.ProcessInstance.XmlFields[infopathFormName].Value = doc.InnerXml;
}
public string GetK2RoleUsers(string K2Role)
{
Role role = RoleManager.GetRole(K2Role);
string sRoleName = "";
foreach (UserItem UIDel in role.Include)
{
if (sRoleName.Equals(""))
sRoleName += UIDel.Name.Split(":".ToCharArray())[1];
else
sRoleName += ";" + UIDel.Name.Split(":".ToCharArray())[1];
}
return sRoleName.ToLower();
}
public SourceCode.Security.UserRoleManager.Management.UserRoleManager RoleManager
{
get
{
string connString = string.Empty;
if (_roleManager == null)
_roleManager = new UserRoleManager();
_roleManager.CreateConnection();
if (!_roleManager.Connection.IsConnected)
{
connString = "Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=BLACKPEARL;Port=5555";//Connection string to HostServer
}
_roleManager.Connection.Open(connString);
return _roleManager;
}
}
public XmlNamespaceManager InitNamespaceManager(XmlDocument xmlDOMDoc)
{
XmlNamespaceManager xnmMan;
xnmMan = new XmlNamespaceManager(xmlDOMDoc.NameTable);
foreach (XmlAttribute nsAttr in xmlDOMDoc.DocumentElement.Attributes)
{
if (nsAttr.Prefix == "xmlns")
xnmMan.AddNamespace(nsAttr.LocalName, nsAttr.Value);
}
return xnmMan;
}
}
}