Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

C# call actions through webapi with parameters

Posted on by 2,397

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

  • sdnd2000 Profile Picture
    sdnd2000 2,397 on at
    RE: C# call actions through webapi with parameters

    I got this error

    2017_2D00_11_2D00_16_5F00_0954.png

  • Nuno Profile Picture
    Nuno 2,005 on at
    RE: C# call actions through webapi with parameters

    The uri is not correct. try this

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

  • sdnd2000 Profile Picture
    sdnd2000 2,397 on at
    RE: C# call actions through webapi with parameters

    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);
            }


  • Verified answer
    Nuno Profile Picture
    Nuno 2,005 on at
    RE: C# call actions through webapi with parameters

    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/.../&quot; + 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&quot;

  • sdnd2000 Profile Picture
    sdnd2000 2,397 on at
    RE: C# call actions through webapi with parameters

    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);
                }
            }


  • Suggested answer
    Nuno Profile Picture
    Nuno 2,005 on at
    RE: C# call actions through webapi with parameters

    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);

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans