ASDK tool a day #1 - Add Users to a security role
I started writing this straight after work and ran into a few snags. The design was simple enough. I wanted to select a server, select a role and then add a full batch of users from a text box I could paste into. A few snags, but nothing I couldn't work around.
Snag #1
At I first tried to use method "getitemsinfolder" in the item management class.After scratching my head wondering why I wasn't get any return at all, I realized security roles are not technically "items" ( "items" can be exported. Security roles cannot ). So everytime I used that method, it was returning null since the security role fodler actually doesn't contain items. I switched this to an NS query using the method "ExecuteNSDatabaseQuery". It was simple enough:
string sql = "select name,guid from SecurityRole";
To attach the results to the combo box you have to use an XML Reader. This is done by using System.XML and the following code block:
XmlNode results = objItem.ExecuteNSDatabaseQuery(sql);
XmlTextReader reader = new XmlTextReader( new StringReader( results.OuterXml ) );
DataSet ds = new DataSet();
ds.ReadXml(reader);
DataTable dt = ds.Tables[0];
cmbBoxRoles.BeginUpdate();
cmbBoxRoles.DataSource = dt.DefaultView;
cmbBoxRoles.DisplayMember = "name";
cmbBoxRoles.ValueMember = "guid";
cmbBoxRoles.EndUpdate();
Snag #2
I tried to keep it simple when reading the textbox of users by replacing the new line with a comma and then passing that to the method "AddRoleMembers". This method is listed under SecurityManagementLib class. However, the problem is that the method only returns a single True if all were successful or a False if one failed. I needed to create an array that I can loop through so i can provide feedback on each id. I solved this with the following code block:
//grab the IDs from the text box and put them into an array string txtIds = txtBoxIDs.Text; string[] idArray = txtIds.Split(Environment.NewLine.ToCharArray());
//add the members to the role
foreach (string item in idArray){
bool success = objSecurity.AddRoleMembers(roleGuid, item);
if (item != "")
{
if (success.ToString() == "True")
{strSuccess = strSuccess + "Success" + "\r\n";}
else
{strSuccess = strSuccess + "Failed" + "\r\n";}
}
}
txtBoxStatus.Text = strSuccess;
Which leads into Snag #3
For whatever reason, if something returned true, it was always followed by another blank True, but no item name. So I had to throw a check to see if "item" was null.
ASDK Classes used: SecurityManagementLib , ItemManagementLib
ASDK Methods used: ExecuteNSDatabaseQuery AddRoleMembers