Tuesday, 5 June 2018

custom workflow to change Lead data to another language in Dynamic CRM

Translate product names and properties into multiple languages
Description:
In this example we explain that how to translate Lead Data from one Language to another Language in Dynamic CRM using Custom Workflow. Or translate lead data in Dynamic CRM using plugin or custom workflow. Or how to translate record from one language to another language using custom workflow in Dynamic CRM.

So here we demonstrate that how to change Lead Record from English to other language like Spanish, Dutch etc. when Lead is created in Dynamic CRM at that that custom workflow will be called in back end and it will translate all field of the Lead to another Language.

So when English user enter Lead data in English and when same Lead is open by Dutch user then these Lead data are shown in Dutch Language based on user language you can setup.

Below is the custom workflow code that will be helpful to translate the Lead Record to another language.
Common Class:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Metadata.Query;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChangeLanguage.Language
{
    public class Common
    {
        public ITracingService tracingService;
        public IWorkflowContext context;
        public IOrganizationServiceFactory serviceFactory;
        public IOrganizationService service;

        public Common(CodeActivityContext executionContext)
        {
            tracingService = executionContext.GetExtension<ITracingService>();
            context = executionContext.GetExtension<IWorkflowContext>();
            serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            service = serviceFactory.CreateOrganizationService(context.UserId);
        }
    }
}

 msdyncrmWorkflowTools_Class Class :

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Net.Http.Headers;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using Microsoft.Crm.Sdk.Messages;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using System.Globalization;

using System.Drawing;
using System.Drawing.Imaging;
using System.Xml.Linq;

namespace ChangeLanguage.Language
{
    public class msdyncrmWorkflowTools_Class
    {
        private IOrganizationService service;
        private ITracingService tracing;

        //Shared HttpClient
        private static HttpClient httpClient;

        /// <summary>
        /// Class Constructor: Inits Singletion objects
        /// </summary>
        static msdyncrmWorkflowTools_Class()
        {
            //Setup a commong HttpClient as a best practice to avoid leaving open connections
            //more details https://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/
            httpClient = new HttpClient();
            httpClient.Timeout = new TimeSpan(0, 0, 30); //30 second timeout as recommend by Microsoft Support to prevent TimeOut on Sandbox
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header

        }

        public msdyncrmWorkflowTools_Class(IOrganizationService _service, ITracingService _tracing)
        {
            service = _service;
            tracing = _tracing;
         
        }

        public msdyncrmWorkflowTools_Class(IOrganizationService _service)
        {
            service = _service;
            tracing = null;
        }

        public void QueryValues()
        {
        }


        public string JsonParser(string Json, string JsonPath)
        {
            if (JsonPath == null) JsonPath = "";
            JObject o = JObject.Parse(Json);
            string name = o.SelectToken(JsonPath).ToString();

            return name;

        }

     

        public string AzureTranslateText(string subscriptionKey, string text, string sourceLanguage, string destinationLanguage)
        {
            string uri;
            var authTokenSource = new AzureAuthToken(subscriptionKey.Trim());
            string authToken = authTokenSource.GetAccessToken();
            HttpRequestMessage request;

            if (sourceLanguage == "")
            {
                uri = "https://api.microsofttranslator.com/v2/Http.svc/Detect?text=" + text;
                request = new HttpRequestMessage(HttpMethod.Get, uri);

                request.Headers.Add("Authorization", authToken);

                XmlDocument xmlDoc = new XmlDocument();

                xmlDoc.LoadXml(ExecuteAsyncRequest(request));
                sourceLanguage = xmlDoc.ChildNodes[0].InnerText;

            }

            uri = "https://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + text + "&from=" + sourceLanguage + "&to=" + destinationLanguage;

            request = new HttpRequestMessage(HttpMethod.Get, uri);
            request.Headers.Add("Authorization", authToken);

            return ExecuteAsyncRequest(request);
        }

        public string AzureFunctionCall(string jSon, string serviceUrl)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, serviceUrl);
            request.Content = new StringContent(jSon, Encoding.UTF8, "application/json");

