Skip to content

Extending Inaport – Example of a Custom Function

Following our previous post on extending Inaport using custom functions, Justin Knieling of Grand Canyon University kindly shared an example of a custom function he has written. The function assigns new users a security role in Microsoft CRM 2011.

Here is Justin’s discussion of his requirements and implementation:

Rarely do I find something that Inaport cannot do. However, we were recently trying to automate our user creation process and ran into a bit of a snag. We wrote a query to fetch user information from our identity management database and were able to use this to create new users in Dynamics CRM 2011 with Inaport. However, we also wanted to assign these users a security role based on some information in their record. We found that Inaport was lacking a feature to do this.

The great thing is that a new feature was introduced in Inaport version 7.3 that allows users to create their own custom Inaport functions. This means that if you can write code in a .Net language to do what you are trying to accomplish, you can write an Inaport function that will do it right within an Inaport profile.

I immediately set about writing a function to assign a security role to a user. It was extremely easy to incorporate the code to associate the user with a role into an Inaport function. In no time, I had my first function written and ready to use. Once you compile your function into a dll, all you have to do is drop that dll into the Inaport install directory, restart Inaport, and your new function will appear right within the interface.

This allowed us to automate a process that was extremely manual prior to this ability and is now saving us hours of work every week. With new features constantly being added to Inaport, I’m not sure how often I will have to write my own functions, but I feel more secure in my choice of ETL software knowing I am able to do this.

For reference, here is the actual code used to implement the function:

public class AssignSecurityRoleToUser : IPFunctionBase
{

    public AssignSecurityRoleToUser()
    {
         Name = "AssignSecurityRoleToUser("; // name of the function. YES it does need the "(" at the end.
         FuncDescriptor = new IPFuncDescriptor(
             "Assign a security role to a user", // this is the function description
             "AssignSecurityRoleToUser(#field1, #field2, \"http://server/org\")", // this is the example
             new string[] { "Extensions" }, // this is the category the function will appear under
             new IPFuncParamDesc[] { // these are the parameter descriptions
                 new IPFuncParamDesc("UserId", "Id of the system user record", "String", true),
                 new IPFuncParamDesc("RoleId", "Id of the security role record", "String", true),
                 new IPFuncParamDesc("OrgURL", "URL of the org", "String", true)
            }
         );
     }

    public override object Evaluate(Stack stack)
    {
        int argCount = popInt(stack);
        if (argCount >= 3)
        {
            try
            {
               ClientCredentials clientCred = new ClientCredentials();
               clientCred.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials; //credentials of logged in user
               String orgUrl = popStr(stack);
               Guid secRoleId = new Guid(popStr(stack));
               Guid userId = new Guid(popStr(stack));
               orgUrl = String.Format("{0}/XRMServices/2011/Organization.svc", orgUrl);

               OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(new Uri(orgUrl), null, clientCred, null);
               IOrganizationService orgService = (IOrganizationService)serviceProxy;

               AssociateRequest assignRoleRequest = new AssociateRequest();
               assignRoleRequest.RelatedEntities = new EntityReferenceCollection();
               assignRoleRequest.RelatedEntities.Add(new EntityReference("systemuser", userId));
               assignRoleRequest.Relationship = new Relationship("systemuserroles_association");
               assignRoleRequest.Target = new EntityReference("role", secRoleId);
               orgService.Execute(assignRoleRequest);
           }
           catch (Exception e)
           {
               throw e;
           }
           return 0;
       }
       else
       {
           return 1;
       }
     }
 } // end of Function

 

 

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: