Call Custom Actions in Dataverse using Web API – Quick Recap

Hi Folks,

Here is how you can quickly call action using Web API, with this method you can execute a single action, function, or CRUD operation. In the below example, let’s see how you can call an action. Here is function…to achieve this..

var formContext = executionContext.getFormContext();
var message = "Please enter a valid NRIC Number";
var uniqueId = "nric_valid";
var NRIC = formContext.getAttribute("new_nric").getValue();
if(NRIC !== null)
{
var execute_ValidateNRIC = {
NRIC: NRIC, // Call this function only when NRIC value which is non-null
getMetadata: function () {
return {
boundParameter: null,
parameterTypes: {
NRIC: { typeName: "Edm.String", structuralProperty: 1 }
},
operationType: 0,
operationName: "new_ValidateNRIC",
outputParameterTypes: {
IsValid: { typeName: "Edm.Boolean" }
}
};
}
};
Xrm.WebApi.execute(execute_new_ValidateNRIC).then(
function success(response) {
if (response.ok) {
response.json().then(function (data) {
if (!data.IsValid) {
formContext.getControl("new_nric").setNotification(message, uniqueId);
} else {
formContext.getControl("new_nric").clearNotification(uniqueId);
}
}).catch(function (error) {
Xrm.Navigation.openAlertDialog("Error occured from Validate NRIC "+error);
});
}
}
).catch(function (error) {
Xrm.Navigation.openAlertDialog(error.message);
}).catch(function (error) {
Xrm.Navigation.openAlertDialog(error.message);
});
}
}

This example details with Unbound action, which is not tagged to any entity, however if in case on Bound action, you will specify the entity name for bound parameter instead of null. You need to specify the Metadata accordingly for your Action. Let’s understand it’s syntax first…

Xrm.WebApi.online.execute(request).then(successCallback, errorCallback);

Parameters

Encrypting/Decrypting a file using Public & Private Key Pair with GnuPG

Hi Folks,

Thank you for visiting my blog today…this is post is mainly for Pro developers. Encryption is crucial to maintain the confidentiality in this digital age for the security of our sensitive information. So here is a blog about it. This is in continuation to my previous blog post on encrypting files using GnuPG.

In this blog post, I will give you sample how you can encrypt/decrypt using GnuPG with command line scripts from C# code.

If you didn’t go through my previous article, I strongly recommend you go through that article below first to understand the background.

Next, in order to encrypt/decrypt a given csv file (taken for simplicity), we can use the following C# codes. For illustration purpose, I have just provided you the logic in the form of a Console.

Encryption:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace eHintsBatchDecryptionTest
{
class Program
{
static void Main(string[] args)
{
string gpgPath = @"D:\Softwares\Kleo Patra\GnuPG\bin\gpg.exe"; //This is the place where you have installed GnuPG Software
string inputFile = "location of input file";
string outputFile = "location of output file";
string passphrase = "passPhrase";
DecryptGPGFile(gpgPath, inputFile, outputFile, passphrase);
}
static void DecryptGPGFile(string gpgPath, string inputFile, string outputFile, string passphrase)
{
using (Process process = new Process())
{
process.StartInfo.FileName = gpgPath;
process.StartInfo.Arguments = $"–batch –yes –pinentry-mode=loopback –passphrase {passphrase} -d -o \"{outputFile}\" \"{inputFile}\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
{
Console.WriteLine("Decryption successful.");
}
else
{
Console.WriteLine("Decryption failed. Error: " + error);
}
}
}
}
}

Decryption:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace BatchDecryptionTest
{
class Program
{
static void Main(string[] args)
{
string gpgPath = @"D:\Softwares\Kleo patra\GnuPG\bin\gpg.exe";//Once GPG installed, you can look for gpg.exe in the bin folder of the installation
string inputFile = "Input encrypted file";//Replace with your gpg encrypted file location
string outputFile = "Decrypted CSV file"; //give it a name for the decrypted file and location, output file path doesnt exists yet, you may give a sample name
string passphrase = "passPhrase";
DecryptGPGFile(gpgPath, inputFile, outputFile, passphrase);
}
static void DecryptGPGFile(string gpgPath, string inputFile, string outputFile, string passphrase)
{
using (Process process = new Process())
{
process.StartInfo.FileName = gpgPath;
process.StartInfo.Arguments = $"–batch –yes –pinentry-mode=loopback –passphrase {passphrase} -d -o \"{outputFile}\" \"{inputFile}\""; //Pass the PassPhrase, Input and Output file paths as parameters
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
{
Console.WriteLine("Decryption successful.");
}
else
{
Console.WriteLine("Decryption failed. Error: " + error);
}
}
}
}
}

All you need is to copy and replace the file locations in the code. Sit back and enjoy encrypting and decrypting with GnuPG. I should say once known, this is the easiest way to encrypt/decrypt from C# code, no strings attached.

If you need any other information, please do let me know in comments.

Cheers,

PMDY