Question

I am creating an app part working with dates. I need to show date by users regional settings as well as in SharePoint.

SharePoint Page have _spPageContextInfo.currentCultureName, where is current regional settings, but apppart has only url parameter lang (but this is not a regional setting for current user)

Is it really necessary to get the regional settings by calling UserProfileService?

var peopleManager = new PeopleManager(ctx);
var userProperties = peopleManager.GetMyProperties();
ctx.Load(userProperties);
ctx.ExecuteQuery();

Is there a better way?

Was it helpful?

Solution 2

My final code for getting correct LCID:

    RegionalSettings _regionalSettings;
    PersonProperties _userProfile;
    public PersonProperties UserProfile
    {
        get
        {
            if (_userProfile == null)
            {
                var peopleManager = new PeopleManager(_clientContext);
                _userProfile = peopleManager.GetMyProperties();
                _clientContext.Load(_userProfile);
                _clientContext.ExecuteQuery();
            }
            return _userProfile;
        }
    }

    public uint CurrentLCID
    {
        get
        {
            if (_regionalSettings == null)
            {
                _regionalSettings = _clientContext.Web.RegionalSettings;
                _clientContext.Load(_regionalSettings);
                _clientContext.ExecuteQuery();
            }
            var lcid = _regionalSettings.LocaleId;
            try
            {
                if (UserProfile.UserProfileProperties.ContainsKey("SPS-Locale"))
                {
                    var userLCID = lcid;
                    if (UInt32.TryParse(UserProfile.UserProfileProperties["SPS-Locale"], out userLCID))
                        if (userLCID > 0)
                            lcid = userLCID;
                }
            }
            catch { }
            _currentCulture = new CultureInfo((int)lcid);
            return lcid;
        }
    }

OTHER TIPS

In CSOM API Web class exposes Web.RegionalSettings property to get the regional settings that are currently implemented on the website.

Example

var regionalSettings = ctx.Web.RegionalSettings;
ctx.Load(regionalSettings);
ctx.ExecuteQuery();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top