Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Error stringifying an object when calling Custom Action in OData

$
0
0

I have created a custom action that takes in an input parameter called paramsInput (string).

I want to send to said action a stringified JSON object so that it may be deserialized inside the action.

public void Execute(IServiceProvider serviceProvider)
{
    try
    {
        _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        _crmService = factory.CreateOrganizationService(_context.UserId);

        string paramsInput = _context.InputParameters.Contains("paramsInput") ? _context.InputParameters["paramsInput"].ToString() : string.Empty;

        if (paramsInput != string.Empty)
        {
            InputParams inputParameters = JsonConvert.DeserializeObject<InputParams>(paramsInput);

            // logic here...       
        }
    }
    catch (Exception ex)
    {
        // handle exception...
    }
}

I have also created a generic function that recieves the action's name and the parameters to send and calls the action via OData:

public async Task<bool> CallMessage(string action, JObject parameters)
{
    RegenerateAccess();  // Microsoft Online

    bool succeeded = false;
    string urlAPI = "/api/data/v9.1/" + action;
    using (HttpClient client = new HttpClient())
    {
        // Setup client configuration here...
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, urlAPI);
        string jsonObj = JsonConvert.SerializeObject(parameters);
        request.Content = new StringContent(jsonObj, Encoding.UTF8, "application/json");
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

        //Set the access token
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _authResult.AccessToken);
        HttpResponseMessage response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        succeeded = true;
        return succeeded;
    }
}

My issue is that I'm getting a Bad Request 400 back, and the response reads - "Microsoft.OData.ODataException: An unexpected 'StartObject' node was found for property named 'paramsInput' when reading from the JSON reader. A 'PrimitiveValue' node was expected."

My parameters JObject is structured as follows:

{

    "paramsInput" : {

          "id" : "xxxx-xxxx...", // some guid

          "code" : 1 // int

    }

}

or via code:

Guid guid = Guid.NewGuid();
JObject parameters = new JObject();
parameters.Add("id", guid);
parameters.Add("code", 1);
JObject wrapper = new JObject();
wrapper.Add("paramsInput", parameters);

// send wrapper JObject to CallMessage

I don't want to change the structure of CallMessage function as it is a generic function that may be used multiple times by other sources.


Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>