Tuesday 15 May 2018

how to change the user language in Dynamic 365 using Custom Workflow

Change Microsoft Dyanamics 365 Language

Description:

In this example we explain that how to change the user setting in Dynamic CRM using custom workflow. Or how to change the Language of the Dynamic CRM using custom workflow. Or how to update the CRM user UILanguage and HelpLanguage using custom workflow in Dynamic 365.or how to change user setting based on the Language of the CRM user using custom workflow or plugin in Dynamic 365.

Here we demonstrate that how to change regional and language option using custom workflow. Or how to change language in Dynamic CRM dynamically based on user language change using custom workflow.

Here we have created one lookup of Language in User Form and when user change the Look up with selection of different language based on that custom workflow will be call and change the Language (UiLanguage and HelpLanguage) of the Dynamic CRM as per lookup change.

So below is the custom workflow code that will help you to how to change Language in Dynamic 365.

Custom Workflow:

namespace ChangeLanguage.Language
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
    using System.Activities;
    using Microsoft.Xrm.Sdk.Metadata;
    using System.Runtime.Remoting.Contexts;
    using System.IO;

    public sealed class LanguageChange : CodeActivity
    {

        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"

            EntityReference userReference = this.User.Get(executionContext);

            var userDetail = objCommon.service.Retrieve("systemuser", userReference.Id, new ColumnSet(true));
            EntityReference selectedLanguage = (EntityReference)userDetail.Attributes["new_userlanguageid"];
            var LanguageDetail = objCommon.service.Retrieve("new_language", selectedLanguage.Id, new ColumnSet(true));
            // throw new InvalidWorkflowException("Old Text is  :" + selectedLanguage.Id + " New Text Is :" + this.HelpLanguageId.Get(executionContext));
            int pagingLimit = this.PagingLimit.Get(executionContext);
            int advancedFindStartupMode = this.AdvancedFindStartupMode.Get(executionContext);
            int timeZoneCode = this.TimeZoneCode.Get(executionContext);
            int helpLanguageId = this.HelpLanguageId.Get(executionContext);
            int uiLanguageId = this.UILanguageId.Get(executionContext);
            int defaultCalendarView = this.DefaultCalendarView.Get(executionContext);

            objCommon.tracingService.Trace(String.Format("UserID: {0} ", userReference.Id.ToString()));
            #endregion

            Entity newSettings = new Entity("usersettings");
            newSettings.Attributes.Add("systemuserid", userReference.Id);
            if (pagingLimit != 0)
            {
                newSettings.Attributes.Add("paginglimit", pagingLimit);
            }
            if (advancedFindStartupMode == 1 || advancedFindStartupMode == 2)
            {
                newSettings.Attributes.Add("advancedfindstartupmode", advancedFindStartupMode);
            }
            if (timeZoneCode != 0)
            {
                newSettings.Attributes.Add("timezonecode", timeZoneCode);
            }
            if (helpLanguageId != 0)
            {
                // newSettings.Attributes.Add("helplanguageid", helpLanguageId);
                newSettings.Attributes.Add("helplanguageid",Convert.ToInt32(LanguageDetail.Attributes["new_languagecode"]));
            }
            if (uiLanguageId != 0)
            {
                //  newSettings.Attributes.Add("uilanguageid", uiLanguageId);
                newSettings.Attributes.Add("uilanguageid", Convert.ToInt32(LanguageDetail.Attributes["new_languagecode"]));
            }
            if (defaultCalendarView == 0 || defaultCalendarView == 1 || defaultCalendarView == 2)
            {
                newSettings.Attributes.Add("defaultcalendarview", defaultCalendarView);
            }

            objCommon.service.Update(newSettings);


        }


     

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

        [RequiredArgument]
        [Input("PagingLimit")]
        [Default("0")]
        public InArgument<int> PagingLimit { get; set; }
        //Specify how many records per view. Value can be 25,50,75,100,250 

        [RequiredArgument]
        [Input("AdvancedFindStartupMode")]
        [Default("1")]
        public InArgument<int> AdvancedFindStartupMode { get; set; }
        //Specify AdvancedFind mode. 1:simple, 2:detail.

        [RequiredArgument]
        [Input("TimeZoneCode")]
        [Default("0")]
        public InArgument<int> TimeZoneCode { get; set; }
        //Specify TimeZoneCode for users. Use Get-CrmTimeZones to see all options 0 for ignore.

        [RequiredArgument]
        [Input("HelpLanguageId")]
        [Default("0")]
        public InArgument<int> HelpLanguageId { get; set; }
        //Specify Unique identifier of the Help language. 0 for ignore

        [RequiredArgument]
        [Input("UILanguageId")]
        [Default("0")]
        public InArgument<int> UILanguageId { get; set; }
        //Specify Unique identifier of the language in which to view the user interface (UI). 0 for ignore


        [RequiredArgument]
        [Input("DefaultCalendarView")]
        [Default("0")]
        public InArgument<int> DefaultCalendarView { get; set; }
    }
}




This entry was posted in :

1 comments:

  1. how you created the language lookup on the user form ?

    ReplyDelete