            return ExecuteAsyncRequest(request);
        }


        public string AzureTextAnalyticsSentiment(string subscriptionKey, string text, string language)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
                    "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment");

            request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
            request.Content = new StringContent("{\"documents\":[" + "{\"language\":\"" + language + "\" , \"id\":\"1\",\"text\":\"" + text + "\"}]}"
                , Encoding.UTF8, "application/json");

            return ExecuteAsyncRequest(request);
        }


     

        public string TranslateText(string textToTranslate, string language, string key)
        {

            TranslateTextasync(textToTranslate, language, key).Wait() ;
            var content = XElement.Parse(result).Value;
            return content;
        }
        string result;
        async  Task TranslateTextasync(string textToTranslate, string language, string key)
        {
             string host = "https://api.microsofttranslator.com";
             string path = "/V2/Http.svc/Translate";

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);

            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>() {
                new KeyValuePair<string, string> (textToTranslate,language)// "fr-fr"

            };
           
            foreach (KeyValuePair<string, string> i in list)
            {
                string uri = host + path + "?to=" + i.Value + "&text=" + System.Net.WebUtility.UrlEncode(i.Key);

                HttpResponseMessage response = await client.GetAsync(uri);

                 result = await response.Content.ReadAsStringAsync();
                // NOTE: A successful response is returned in XML. You can extract the contents of the XML as follows.
                // var content = XElement.Parse(result).Value;
                Console.WriteLine(result);

              
            }
           
        }
     
        /// <summary>
        /// Forces a synchronous Execution of an HttpRequest
        /// </summary>
        /// <param name="message">HttpRequest to Execute</param>
        /// <returns>String with Response</returns>
        private string ExecuteAsyncRequest(HttpRequestMessage message)
        {
            string result = null;
            var task = Task.Run(async () =>
            {
                var response = await httpClient.SendAsync(message);
                result = await response.Content.ReadAsStringAsync();
            });

            while (!task.IsCompleted)
            {
                System.Threading.Thread.Yield();
            }
            if (task.IsFaulted)
            {
                throw task.Exception;
            }
            else if (task.IsCanceled)
            {
                throw new TimeoutException(string.Format("Timeout waiting for HttpResponse {1}:{0}", message.RequestUri, message.Method));
            }
            return result;
        }

    }


    public class AzureAuthToken
    {
        private static HttpClient client = new HttpClient();

        /// URL of the token service
        private static readonly Uri ServiceUrl = new Uri("https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
        /// Name of header used to pass the subscription key to the token service
        private const string OcpApimSubscriptionKeyHeader = "Ocp-Apim-Subscription-Key";
        /// After obtaining a valid token, this class will cache it for this duration.
        /// Use a duration of 5 minutes, which is less than the actual token lifetime of 10 minutes.
        private static readonly TimeSpan TokenCacheDuration = new TimeSpan(0, 5, 0);

        /// Cache the value of the last valid token obtained from the token service.
        private string storedTokenValue = string.Empty;
        /// When the last valid token was obtained.
        private DateTime storedTokenTime = DateTime.MinValue;

        /// Gets the subscription key.
        public string SubscriptionKey { get; private set; } = string.Empty;

        /// Gets the HTTP status code for the most recent request to the token service.
        public HttpStatusCode RequestStatusCode { get; private set; }

        /// <summary>
        /// Creates a client to obtain an access token.
        /// </summary>
        /// <param name="key">Subscription key to use to get an authentication token.</param>
        public AzureAuthToken(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", "A subscription key is required");
            }

            this.SubscriptionKey = key;
            this.RequestStatusCode = HttpStatusCode.InternalServerError;
        }

        /// <summary>
        /// Gets a token for the specified subscription.
        /// </summary>
        /// <returns>The encoded JWT token prefixed with the string "Bearer ".</returns>
        /// <remarks>
        /// This method uses a cache to limit the number of request to the token service.
        /// A fresh token can be re-used during its lifetime of 10 minutes. After a successful
        /// request to the token service, this method caches the access token. Subsequent
        /// invocations of the method return the cached token for the next 5 minutes. After
        /// 5 minutes, a new token is fetched from the token service and the cache is updated.
        /// </remarks>
        public async Task<string> GetAccessTokenAsync()
        {
            if (SubscriptionKey == string.Empty) return string.Empty;

            // Re-use the cached token if there is one.
            if ((DateTime.Now - storedTokenTime) < TokenCacheDuration)
            {
                return storedTokenValue;
            }

            using (var request = new HttpRequestMessage())
            {
                request.Method = HttpMethod.Post;
                request.RequestUri = ServiceUrl;
                request.Content = new StringContent(string.Empty);
                request.Headers.TryAddWithoutValidation(OcpApimSubscriptionKeyHeader, this.SubscriptionKey);
                client.Timeout = TimeSpan.FromSeconds(2);
                var response = await client.SendAsync(request);
                this.RequestStatusCode = response.StatusCode;
                response.EnsureSuccessStatusCode();
                var token = await response.Content.ReadAsStringAsync();
                storedTokenTime = DateTime.Now;
                storedTokenValue = "Bearer " + token;
                return storedTokenValue;
            }
        }

     

    }

}

 Custom WorkFlow:

