简体   繁体   中英

Return a custom-defined DTO class from a Dynamics365 code activity

I'm trying to create a Dynamics365 extension that can be called from outside the Dynamics world, to fetch some data.

The first approach I was told to investigate is creating an "action" inside Dynamics - which entails creating a CodeActivity in C# code, that then gets added to the OData feeds provided by Dynamics - sounds quite compelling!

So I tried to set up a CodeActivity class to handle my task - getting a ProjectId (Guid) and Language as parameters from the caller, and then inside Dynamics, I will need to fetch a few entities, extract some information from them, and bundle everything up into a DTO class I've defined (which doesn't match any of the entities in the Dynamics world - so that's why I need a separate DTO class to hold just the data I need to provide) and return that from the code activity.

My class looks like this:

public class GetProject : System.Activities.CodeActivity
{
    [RequiredArgument]
    [Input("ProjectId")]
    public InArgument<Guid> ProjectId { get; set; }

    [Input("Language")]
    public InArgument<string> Language { get; set; }

    [Output("ProjectResponse")]
    public OutArgument<WebPortalDto> Response { get; set; }

    /// <summary>When implemented in a derived class, performs the execution of the activity.</summary>
    /// <param name="context">The execution context under which the activity executes.</param>
    protected override void Execute(CodeActivityContext context)
    {
        // TODO - implement logic
    }
}

I was able to compile the assembly - but upon registration with the Plugin Registration tool, I first got an error that the datatype of InParameter #1 is not supported (I take it that's the ProjectId parameter, with the GUID type) - so ok, I changed that to string, no biggie.

But my bigger problem comes now: the datatype of the OutParameter #1 is also unsupported....... looking at all the other code activity classes in our project, I saw that they pretty much all returned an EntityReference - a reference to a built-in Dynamics entity.

But in my case, I really CANNOT do this. I must return this DTO class (which just holds a bunch of INT and STRING properties, mostly). How can I do that from a Dynamics365 plugin / code activity?

First of all keep in mind that calling an action from outside Dynamics will still require Dynamics authentication, they are not exposed to be consumed openly.

An alternative you can consider to create a webservice (hosted by you somewhere) or an Azure Function that will access the Dynamics data and return the output you want.

If you still want to proceed with an action and a custom workflow activity, you are limited to the types you can use, as you already found you can't use a custom object, the best choice in your case will be to return a string containing the JSON of your DTO.

Ps: Guid is not a valid type, to reference a specific record inside Dynamics you need to use EntityReference (that is a class with a Guid and the logical name of the entity)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM