Skip to content

Commit

Permalink
Use OOB RetrieveSetting Function in GetSetting API
Browse files Browse the repository at this point in the history
  • Loading branch information
drivardxrm committed Dec 7, 2021
1 parent e4c308c commit 150a297
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 51 deletions.
29 changes: 16 additions & 13 deletions Driv.CustomApis.Code/API/GetSetting.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Driv.CustomApis.Helpers;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using XrmVision.Extensions.Extensions;

Expand Down Expand Up @@ -27,35 +28,37 @@ public void Execute(LocalPluginContext localcontext)
//default values
outputparameters["Exists"] = false;
outputparameters["ValueString"] = string.Empty;


var definition = xrmcontext.GetSettingDefinitionFor(settingname);
if (definition != null)
{
outputparameters["Exists"] = true;

var appmodule = xrmcontext.GetAppModuleByUniqueName(appname);

var organizationsetting = xrmcontext.GetOrganizationSettingFor(definition);
//Error handling
if (!string.IsNullOrEmpty(appname))
{
var appmodule = xrmcontext.GetAppModuleByUniqueName(appname);
if (appmodule == null)
{
throw new InvalidPluginExecutionException($"App with name {appname} doesn't exists");
}
}

var appsetting = xrmcontext.GetAppSettingFor(definition, appmodule);
// use OOB RetrieveSEtting function
var settingdetail = service.RetrieveSetting(settingname, appname);

var valuestring = appsetting != null ?
appsetting.Value :
(organizationsetting != null ?
organizationsetting.Value :
definition.DefaultValue);
outputparameters["ValueString"] = settingdetail.Value;

outputparameters["ValueString"] = valuestring;
switch (definition.DataType)
{
case settingdefinition_datatype.Boolean:

outputparameters["ValueBool"] = valuestring.ToLower() == "true";
outputparameters["ValueBool"] = settingdetail.Value.ToLower() == "true";
break;

case settingdefinition_datatype.Number:
outputparameters["ValueDecimal"] = decimal.Parse(valuestring);
outputparameters["ValueDecimal"] = decimal.Parse(settingdetail.Value);

break;

Expand Down
6 changes: 1 addition & 5 deletions Driv.CustomApis.Code/Helpers/AppModuleHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace Driv.CustomApis.Helpers
{
Expand Down
5 changes: 0 additions & 5 deletions Driv.CustomApis.Code/Helpers/ChoiceHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Driv.CustomApis.Helpers
{
Expand Down
6 changes: 1 addition & 5 deletions Driv.CustomApis.Code/Helpers/EnvironmentVariableHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace Driv.CustomApis.Helpers
{
Expand Down
34 changes: 16 additions & 18 deletions Driv.CustomApis.Code/Helpers/SettingsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Driv.CustomApis.Helpers
{
Expand All @@ -11,20 +9,20 @@ public static class SettingsHelper
public static SettingDefinition GetSettingDefinitionFor(this XrmContext xrmcontext, string settingname)
=> xrmcontext.SettingDefinitionSet.SingleOrDefault(s => s.UniqueName == settingname);

public static OrganizationSetting GetOrganizationSettingFor(this XrmContext xrmcontext, SettingDefinition definition)
=> definition.IsOverridable == true &&
definition.OverridableLevel == settingdefinition_overridablelevel.AppOnly ?
null :
xrmcontext.OrganizationSettingSet.SingleOrDefault(o => o.SettingDefinitionId != null &&
o.SettingDefinitionId.Id == definition.Id);
public static SettingDetail RetrieveSetting(this IOrganizationService service, string settingname, string appname)
{
// USING OOB RetrieveSetting Function
// https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/retrievesetting?view=dynamics-ce-odata-9

var request = new OrganizationRequest("RetrieveSetting");
request.Parameters.Add("SettingName", settingname);
request.Parameters.Add("AppUniqueName", appname);

var response = service.Execute(request);


return response.Results["SettingDetail"] as SettingDetail;
}

public static AppSetting GetAppSettingFor(this XrmContext xrmcontext, SettingDefinition definition, AppModule appmodule)
=> definition.IsOverridable == true &&
definition.OverridableLevel != settingdefinition_overridablelevel.OrganizationOnly &&
appmodule == null ? null :
xrmcontext.AppSettingSet.SingleOrDefault(a => a.SettingDefinitionId != null &&
a.SettingDefinitionId.Id == definition.Id &&
a.ParentAppModuleId != null &&
a.ParentAppModuleId.Id == appmodule.Id);
}
}
6 changes: 1 addition & 5 deletions Driv.CustomApis.Code/Helpers/UserHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace Driv.CustomApis.Helpers
{
Expand Down

0 comments on commit 150a297

Please sign in to comment.