Delete user from Role via API or smartobject


Badge +6

I'm trying to remove a user from a role using the API. I can successfuly add one but cannot remove one. I dont get any errors. It just doesnt remove the name. I verify this by looking in the workspace and refreshing. Here is my code:


 


 


 



ConnectToWorkspace(); //connect to the server


_role = urmServer.GetRole(rolename);



 



 



 



//_role.Include.Add(



 



new UserItem(K2Username)); //this example works fine.

 



 



 


_role.Include.Remove(new UserItem(K2Username)); //this part does nothing

urmServer.UpdateRole(_role);
urmServer.Connection.Close();
urmServer.Connection.Dispose();


8 replies

Badge +10

I "think" this is covered in a sample in the documentation located here


Developer Reference > Users > Roles > How to Remove a User from a Role using the K2 API


Code from article pasted below.


 



//instantiates userRoleManager


UserRoleManager urmServer = new UserRoleManager();


urmServer.CreateConnection();


//build connection string


SCConnectionStringBuilder builder = new SCConnectionStringBuilder()


{


Host = "localhost",


Port = 5555,


Integrated = true,


IsPrimaryLogin = true,


};


//open connection


urmServer.Connection.Open(builder.ConnectionString);


//Get the Role you wish to modify


Role _role = urmServer.GetRole("MyRole01");


//HERE WE DO THE INCLUDE COLLECTION


//if item to remove is user and part of include list


_role.Include.Remove(new UserItem("K2:DOMAININCLUDEUSERTOREMOVE"));


//if item to remove is group and part of include list


_role.Include.Remove(new GroupItem("K2:DOMAININCLUDEGROUPTOREMOVE"));


//if item to remove is smartobject and part of include list


SmartObjectItem soItem = new SmartObjectItem();


soItem.Name = "INCLUDE SMO TO REMOVE";


_role.Include.Remove(soItem);


//HERE WE DO THE EXCLUDE COLLECTION


//if item to remove is user and part of exclude list


_role.Exclude.Remove(new UserItem("K2:DOMAINEXCLUDEUSERTOREMOVE"));


//if item to remove is group and part of exclude list


_role.Exclude.Remove(new GroupItem("K2:DOMAINEXCLUDEGROUPTOREMOVE"));


//if item to remove is smartobject and part of exclude list


SmartObjectItem soItemExclude = new SmartObjectItem();


soItemExclude.Name = "EXCLUDE SMO TO REMOVE";


_role.Exclude.Remove(soItemExclude);


//update the Role


urmServer.UpdateRole(_role);


//cleanup memory


urmServer.Connection.Close();


urmServer.Connection.Dispose();


urmServer.DeleteConnection();


urmServer.Connection = null;


urmServer = null;


// Removing the role itself.


SourceCode.Security.UserRoleManager.Management.UserRoleManager srv = new SourceCode.Security.UserRoleManager.Management.UserRoleManager();


srv.CreateConnection();


srv.Connection.Open(CreateConn());


srv.DeleteRole(new Guid("8b937e13-5947-45f6-a4fb-7603570f9660"), "Tester");


// TODO: Close connection


Badge +6

Yep. That is the example i based my code on. But it is not working on my end. Without any errors, I'm at a loss as to what is going on.

Badge +3

The user that is trying to delete the role, do they have sufficient rights to do this?


 


Regards,

Badge +6

we are new at this. I assumed it did since i could add a person. How can i tell?


thanks

Badge +6

the user is listed as an admin for the process that has these roles. I assume that means it has delete rights.

Badge +1

hello :) i was wondering if you found a solution for this?  i am experiencing the same thing--i can add and delete roles, i can also add role items, but i cannot delete role items via api..


thanks!

Badge +8

The code sample does seem to be ineffective. I was able to delete users however using the following code:


var role = urm.GetRole("My Role");


 


URM.UserItem user = null;


foreach (URM.UserItem item in role.Include)


{


    if (string.Compare(item.Name, "K2:DenallixMike", true) == 0)


    {


        user = item;


        break;


    }


}


 


if (user != null)


{


    role.Include.Remove(user);


    urm.UpdateRole(role);


}

Badge +1

you're a lifesaver! thank you! that worked!

Reply