Send Invite to Contacts automatically for Microsoft Portal in Dynamics 365

By | August 23, 2017

Introduction:

After installing/configuring the Portal for Dynamics 365 online, the first question that comes to our mind is, how will we give the Portal access/login to contacts?

In this blog, we will discuss few questions related to Portal access.

Question #1

As a System Administrator/CRM User, how can we send an invitation to Dynamics CRM contacts in order to provide access to Microsoft Dynamics CRM/365 Portal?

We can send an invitation to Dynamics CRM contacts by following few simple steps.

1. First, open the Contact record to which you want to send an invitation.

2. Then, click on Create Invitation button to create Invitation record with Invitation Code.

3. Save the Invitation record.

4. Now, run the Send Invitation

An email with the invitation link will be sent to the Contact.

Refer the following link to know more about these steps, https://community.adxstudio.com/products/adxstudio-portals/documentation/configuration-guide/portal-authentication/registration-and-invitations/invitations/

Question #2

As a System Administrator/CRM User, how can we send an invitation automatically when a contact is created in Dynamics CRM in order to provide access to Microsoft Dynamics CRM/365 Portal?

We can achieve this using a combination of manual workflow and workflow assembly. We need to create one workflow which will trigger on the creation of a contact. And then we just need to execute the steps mentioned above through the workflow.

However, generating invitation code is a bit tricky. We have to pass the Invitation code (which is unique for invitations) along with the invitation link.

Below is the screenshot of the workflow and explanation for each step;

Send Invite to Contacts automatically for Microsoft Portal in Dynamics 3651. Create an Invitation Code:

We have to call the workflow assembly that returns an auto-generated Invitation code as an output parameter.

Code that generates the invitation code (This is the same logic of generating invitation code as in OOB Invitation process.)

/// <summary>
    /// This assembly will return a randomly generated invitation code
    /// </summary>
    public class CreateInvitationCode : CodeActivity
    {
        [Output("Invitation Code")]
        public OutArgument<string> InvitationCode { get; set; }

        /// <summary>
        /// Execute the process and generate the code
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(CodeActivityContext codeactivitycontext)
        {
            #region (*Services & Context*)

            //Create Tracing Service
            ITracingService tracingservice = codeactivitycontext.GetExtension<ITracingService>();

            //Create the Workflow Context
            IWorkflowContext workflowcontext = codeactivitycontext.GetExtension<IWorkflowContext>();

            //Service Factory
            IOrganizationServiceFactory organizationservicefactory = codeactivitycontext.GetExtension<IOrganizationServiceFactory>();

            //Create the Organization Service
            IOrganizationService organizationservice = organizationservicefactory.CreateOrganizationService(workflowcontext.UserId);

            //Organization Context
            OrganizationServiceContext context = new OrganizationServiceContext(organizationservice);

            #endregion
            try
            {
                //create the code
                string code = string.Empty;// GetCodePartition() + GetCodePartition() + GetCodePartition() + GetCodePartition() + GetCodePartition() + GetCodePartition() + GetCodePartition() + GetCodePartition();

                //Declare random class to randomly generate a number between 0and 1
                Random rnd = new Random();

                //call the GetCodePartition() 8 times to create the code
                for (int index = 0; index < 8; index++)
                {
                    code += GetCodePartition(rnd);
                }
                //check if code has value
                if (code != string.Empty && code != null)
                {
                    tracingservice.Trace("Code: " + code);
                    //set the value in output parameter
                    this.InvitationCode.Set(codeactivitycontext, code.Trim().ToLower());
                }
            }
            catch (InvalidPluginExecutionException ex)
            {
                throw new InvalidPluginExecutionException("Execute" + ex.Message);
            }
        }

        /// <summary>
        /// Generate code partitions
        /// </summary>
        /// <returns></returns>
        private string GetCodePartition(Random rnd)
        {
            try
            {
                #region(Original function used in JScript)

                //  return Math.floor((1 + Math.random()) * 0x10000)
                //.toString(16)
                //.substring(1);

                #endregion

                //Create an integer number and then convert it to hexadecimal string
                string codePart = Convert.ToInt32(Math.Floor((1 + rnd.NextDouble()) * 65536)).ToString("X").Substring(1);
                return codePart;
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("getCodePartition" + ex.Message);
            }
        }
    }

2Create an Invitation Record:

This workflow step will CREATE an Invitation entity record. Set the output parameter returned by Step 1 in the Invitation code. Then set the other required fields; Sender, Name, Type, Invite Contact, Assign to Account, etc.

3Call child workflow that runs for Invitation entity:

This workflow will create and send an invitation link. Below is the screenshot of the child Workflow;

Send Invite to Contacts automatically for Microsoft Portal in Dynamics 365 1a) Create Email:

This step creates an email record where we need to add an invitation link in the email body. Append the invitation code that we generated and saved on the Invitation record, at the end of portal invitation URL.

Send Invite to Contacts automatically for Microsoft Portal in Dynamics 365b) Call ‘Portals: Send Invitation’ assembly:

Call Microsoft Portal’s ‘Send Invitation’ custom workflow assembly that sends the email created above to invitees.

c) Update status of invitation record to Invitation sent:

The next step is to update the Invitation Record to set status reason as Sent.

4. Stop workflow:

The final step is to stop the workflow.

Hope this helps!

70% of global 2000 companies apply gamification to improve productivity and returns!

Gamifics365 – Spin the magic of games within Microsoft Dynamics 365 CRM to improve user adoption, enhance productivity, and achieve company goals!

4 thoughts on “Send Invite to Contacts automatically for Microsoft Portal in Dynamics 365

  1. AYAN DEB

    how to send invitation using non admin user, using non admin i am unable to send invitation. it needs delete access on Activity entity

    1. inogic

      Hi Ayan,

      What error you are getting? Could you please share that?

      Thanks!

  2. Arunesh Karunanidhi

    Hi – Where do I need to copy and paste this code to create the invitation code? Please can you advise the steps in details if possible? I have created a workflow but unsure where to paste this code.

    1. Inogic

      Hi Arunesh,

      As you can see from step 1, the custom workflow is returning the value of “InvitationCode” which is nothing but the Output parameter of the custom workflow.

      In step 2, when you are creating the record of the Invitation entity, you need to set the “InvitationCode” which is the output parameter from step 1 in the Invitation Code field of the Invitation entity in CRM. This parameter can be found inside the “Local Values” section of the workflow properties where you will see the name of step 1 in the Local Values. Please refer to the below screenshot.

      Hope this helps!
      Thanks

Comments are closed.