Saturday, March 14, 2015

OnBase Unity Script to Create a User

The following C# Unity script snippet could be used to create OnBase User accounts before the users actually log in. This is useful if you want to provision the User account, group memberships, and load balancing without burdening the User to log in first.

ctx = new PrincipalContext(ContextType.Domain, "<your company's domain>");
UserAdministration userAdmin = g_Application.Core.UserAdministration;

//loop through all users in a list <listADUsers>
//check if user is in the OnBase core group
User OBuserTemp = g_Application.Core.GetUser(AddOBUser);
UserGroupList obusergrps = OBuserTemp.GetUserGroups();
Boolean onbasegrptest = false;

foreach (string ADUser in listADUsers)
{
AddOBUser = ADUser.ToString().ToUpper();

UserPrincipal user = new UserPrincipal(ctx);
PrincipalSearcher srch = null;
srch = new PrincipalSearcher(user);
user.SamAccountName = AddOBUser;
srch = new PrincipalSearcher();
srch.QueryFilter = user;
PrincipalSearchResult<Principal> result = srch.FindAll();
Principal Psrch = result.ToList()[0];
DirectoryEntry Dresult = (DirectoryEntry)Psrch.GetUnderlyingObject();

String EmailAddress = Dresult.Properties["mail"].Value.ToString();
String DisplayName = Dresult.Properties["displayName"].Value.ToString();

//create user and add to selected group
NewUserProperties userProperties = userAdmin.CreateNewUserProperties(AddOBUser, "<default password>");
userProperties.EmailAddress = EmailAddress;
userProperties.RealName = DisplayName;
User OBuser = userAdmin.CreateUser(userProperties);
userAdmin.AddUserToGroups(OBuser, grpList);
user.Dispose();
srch.Dispose();
Psrch.Dispose();
result.Dispose();
Dresult.Dispose();
}

No comments:

Post a Comment