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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Error stringifying an object when calling Custom Action in OData

(0) ShareShare
ReportReport
Posted on by 8

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(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 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.

I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 165

#2
ManoVerse Profile Picture

ManoVerse 156 Super User 2026 Season 1

#3
Zhilan Profile Picture

Zhilan 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans