/// <summary>
        /// This method returns Translator API settings object
        /// </summary>
        /// <param name="service">IOrganizationService object</param>
        /// <param name="tracingService">ITracingService object</param>
        /// <returns>TranslateApiSettings object</returns>
        internal static TranslateApiSettings GetTranslatorAPISettings(IOrganizationService service, ITracingService tracingService)
        {
            EntityCollection     retrieved;
            TranslateApiSettings apiSettings = new TranslateApiSettings();
            var cols = new ColumnSet();

            cols.AddColumns(new string[] { "msdyn_apikey", "msdyn_name", "msdyn_isautotranslationenabled" });
            try
            {
                var query = new QueryExpression
                {
                    ColumnSet  = cols,
                    EntityName = "msdyn_automatickmtranslationsetting",
                };
                retrieved = service.RetrieveMultiple(query);
                if (retrieved.Entities.Count == 1)
                {
                    if (retrieved.Entities[0].Attributes.Contains("msdyn_apikey"))
                    {
                        apiSettings.ClientSecret = (string)retrieved.Entities[0].Attributes["msdyn_apikey"];
                    }

                    if (retrieved.Entities[0].Attributes.Contains("msdyn_name"))
                    {
                        apiSettings.ClientID = (string)retrieved.Entities[0].Attributes["msdyn_name"];
                    }

                    if (retrieved.Entities[0].Attributes.Contains("msdyn_isautotranslationenabled"))
                    {
                        apiSettings.Isautotranslationenabled = (bool)retrieved.Entities[0].Attributes["msdyn_isautotranslationenabled"];
                    }
                }
            }
            catch (Exception ex)
            {
                tracingService.Trace("GetTranslatorAPISettings: {0}", ex.Message);
            }
            return(apiSettings);
        }
        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                                              serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Obtain the organization service reference.
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is EntityReference)
            {
                EntityReference reference       = (EntityReference)context.InputParameters["Target"];
                Entity          translateEntity = service.Retrieve(reference.LogicalName, reference.Id,
                                                                   new ColumnSet("isprimary", "isrootarticle", "title", "keywords", "description", "content", "languagelocaleid", "parentarticlecontentid"));

                TranslateApiSettings apiSettings = MicrosoftTranslateHelper.GetTranslatorAPISettings(service, tracingService);

                // Update the knowledge article entity record with the translated text only if it is only
                // translation (isprimary= false, isrootarticle=false) and if isautotranslationenabled is true

                if (((bool)translateEntity["isprimary"]) == false &&
                    ((bool)translateEntity["isrootarticle"]) == false)
                {
                    ColumnSet columns = new ColumnSet();
                    columns.AddColumn("languagelocaleid");

                    // Get the primary entity to retrieve the source language code
                    EntityReference parentArticleReference  = (EntityReference)translateEntity["parentarticlecontentid"];
                    Entity          primaryEntity           = service.Retrieve("knowledgearticle", parentArticleReference.Id, columns);
                    EntityReference languageLocaleReference = (EntityReference)primaryEntity["languagelocaleid"];
                    new ColumnSet();
                    columns.AddColumn("code");
                    Entity languageLocaleEntity = service.Retrieve("languagelocale", languageLocaleReference.Id, columns);
                    string sourceLanguageCode   = languageLocaleEntity["code"].ToString();

                    // Get the language code of the knowledge article being translated
                    languageLocaleReference = (EntityReference)translateEntity["languagelocaleid"];
                    languageLocaleEntity    = service.Retrieve("languagelocale", languageLocaleReference.Id, columns);
                    string destinationLanguageCode = languageLocaleEntity["code"].ToString();

                    tracingService.Trace("knowledgeArticleId of the parent article: " + parentArticleReference.Id.ToString());
                    tracingService.Trace("Translating from <" + sourceLanguageCode + "> to <" + destinationLanguageCode + ">");

                    try
                    {
                        if (translateEntity.Attributes.ContainsKey("title"))
                        {
                            translateEntity["title"] = MicrosoftTranslateHelper.Translate(translateEntity["title"].ToString(), sourceLanguageCode, destinationLanguageCode, apiSettings.ClientSecret, apiSettings.ClientID);
                        }
                        tracingService.Trace("Translated <title>: {0}", translateEntity["title"].ToString());

                        if (translateEntity.Attributes.ContainsKey("keywords"))
                        {
                            translateEntity["keywords"] = MicrosoftTranslateHelper.Translate(translateEntity["keywords"].ToString(), sourceLanguageCode, destinationLanguageCode, apiSettings.ClientSecret, apiSettings.ClientID);
                        }

                        if (translateEntity.Attributes.ContainsKey("description"))
                        {
                            translateEntity["description"] = MicrosoftTranslateHelper.Translate(translateEntity["description"].ToString(), sourceLanguageCode, destinationLanguageCode, apiSettings.ClientSecret, apiSettings.ClientID);
                        }

                        if (translateEntity.Attributes.ContainsKey("content"))
                        {
                            translateEntity["content"] = MicrosoftTranslateHelper.Translate(translateEntity["content"].ToString(), sourceLanguageCode, destinationLanguageCode, apiSettings.ClientSecret, apiSettings.ClientID);
                        }

                        service.Update(translateEntity);
                    }
                    catch (FaultException <OrganizationServiceFault> ex)
                    {
                        tracingService.Trace("KnowledgeArticleTranslation: {0}", ex.ToString());
                        throw new Exception(ex.Message);
                    }

                    catch (Exception ex)
                    {
                        tracingService.Trace("KnowledgeArticleTranslationSolution: {0}", ex.ToString());
                        throw new Exception(ex.Message);
                    }
                }
            }
        }