web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nuno's Dynamic CRM Blog / Custom Actions: set and get...

Custom Actions: set and get objects in input or output parameters

Nuno Profile Picture Nuno 2,005

When creating custom actions it is possible to create input and/or output parameters of all the types displayed on the next picture.

custom_action_in_out_parameters

Despite the available parameters types cover most of all the needs we face as developers, we may face some situations where it would be great if we could set a parameter as any other type. Well, in fact it is possible a work-around! Next is outlined an example how to achieve that.

Let us suppose in an action there is the need to access a reporting server. The next class models an object where the data to access the reporting server may be set and get:

public class ReportServer
{
   public String Url { get; set; }
   public String UserDomain { get; set; }
   public String UserName { get; set; }
   public String UserPassord { get; set; }
}

In the action, instead of creating an input parameter for each ReportServer class property:

  • Create an input parameter named ReportServer string type to pass into the action the ReportServer object serialized as a JSON string;
  • Retrieve the ReportServer object inside the action, de-serializing the JSON string in the input parameter ReportServer.

To serialize the object and de-serialize JSON string the next code may be used:

class JsonHelper
{
   internal static string JsonSerializer<T>(T t)
   {
      string jsonString = null;
      using (MemoryStream ms = new MemoryStream())
      {
         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
         ser.WriteObject(ms, t);
         jsonString = Encoding.UTF8.GetString(ms.ToArray());
      }
      return jsonString;
   }

   internal static T JsonDeserialize<T>(string jsonString)
   {
      DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
      MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
      T obj = (T)ser.ReadObject(ms);
      return obj;
   }
}

When calling the action, set the input parameter with the ReportServer object as string:

// CALL ACTION
ReportServer reportServer = new ReportServer();
OrganizationRequest request = new OrganizationRequest("new_Teste");
request.Parameters.Add("ReportServer", JsonHelper.JsonSerializer<ReportServer>((reportServer)));
service.Execute(request);

Inside the action the ReportServer object is rebuilt de-serializing the JSON string.

// Access the Input Parameter String Type Inside the ACTION CodeActivity
ReportServer reportServer = JsonHelper.JsonDeserialize<ReportServer>(ReportServer.Get<String>(context));

This approach is obviously valid for any class and for any type of its properties.

In custom actions passing any type of object in input and/or output parameters can be achieved through a parameter string type which holds the object in a JSON string.



This was originally posted here.

Comments

*This post is locked for comments