namespace ChangeLanguage.Language
{
    using System;
    using System.Activities;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Query;
    using System.Collections.Generic;
    using System.Linq;

    public sealed class TranslateText : CodeActivity
    {
       

        #region "Parameter Definition"
        [RequiredArgument]
        [Input("Text To Translate")]
        [Default("")]
        public InArgument<String> TextToTranslate { get; set; }

        [RequiredArgument]
        [Input("User")]
        [ReferenceTarget("systemuser")]
        public InArgument<EntityReference> User { get; set; }

        [RequiredArgument]
        [Input("Authentication key")]
        [Default("")]
        public InArgument<string> Authenticationkey { get; set; }

        [RequiredArgument]
        [Input("Lead")]
        [ReferenceTarget("lead")]
        public InArgument<EntityReference> Lead { get; set; }



        protected override void Execute(CodeActivityContext executionContext)
        {


            #region "Load CRM Service from context"

            Common objCommon = new Common(executionContext);
            objCommon.tracingService.Trace("Load CRM Service from context --- OK");
            #endregion

            #region "Read Parameters"
            String _Authenticationkey = this.Authenticationkey.Get(executionContext);

            #endregion

            string topicinenglish = null;
            string topicinspanish = null;
            string topicinitly = null;
            string topicingerman = null;
            string topicindutch = null;
            string topicinfrench = null;

            string jobtitleinenglish = null;
            string jobtitleinspanish = null;
            string jobtitleinitly = null;
            string jobtitleingerman = null;
            string jobtitleindutch = null;
            string jobtitleinfrench = null;

            string descinenglish = null;
            string descinspanish = null;
            string descinitly = null;
            string descingerman = null;
            string descindutch = null;
            string descinfrench = null;
            EntityReference userReference = this.Lead.Get(executionContext);
            EntityReference systemuserReference = this.User.Get(executionContext);

            QueryExpression ContactlList = new QueryExpression("usersettings");
            ContactlList.ColumnSet = new ColumnSet(true);

            ContactlList.Criteria.AddCondition(

               "systemuserid",
                ConditionOperator.Equal,
                systemuserReference.Id
            );

            var contactDetail = objCommon.service.RetrieveMultiple(ContactlList);



            QueryExpression leadlist = new QueryExpression("lead");
            leadlist.ColumnSet = new ColumnSet(true);

            leadlist.Criteria.AddCondition(

               "leadid",
                ConditionOperator.Equal,
               userReference.Id
            );

            var leadDetail = objCommon.service.RetrieveMultiple(leadlist);

            string LanguageId = Convert.ToString(contactDetail.Entities[0].Attributes["uilanguageid"]);

            Entity ld = new Entity("lead");
            ld.Attributes.Add("leadid", userReference.Id);
            msdyncrmWorkflowTools_Class commonClass = new msdyncrmWorkflowTools_Class(objCommon.service, objCommon.tracingService);

            // throw new InvalidWorkflowException("Old Text is  :" + Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinfrench"]));
            if (LanguageId == "1033") //English
            {
                if (leadDetail.Entities[0].Attributes.Contains("subject"))
                {
                    topicinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["subject"]), "es", _Authenticationkey);
                     topicinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["subject"]), "it", _Authenticationkey);
                    topicingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["subject"]), "de", _Authenticationkey);
                    topicindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["subject"]), "nl", _Authenticationkey);
                    topicinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["subject"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_topicinspanish", topicinspanish);
                     ld.Attributes.Add("new_topicinitalian", topicinitly);
                    ld.Attributes.Add("new_topicingerman", topicingerman);
                    ld.Attributes.Add("new_topicindutch", topicindutch);
                    ld.Attributes.Add("new_topicinfrench", topicinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("jobtitle"))
                {
                    jobtitleinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["jobtitle"]), "es", _Authenticationkey);
                     jobtitleinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["jobtitle"]), "it", _Authenticationkey);
                    jobtitleingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["jobtitle"]), "de", _Authenticationkey);
                    jobtitleindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["jobtitle"]), "nl", _Authenticationkey);
                    jobtitleinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["jobtitle"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_jobtitleinspanish", jobtitleinspanish);
                     ld.Attributes.Add("new_jobtitleinitalian", jobtitleinitly);
                    ld.Attributes.Add("new_jobtitleingerman", jobtitleingerman);
                    ld.Attributes.Add("new_jobtitleindutch", jobtitleindutch);
                    ld.Attributes.Add("new_jobtitleinfrench", jobtitleinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("description"))
                {
                    descinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["description"]), "es", _Authenticationkey);
                       descinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["description"]), "it", _Authenticationkey);
                    descingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["description"]), "de", _Authenticationkey);
                    descindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["description"]), "nl", _Authenticationkey);
                    descinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["description"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_descriptioninspanish", descinspanish);
                    ld.Attributes.Add("new_descriptioninitalian", descinitly);
                    ld.Attributes.Add("new_descriptioningerman", descingerman);
                    ld.Attributes.Add("new_descriptionindutch", descindutch);
                    ld.Attributes.Add("new_descriptioninfrench", descinfrench);
                }
            }
            else if (LanguageId == "3082")//Spanish
            {
                if (leadDetail.Entities[0].Attributes.Contains("new_topicinspanish"))
                {
                    topicinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinspanish"]), "en", _Authenticationkey);
                     topicinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinspanish"]), "it", _Authenticationkey);
                    topicingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinspanish"]), "de", _Authenticationkey);
                    topicindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinspanish"]), "nl", _Authenticationkey);
                    topicinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinspanish"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("subject", topicinenglish);
                     ld.Attributes.Add("new_topicinitalian", topicinitly);
                    ld.Attributes.Add("new_topicingerman", topicingerman);
                    ld.Attributes.Add("new_topicindutch", topicindutch);
                    ld.Attributes.Add("new_topicinfrench", topicinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_jobtitleinspanish"))
                {
                    jobtitleinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinspanish"]), "en", _Authenticationkey);
                      jobtitleinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinspanish"]), "it", _Authenticationkey);
                    jobtitleingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinspanish"]), "de", _Authenticationkey);
                    jobtitleindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinspanish"]), "nl", _Authenticationkey);
                    jobtitleinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinspanish"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("jobtitle", jobtitleinenglish);
                     ld.Attributes.Add("new_jobtitleinitalian", jobtitleinitly);
                    ld.Attributes.Add("new_jobtitleingerman", jobtitleingerman);
                    ld.Attributes.Add("new_jobtitleindutch", jobtitleindutch);
                    ld.Attributes.Add("new_jobtitleinfrench", jobtitleinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_descriptioninspanish"))
                {
                    descinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninspanish"]), "en", _Authenticationkey);
                     descinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninspanish"]), "it", _Authenticationkey);
                    descingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninspanish"]), "de", _Authenticationkey);
                    descindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninspanish"]), "nl", _Authenticationkey);
                    descinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninspanish"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("description", descinenglish);
                    ld.Attributes.Add("new_descriptioninitalian", descinitly);
                    ld.Attributes.Add("new_descriptioningerman", descingerman);
                    ld.Attributes.Add("new_descriptionindutch", descindutch);
                    ld.Attributes.Add("new_descriptioninfrench", descinfrench);
                }
            }
            else if (LanguageId == "1040")//Italy
            {
                if (leadDetail.Entities[0].Attributes.Contains("new_topicinitalian"))
                {
                    topicinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinitalian"]), "es", _Authenticationkey);
                    topicinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinitalian"]), "en", _Authenticationkey);
                    topicingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinitalian"]), "de", _Authenticationkey);
                    topicindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinitalian"]), "nl", _Authenticationkey);
                    topicinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinitalian"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_topicinspanish", topicinspanish);
                    ld.Attributes.Add("subject", topicinenglish);
                    ld.Attributes.Add("new_topicingerman", topicingerman);
                    ld.Attributes.Add("new_topicindutch", topicindutch);
                    ld.Attributes.Add("new_topicinfrench", topicinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_jobtitleinitalian"))
                {
                    jobtitleinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinitalian"]), "es", _Authenticationkey);
                    jobtitleinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinitalian"]), "en", _Authenticationkey);
                    jobtitleingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinitalian"]), "de", _Authenticationkey);
                    jobtitleindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinitalian"]), "nl", _Authenticationkey);
                    jobtitleinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinitalian"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_jobtitleinspanish", jobtitleinspanish);
                    ld.Attributes.Add("jobtitle", jobtitleinenglish);
                    ld.Attributes.Add("new_jobtitleingerman", jobtitleingerman);
                    ld.Attributes.Add("new_jobtitleindutch", jobtitleindutch);
                    ld.Attributes.Add("new_jobtitleinfrench", jobtitleinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_descriptioninitalian"))
                {
                    descinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninitalian"]), "es", _Authenticationkey);
                    descinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninitalian"]), "en", _Authenticationkey);
                    descingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninitalian"]), "de", _Authenticationkey);
                    descindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninitalian"]), "nl", _Authenticationkey);
                    descinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninitalian"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_descriptioninspanish", descinspanish);
                    ld.Attributes.Add("description", descinenglish);
                    ld.Attributes.Add("new_descriptioningerman", descingerman);
                    ld.Attributes.Add("new_descriptionindutch", descindutch);
                    ld.Attributes.Add("new_descriptioninfrench", descinfrench);
                }
            }
            else if (LanguageId == "1031")//German
            {
                if (leadDetail.Entities[0].Attributes.Contains("new_topicingerman"))
                {
                    topicinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicingerman"]), "es", _Authenticationkey);
                      topicinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicingerman"]), "it", _Authenticationkey);
                    topicinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicingerman"]), "en", _Authenticationkey);
                    topicindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicingerman"]), "nl", _Authenticationkey);
                    topicinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicingerman"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_topicinspanish", topicinspanish);
                      ld.Attributes.Add("new_topicinitalian", topicinitly);
                    ld.Attributes.Add("subject", topicinenglish);
                    ld.Attributes.Add("new_topicindutch", topicindutch);
                    ld.Attributes.Add("new_topicinfrench", topicinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_jobtitleingerman"))
                {
                    jobtitleinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleingerman"]), "es", _Authenticationkey);
                     jobtitleinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleingerman"]), "it", _Authenticationkey);
                    jobtitleinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleingerman"]), "en", _Authenticationkey);
                    jobtitleindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleingerman"]), "nl", _Authenticationkey);
                    jobtitleinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleingerman"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_jobtitleinspanish", jobtitleinspanish);
                      ld.Attributes.Add("new_jobtitleinitalian", jobtitleinitly);
                    ld.Attributes.Add("jobtitle", jobtitleinenglish);
                    ld.Attributes.Add("new_jobtitleindutch", jobtitleindutch);
                    ld.Attributes.Add("new_jobtitleinfrench", jobtitleinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_descriptioningerman"))
                {
                    descinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioningerman"]), "es", _Authenticationkey);
                      descinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioningerman"]), "it", _Authenticationkey);
                    descinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioningerman"]), "en", _Authenticationkey);
                    descindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioningerman"]), "nl", _Authenticationkey);
                    descinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioningerman"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_descriptioninspanish", descinspanish);
                    ld.Attributes.Add("new_descriptioninitalian", descinitly);
                    ld.Attributes.Add("description", descinenglish);
                    ld.Attributes.Add("new_descriptionindutch", descindutch);
                    ld.Attributes.Add("new_descriptioninfrench", descinfrench);
                 }
                }
            else if (LanguageId == "1043")//Dutch
            {
                if (leadDetail.Entities[0].Attributes.Contains("new_topicindutch"))
                {
                    topicinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicindutch"]), "es", _Authenticationkey);
                    topicinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicindutch"]), "it", _Authenticationkey);
                    topicingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicindutch"]), "de", _Authenticationkey);
                    topicinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicindutch"]), "en", _Authenticationkey);
                    topicinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicindutch"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_topicinspanish", topicinspanish);
                    ld.Attributes.Add("new_topicinitalian", topicinitly);
                    ld.Attributes.Add("new_topicingerman", topicingerman);
                    ld.Attributes.Add("subject", topicinenglish);
                    ld.Attributes.Add("new_topicinfrench", topicinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_jobtitleindutch"))
                {
                    jobtitleinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleindutch"]), "es", _Authenticationkey);
                    jobtitleinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleindutch"]), "it", _Authenticationkey);
                    jobtitleingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleindutch"]), "de", _Authenticationkey);
                    jobtitleinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleindutch"]), "en", _Authenticationkey);
                    jobtitleinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleindutch"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_jobtitleinspanish", jobtitleinspanish);
                    ld.Attributes.Add("new_jobtitleinitalian", jobtitleinitly);
                    ld.Attributes.Add("new_jobtitleingerman", jobtitleingerman);
                    ld.Attributes.Add("jobtitle", jobtitleinenglish);
                    ld.Attributes.Add("new_jobtitleinfrench", jobtitleinfrench);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_descriptionindutch"))
                {
                    descinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptionindutch"]), "es", _Authenticationkey);
                    descinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptionindutch"]), "it", _Authenticationkey);
                    descingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptionindutch"]), "de", _Authenticationkey);
                    descinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptionindutch"]), "en", _Authenticationkey);
                    descinfrench = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptionindutch"]), "fr", _Authenticationkey);

                    ld.Attributes.Add("new_descriptioninspanish", descinspanish);
                    ld.Attributes.Add("new_descriptioninitalian", descinitly);
                    ld.Attributes.Add("new_descriptioningerman", descingerman);
                    ld.Attributes.Add("description", descinenglish);
                    ld.Attributes.Add("new_descriptioninfrench", descinfrench);
                }
                }
            else if (LanguageId == "1036")//French
            {
                if (leadDetail.Entities[0].Attributes.Contains("new_topicinfrench"))
                {
                    topicinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinfrench"]), "es", _Authenticationkey);
                    topicinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinfrench"]), "it", _Authenticationkey);
                    topicingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinfrench"]), "de", _Authenticationkey);
                    topicindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinfrench"]), "nl", _Authenticationkey);
                    topicinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_topicinfrench"]), "en", _Authenticationkey);

                    ld.Attributes.Add("new_topicinspanish", topicinspanish);
                    ld.Attributes.Add("new_topicinitalian", topicinitly);
                    ld.Attributes.Add("new_topicingerman", topicingerman);
                    ld.Attributes.Add("new_topicindutch", topicindutch);
                    ld.Attributes.Add("subject", topicinenglish);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_jobtitleinfrench"))
                {
                    jobtitleinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinfrench"]), "es", _Authenticationkey);
                    jobtitleinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinfrench"]), "it", _Authenticationkey);
                    jobtitleingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinfrench"]), "de", _Authenticationkey);
                    jobtitleindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinfrench"]), "nl", _Authenticationkey);
                    jobtitleinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_jobtitleinfrench"]), "en", _Authenticationkey);

                    ld.Attributes.Add("new_jobtitleinspanish", jobtitleinspanish);
                    ld.Attributes.Add("new_jobtitleinitalian", jobtitleinitly);
                    ld.Attributes.Add("new_jobtitleingerman", jobtitleingerman);
                    ld.Attributes.Add("new_jobtitleindutch", jobtitleindutch);
                    ld.Attributes.Add("jobtitle", jobtitleinenglish);
                }
                if (leadDetail.Entities[0].Attributes.Contains("new_descriptioninfrench"))
                {
                    descinspanish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninfrench"]), "es", _Authenticationkey);
                    descinitly = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninfrench"]), "it", _Authenticationkey);
                    descingerman = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninfrench"]), "de", _Authenticationkey);
                    descindutch = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninfrench"]), "nl", _Authenticationkey);
                    descinenglish = commonClass.TranslateText(Convert.ToString(leadDetail.Entities[0].Attributes["new_descriptioninfrench"]), "en", _Authenticationkey);

                    ld.Attributes.Add("new_descriptioninspanish", descinspanish);
                    ld.Attributes.Add("new_descriptioninitalian", descinitly);
                    ld.Attributes.Add("new_descriptioningerman", descingerman);
                    ld.Attributes.Add("new_descriptionindutch", descindutch);
                    ld.Attributes.Add("description", descinenglish);
                }

            }
            objCommon.service.Update(ld);

        }
    }


This entry was posted in :

0 comments:

Post a Comment