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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Call Web api from Custom workflow

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    PranavShroti Profile Picture
    4,510 on at

    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

               }

  • Community Member Profile Picture
    on at

    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

  • PranavShroti Profile Picture
    4,510 on at

    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
    on at

    Hi,

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

    Kind regards

    Martin

  • Suggested answer
    PranavShroti Profile Picture
    4,510 on at

    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
    on at

    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

  • Verified answer
    PranavShroti Profile Picture
    4,510 on at

    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
    on at

    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

  • Community Member Profile Picture
    on at

    Testing1

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

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 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans