Saturday, March 7, 2015

OnBase Unity Script to Add Active Directory Group Members to OnBase Group

The following search Active Directory for a specific group and members and adds these members to the corresponding OnBase Group.

namespace AddADGroupMembersToOnBase
{
    using System;
    using System.Text;
    using Hyland.Unity;
    using Hyland.Unity.Workflow;
    using System.Data.Odbc;
    using System.Collections;
    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;
    using System.Collections.Generic;

    /// <summary>
    /// This adds AD members from AD group to OnBase group.
    /// </summary>
    public class AddADGroupMembersToOnBase : Hyland.Unity.IWorkflowScript
    {
        private Hyland.Unity.Document _doc;
        private Hyland.Unity.Application _app;
        private Hyland.Unity.WorkflowEventArgs _args;
        private PrincipalContext ctx;

        #region IWorkflowScript
        /// <summary>
        /// Implementation of <see cref="IWorkflowScript.OnWorkflowScriptExecute" />.
        /// <seealso cref="IWorkflowScript" />
        /// </summary>
        /// <param name="app"></param>
        /// <param name="args"></param>
        public void OnWorkflowScriptExecute(Hyland.Unity.Application app, Hyland.Unity.WorkflowEventArgs args)
        {
_app = app;
_doc = args.Document;
_args = args;
     
           try{
               

ctx = new PrincipalContext(ContextType.Domain, "<your company domain>");
GroupPrincipal grp = null;
string gName = "<the AD group name to search>";
                grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, gName);
ArrayList ADUserList = new ArrayList();

                int numofusers = 0;

//find all AD users
                    foreach (Principal p in grp.GetMembers(true))
                    {
                        DirectoryEntry Dresult = (DirectoryEntry)p.GetUnderlyingObject();
                        String Attr1 = Dresult.Properties["userAccountControl"].Value.ToString();
                        if (Attr1 == "512")
                            ADUserList.Add(p.SamAccountName.ToUpper());
                        Dresult.Dispose();

                        numofusers++;
                    }

                    //list AP Approvers from OnBase group
                    UserGroup usrGroup;
                    UserList users;
                    ArrayList OBUserList = new ArrayList();
                    usrGroup = _app.Core.UserGroups.Find(gName);
                    users = _app.Core.UserAdministration.GetUsers(usrGroup);

                    foreach (User user in users)
                    {
                        OBUserList.Add(user.Name.ToString().ToUpper());
                    }

                    grp.Dispose();
                    ctx.Dispose();

                    foreach (string ADUser in ADUserList)
                    {
                       if (!OBUserList.Contains(ADUser))
                       {
                          addAPMember(ADUser, gName);
                       }
                    }
}
catch (Exception ex)
{
_app.Diagnostics.Write(ex);
_args.ScriptResult=false;
}
        }


private void addAPMember(string APMember, string gName)
        {
            try
            {
                if (_app.Core.GetUser(APMember) != null)
                {
                    UserAdministration userAdmin = _app.Core.UserAdministration;
                    List<UserGroup> grpList = new List<UserGroup>();
                    UserGroup grpToAdd;

                    //add the onbase core group
                    grpToAdd = _app.Core.UserGroups.Find(gName);
                    grpList.Add(grpToAdd);
                    
                    //add OB user to selected group
                    userAdmin.AddUserToGroups(_app.Core.GetUser(APMember.ToUpper()), grpList);
}

            }catch (Exception ex)
{
_app.Diagnostics.Write(ex);
_args.ScriptResult=false;
}
        }
        #endregion
    }
}

No comments:

Post a Comment