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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

C# call actions through webapi with parameters

(0) ShareShare
ReportReport
Posted on by 2,510

Hi, I got some trouble with passing parameters through the web api to invoke the custom action. I am following this article (https://msdn.microsoft.com/en-us/library/mt742426.aspx) but still confused.  Basically, I need pass 3 parameters, where should I call the action in the uri ?

        public async Task TestActionAsync()
        {
            Console.WriteLine("--Action Test Starts Here--");
            Money amount = new Money() { Value = 300 };
            JObject exeAction = JObject.Parse(@"{ApplicationRef: '8B446743-C6C4-E711-A81C-005056B5FEFA','Amount:"+amount+"',Authcode:'0001'}");
            string actionUri =(how to define the URI)

            response = await SendAsJsonAsync(httpClient, HttpMethod.Post, actionUri, exeAction);




        }


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nuno Profile Picture
    2,005 on at

    Hi,

               JObject exeAction = new JObject();

               exeAction["ApplicationRef"] = "8B446743-C6C4-E711-A81C-005056B5FEFA";

               exeAction["Amount"] = new Money() { Value = 300 };

               exeAction["Authcode"] = "0001";

               string actionUri = "ACTION_NAME";

               response = await SendAsJsonAsync(httpClient, HttpMethod.Post, actionUri, exeAction);

  • sdnd2000 Profile Picture
    2,510 on at

    Hi, Nuno,

    the actionUri should be the url+action name, right? I tried in this way, but I got the error message that " resource not found for the segament"

            public async Task TestActionAsync()
            {
                Console.WriteLine("--Action Test Starts Here--");
                JObject exeAction = new JObject();
                exeAction["ApplicationRef"] = "8B446743-C6C4-E711-A81C-005056B5FEFA";
                exeAction["Amount"] = new Money(){ Value = 300 }.Value;
                exeAction["Authcode"] = "0001";
                string actionUri = "dev-hq.dev.com/.../Microsoft.Dynamics.CRM.recoserv_ACT101_Payment_MarkAsPaid";
                HttpResponseMessage response = await SendAsJsonAsync(httpClient, HttpMethod.Post, actionUri, exeAction);
                
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Console.WriteLine("Successful");
                    
                }
                else
                {
                    Console.WriteLine("fail to call the action");
                    throw new CrmHttpResponseException(response.Content);
                }
            }


  • Verified answer
    Nuno Profile Picture
    2,005 on at

    If the action is global then

    string actionUri = "recoserv_ACT101_Payment_MarkAsPaid";

    If the action is bound to an entity then

    string actionUri = "dev-hq.dev.com/.../" + entitylogicalname + "(" + guid_entity_record + ")" + "/Microsoft.Dynamics.CRM.recoserv_ACT101_Payment_MarkAsPaid"

    for example, if the action is bound to account entity and there is an account record with next guid, the uri would be

    string actionUri = "dev-hq.dev.com/.../Microsoft.Dynamics.CRM.recoserv_ACT101_Payment_MarkAsPaid"

  • sdnd2000 Profile Picture
    2,510 on at

    Hi, Nuno,

    I still got syntax error message, please check 

     public async Task TestActionAsync()
            {
                Console.WriteLine("--Action Test Starts Here--");
                JObject exeAction = new JObject();
                exeAction["ApplicationRef"] = "8B446743-C6C4-E711-A81C-005056B5FEFA";
                exeAction["Amount"] = new Money(){ Value = 300 }.Value;
                exeAction["Authcode"] = "0001";
                string actionUri = "dev-hq.reco.on.ca/.../(05AD4D28-58C6-E711-A81D-005056B5FEFA)Microsoft.Dynamics.CRM.recoserv_ACT101_Payment_MarkAsPaid";
                HttpResponseMessage response = await SendAsJsonAsync(httpClient, HttpMethod.Post, actionUri, exeAction);         
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Console.WriteLine("Successful");
                    
                }
                else
                {
                    Console.WriteLine("fail to call the action");
                    throw new CrmHttpResponseException(response.Content);
                }
            }
    
            private async Task<HttpResponseMessage> SendAsJsonAsync<T>(HttpClient client,
                HttpMethod method, string requestUri, T value)
            {
                string content;
                if (value.GetType().Name.Equals("JObject"))
                { content = value.ToString(); }
                else
                {
                    content = JsonConvert.SerializeObject(value, new JsonSerializerSettings()
                    { DefaultValueHandling = DefaultValueHandling.Ignore });
                }
                HttpRequestMessage request = new HttpRequestMessage(method, requestUri);
                request.Content = new StringContent(content);
                request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                return await client.SendAsync(request);
            }


  • Nuno Profile Picture
    2,005 on at

    The uri is not correct. try this

    dev-hq.reco.on.ca/.../Microsoft.Dynamics.CRM.recoserv_ACT101_Payment_MarkAsPaid

  • sdnd2000 Profile Picture
    2,510 on at

    I got this error

    2017_2D00_11_2D00_16_5F00_0954.png

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 2

#1
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

#1
Shidin Haridas Profile Picture

Shidin Haridas 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans