Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Call Web api from Custom workflow

Posted on by Microsoft Employee

Hi,

I have developed Web api, Custom workflow ( developed in C# ) and created a new Process.

From process I need to call Workflow ( I am done ) and from workflow I need to call web api .

I don't know how to call Web api from workflow.

Please help me.

Thanks in advance..

Kind regards

Martin

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Call Web api from Custom workflow

    Testing1

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Call Web api from Custom workflow

    Hi,

    I found the solution:

    protected override void ExecuteInternal(LocalWorkflowContext context)
            {
                // Todo: implement your logic here
                //var emailRef = this.EmailReference.Get(context.CodeActivityContext);
                
                    using (WebClient client = new WebClient())
                    {
                        client.UseDefaultCredentials = true;   ------------------  remove this line                
                        byte[] responseBytes = client.DownloadData(new Uri("https://mywebaiurl"));                    
                        string response = Encoding.UTF8.GetString(responseBytes); 
                    }            
    
                    
            }


    Kind regards

    Martin

  • Verified answer
    PranavShroti Profile Picture
    PranavShroti 4,510 on at
    RE: Call Web api from Custom workflow

    Code will only work for OnPremise environments and not for Online.

    Kindly check below post:

    community.dynamics.com/.../knowhow-can-we-make-web-api-calls-from-plugins-in-dynamics-crm-2016

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Call Web api from Custom workflow

    Hi,

    Can you give me a some idea, how I should change it, please ?

    Currently my code look like:

     protected override void ExecuteInternal(LocalWorkflowContext context)
            {
                // Todo: implement your logic here
                //var emailRef = this.EmailReference.Get(context.CodeActivityContext);
                
                    using (WebClient client = new WebClient())
                    {
                        client.UseDefaultCredentials = true;                   
                        byte[] responseBytes = client.DownloadData(new Uri("https://mywebaiurl"));                    
                        string response = Encoding.UTF8.GetString(responseBytes); 
                    }            
    
                    
            }


    Thanks in advance..

    Kind regards

    Martin

  • Suggested answer
    PranavShroti Profile Picture
    PranavShroti 4,510 on at
    RE: Call Web api from Custom workflow

    Server side and Client side scripts are different for a reason.

    They are not meant to cross paths, hence there is no simple way of doing this in CRM.

    I would suggest check rewriting whole code either server side (Plugin/WF) or client side (JS/WebAPI).  

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Call Web api from Custom workflow

    Hi,

    basically I need to call some web api from workflow, if you know some better way please help me...

    Kind regards

    Martin

  • PranavShroti Profile Picture
    PranavShroti 4,510 on at
    RE: Call Web api from Custom workflow

    Hi,

    Another option you can try is to update a field on form, which will trigger WebApi call "onchange" event.

    Will it be possible to describe exact scenario why do you wana call WebApi from server side code?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Call Web api from Custom workflow

    Hi,

    Thank you for answer,

    I already tried, got the error:

    Unexpected exception from plug-in (Execute): SWGCRMWinterProInterface.SWGCRMWinterProInterface: System.Security.SecurityException: Fehler bei der Anforderung des Berechtigungstyps "System.Security.Permissions.EnvironmentPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".

    Kind regards

    martin

  • Suggested answer
    PranavShroti Profile Picture
    PranavShroti 4,510 on at
    RE: Call Web api from Custom workflow

    Hi, you can use something like this in your server side code:

    using (WebClient client = new WebClient())

               {            

                   client.UseDefaultCredentials = true;

                   byte[] responseBytes = client.DownloadData(new Uri("<<your web api url>>/accounts(<<GUID>>)"));

                   string response = Encoding.UTF8.GetString(responseBytes);

                   // parse the json response

               }

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans