Javascript code to get the System User settings: like dateformat, email etc in CRM

This code sample uses the timezonedefinition entity.

//Set the CrmService

CrmService s = new CrmService();CrmAuthenticationToken token = new CrmAuthenticationToken();”MicrosoftCRM”;CredentialCache.DefaultCredentials;RetrieveUserSettingsSystemUserRequest req = new RetrieveUserSettingsSystemUserRequest();new AllColumns();new Guid(“{304d27d5-39d0-dc11-aa32-0003ff33509e}”);RetrieveUserSettingsSystemUserResponse res = (RetrieveUserSettingsSystemUserResponse)s.Execute(req);usersettings userSettings = (usersettings)res.BusinessEntity;

token.AuthenticationType = 0;

token.OrganizationName =

s.CrmAuthenticationTokenValue = token;

s.Credentials = System.Net.

//RetrieveUserSettings TimeZone Code

req.ColumnSet =

req.EntityId =

//Query the timezonedefinition Entity

QueryExpression query = new QueryExpression();

//Target the userinterfacename column e.g. (GMT-08:00) Pacific Time (US & Canada)ColumnSet Columnset = new ColumnSet();new String[]{“userinterfacename”}; EntityName.timezonedefinition.ToString();

Columnset.Attributes =

query.ColumnSet = Columnset;

query.EntityName =

//Retrieve the TimeZone the match the user codeConditionExpression condition = new ConditionExpression();”timezonecode”;ConditionOperator.Equal;new object[]{ userSettings.timezonecode.Value.ToString() };FilterExpression filter = new FilterExpression();new ConditionExpression[]{ condition };LogicalOperator.And;BusinessEntityCollection resultTimeZone = service.RetrieveMultiple( query );

condition.AttributeName =

condition.Operator =

condition.Values =

filter.Conditions =

filter.FilterOperator =

query.Criteria = filter;

Console.WriteLine( ((timezonedefinition)resultTimeZone.BusinessEntities[0]).userinterfacename );

//Result: (GMT-08:00) Pacific Time (US & Canada)

Here is the complete example.

CrmService

token.AuthenticationType = 0;

token.OrganizationName =

service.CrmAuthenticationTokenValue = token;

service.Credentials = System.Net.

/* RetrieveUserSettings */

RetrieveUserSettingsSystemUserRequest req = new RetrieveUserSettingsSystemUserRequest();new AllColumns();new Guid(“{304d27d5-39d0-dc11-aa32-0003ff33509e}”);RetrieveUserSettingsSystemUserResponse res = (RetrieveUserSettingsSystemUserResponse)service.Execute(req);usersettings userSettings = (usersettings)res.BusinessEntity;

/* GetAllTimeZones */

GetAllTimeZonesWithDisplayNameRequest timeZonesRequest = new GetAllTimeZonesWithDisplayNameRequest();timeZonesRequest.LocaleId = 1033;

/* Match User Timezone */

foreach (BusinessEntity businessEntity in timeZonesResponse.BusinessEntityCollection.BusinessEntities) timezonedefinition timeZone = (timezonedefinition)businessEntity; if( userSettings.timezonecode.Value == timeZone.timezonecode.Value ) Console.WriteLine(timeZone.userinterfacename);

{

}

GetAllTimeZonesWithDisplayNameResponse timeZonesResponse = (GetAllTimeZonesWithDisplayNameResponse)service.Execute(timeZonesRequest);

req.ColumnSet =

req.EntityId =

service = new CrmService();CrmAuthenticationToken token = new CrmAuthenticationToken();”Organization Name”;CredentialCache.DefaultCredentials;

RetrieveUserSettingsSystemUserRequest Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: