Advertisements

[Code Snippet] Dynamics Post Assign Plugin

This article is a response to one of the blog questions. 

Below is the code snippet for Post Assign (i.e., Ownership change) plug-in.

In the ‘Post Assign’ plug-in you get 2

  • Current record as ‘Entity Reference’ in ‘Target‘ attribute.
  • Records new Owner (i.e., Owner or Team) in the ‘Assignee‘ attribute.

EntityReference entRefCurrentRecord = null;
EntityReference entRefAssignee = null;

// Get current record as Entity Reference
if ((context.InputParameters.Contains(“Target”)) && (context.InputParameters[“Target”] is EntityReference))
{
entRefCurrentRecord = (EntityReference)context.InputParameters[“Target“];
}

// Get User/Team whom the record assigned to from ‘Assignee’ property of Context
if ((context.InputParameters.Contains(“Assignee”)) && (context.InputParameters[“Assignee”] is EntityReference))
{
entRefAssignee = (EntityReference)context.InputParameters[“Assignee“];
}

// Write your business logic

  • You might want to register Pre/Post images on the Plug-in step, to read other attributes of the record.

🙂

Advertisements
Categories: CRM Tags: , ,

C# Error while calling API – Could not create SSL/TLS secure channel

Other day, we were getting following error while calling SSL enables 3rd party API from C# console.

The request was aborted: Could not create SSL/TLS secure channel

Fix:

  • Add below statement before making your API call, which validates the SSL certification.

ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};

  • Include following namespaces

using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

🙂

 

Categories: Misc Tags: , ,

Dynamics/ADX Portals – Create Notes using SDK

As part of an integration requirement, we had to create ‘Notes’ (i.e., Annotation) with attachment using SDK.

Key Notes:

  • ‘Subject’ and ‘Notetext’ field values of Portal Notes follows a specific pattern than that of Dynamics.  Values must be set as follows:
    • SubjectNote created on {DateTime.Now.ToString()} by {Portal User Name} [contact:{PortalUserGUID}]
      • If the Portal user name is ‘Rajeev P’ and Portal user GUID(i.e.,Contact record’s GUID) is ‘2510ae27-7289-e911-a958-001dd800d97c’, Subject will be Note created on {DateTime.Now.ToString()} by Rajeev P [contact:2510ae27-7289-e911-a958-001dd800d97c]
    • Notetext*WEB*
  • If you don’t pass Portal User’s GUID in ‘Subject’ field, Portal shows the Notes record created by as User whom the Portal was configured.

Code to create Annotation:

Below is the code snippet

var noteSubject = “Note created on {DateTime.Now.ToString()} by Rajeev P [contact:2510ae27-7289-e911-a958-001dd800d97c]“;

var entAnnotation = new Entity(“annotation”);
entAnnotation[“subject“] = noteSubject;
entAnnotation[“notetext“] = “*WEB*”;
entAnnotation[“filename”] = {File_name}; // Set FileName
entAnnotation[“documentbody”] = {Base64String}; // Pass document body in Base64 format
entAnnotation[“isdocument”] = true;
entAnnotation.Attributes[“objectid”] = new EntityReference({RegardingSchemaName}, {RegardingGuid}); // Set Schema and GUID
entAnnotation.Attributes[“objecttypecode”] = {RegardingSchemaName}; // Set Schema

var noteId = crmService.Create(entAnnotation);

  • Note : Replace values in {} with actual values.

🙂

 

 

[PowerApps Portal (preview)] Build your first portal

PowerApps Portalsprovides ability to build low-code, responsive websites which allow external users to interact with the data stored in the Common Data Service.

Refer article for introduction. PowerApps Portals are in Preview now and lets see how to get started with a simple portal.

Perquisites:

Steps to build your first Portal:

  • Login to PowerApps maker Portal
  • Pick the right ‘Environment’. Refer ‘Notes’ section below to know more about ‘Environments’.
  • Click on ‘Portal from blank (preview)’ link.

PAPP_Portal_15

  • In the pop-up, fill ‘Name’, ‘Address’ and ‘Language’ and click ‘Create’.

PAPP_Portal_2

  • Portal provisioning takes time and you will be notified once done.

PAPP_Portal_5

  • Post provisioning, click on ‘Edit’ which will redirect you to Portal editor.

PAPP_Portal_16

  • You get pre configured ‘Home’ page and ‘Child Pages’.
  • To add a new Page,
    • Either click on ‘+New page’ from ribbon
    • Or Right Click , ‘Home’ and add a New Page.

PAPP_Portal_6

  • I have added a new ‘Accounts’ page and filled the details on the right side ‘Component’ panel.
  • To create a new ‘Account’ form, click on ‘Form’ and choose Entity, Form and Mode from the ‘Component’ panel.

PAPP_Portal_8

  • To add a new ‘Entity List’ control, click on ‘List’ and on the right hand side, select ‘Entity’ and ‘Views’
    • Note: In the screen below, I’ve added an existing ‘List’. The properties are same for new ‘List’ as well.

PAPP_Portal_7

  • To map the Account ‘Form’ with ‘List’, set properties as below

PAPP_Portal_9.PNG

  • At any point of time, to view the changes, click on  ‘Browse website’ on top right corner.
  • Home Screen

PAPP_Portal_13.PNG

  • Accounts Screen

PAPP_Portal_14

Notes:

  • To use the Dynamics CE entities, you have to select the CE ‘Environment’ in your maker portal, before the start of Portal creation.
    • A ‘default’ environment gets created by default. If you pick ‘default’ environment, you will need to create a new Environment with CDS database.

PAPP_Portal_17

PAPP_Portal_18

New Environment

  • We can still create Portal from the ‘Dynamics Admin Center’.

PAPP_Portal_3

  • PowerApps portal got ‘.powerappsportals.com’ as domain and regular portal will be of ‘.microsoftportals.com’ domain.

PAPP_Portal_4

  • You can still create/update portal components (i.e., Entity List, Forms, Web Templates) from Dynamics web application.
  • Clear cache using ‘/_service\about‘ is still available in PowerApps portal.

PAPP_Portal_10

🙂

 

 

Post a File and Data to SandBlast API using Postman and generate C# code

We got a requirement to use SandBlast Threat Extraction  process, for cleansing the files.

SandBlast Threat Extraction, is a solution by ‘Check Point’ company,  which removes exploitable content, including active content and embedded objects, reconstructs files to eliminate potential threats.

‘Threat Extraction’ Upload API, requires following parameters

  • Header
    • Authorization
    • Content-Type
  • Body
    • File Content
    • request (This is string)

If you notice, we need to pass ‘File’ as well as ‘Data’ to the API.

As of now, Sandblast doesn’t have a native .Net library to make API calls. So I’ve used Postman tool as a starting point for my development.

Below is the step-by-step, to pass ‘File’ and ‘Data’ in request ‘Body’, using Postman

  • Open Postman tool
  • Create a new ‘Request’

PM_2

  • Set the method as ‘Post’ and URL.
  • From the ‘Body’ tab, select ‘form-data’ option
  • To pass the ‘File’,  add a new ‘KEY’ of type ‘File’ and browse the file in ‘VALUE’ parameter.
  • To pass the ‘Data’, add a new ‘KEY’ of type ‘Text’ and set the ‘VALUE’

PM_1

Generate C# code from the Request:

  • Postman has an amazing feature to generate the code in your required language.
  • Click on ‘Code’ link and for C#, choose ‘C# (RestSharp)’ option.

PM_3

If anyone having struggling to use Sandblast API’s, post in comments section and I will address.

🙂

 

Categories: Misc Tags: , , ,

HTML & JScript – Read and Encode file

We got a requirement in ADX portal, to read and convert the file content to Base64String , which eventually will be submitted to an external API.

Below is the code snippet to read the File content, browsed using the HTML ‘File Upload’ control:

function readFileAsText(){
try {
// Read the browsed file from ‘File Upload’ control.
var fileToLoad = document.getElementById(“fileOE“).files[0];

var fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
// Set the file content to variable
var textFromFileLoaded = fileLoadedEvent.target.result;
alert(“File Content – ” + textFromFileLoaded);

// Using ‘window.btoa’ encode the plain text to Base 64
var convertedString = window.btoa(unescape(encodeURIComponent(textFromFileLoaded)));
alert(“Base 64 content – ” + convertedString);
};

fileReader.readAsText(fileToLoad, “UTF-8”);
} catch (e) {
alert(“Error in readFileAsText(); Error – ” + e.description);
}
}

<body>
<label for=”fileOE”>Pick the file </label><br>
<input type=”file” id=”fileOE” />
<input type=”button” value=”Upload” onclick=”readFileAsText()” />
</body>

  • Execute the code and you will get output as below
jScript_1

File content as text

jScript_2

Encoded File Content (Base 64)

🙂

Categories: Misc Tags: , , , ,

Dynamics CE – Issue while updating ‘Annotation’ record using SDK

Other day, we encountered following error in our plug-in, which has the logic to retrieve and update the ‘objectid’ field (i.e., Associated record) of Annotation record.

‘new_myentity’ with ID XXX-XXXX-XXXX-XXX does not exists

Below is the plug-in code:

var entAnnotation=new Entity(“annotation”);
entAnnotation.Attributes[“objectid”] = new EntityReference(“new_myentity”,”{GUID}”);
crmService.Update(entAnnotation);

We are trying to update ‘objectid’ and from the error, its clear that Object ID (i.e.,GUID) of the ‘new_myentity’ entity does not exists in the application.

But we know that the GUID is of a valid ‘new_myentity’ entity, as that was retrieved using ‘Query Expression’ in previous plug-in statement.

Reason and Fix:

  • We must also set ‘objecttypecode’ field as the schema name of the ‘objectid’ of the Annotation record.
  • In our case, its ‘new_myentity’.
  • Below is the code of Annotation update setting ‘objecttypecode’ field.

var entAnnotation=new Entity(“annotation”);
entAnnotation.Attributes[“objectid”] = new EntityReference(“new_myentity“,”{GUID}”);
entAnnotation.Attributes[“objecttypecode”] = “new_myentity“;
crmService.Update(entAnnotation);

Issue was annoying and took sometime to troubleshoot and find the actual reason.

🙂