Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Webhooks and Azure Function

(0) ShareShare
ReportReport
Posted on by 266

Hello

I am making a call from my plugin to an Azure Function using a webhook. I am using the below code 

IServiceEndpointNotificationService cloudService = (IServiceEndpointNotificationService)serviceProvider.GetService(typeof(IServiceEndpointNotificationService));
                    if ( cloudService == null )
                        throw new InvalidPluginExecutionException ( "Failed to retrieve the service bus service." );
                    

                    string response = cloudService.Execute(new EntityReference("serviceendpoint", serviceEndpointId), context);
                    if ( !String . IsNullOrEmpty ( response ) )
                    {
                        tracingService . Trace ( "Response = {0}" , response );
                    }


Azure function returns a simple "Hello" message back. The Azure function is working fine when I test it from Azure Portal.

However, in the plugin the Execute method returns null as response. Anyone has an idea what could be wrong in the code?

  • Davina Sungum Profile Picture
    146 on at
    RE: Webhooks and Azure Function

    Hi Wahaj,

    Yes its all correct and registered in sandbox mode.

    It looks like the http code returned is coming back correctly but the message is lost - it looks like MS has not implemented that correctly.

    If I return a 200 then the plugin continues after execute but results string is empty.

    If I put a try, catch around the Execute call to the endpoint, when an error code is returned like 500 then I can catch it and throw a pluginexception error back to display a friendly messsage. This is a workaround as I actually want the message sent back from the function instead of assuming a generic message will fit all errors that might have occured.

    Thanks

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at
    RE: Webhooks and Azure Function

    Hi,

    Did you verify:

    • The endpoint returned is correct.
    • Also, please make sure your plugin is registered in sandbox mode:

    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/write-custom-azure-aware-plugin#plug-in-registration

  • Davina Sungum Profile Picture
    146 on at
    RE: Webhooks and Azure Function

    Hi,

    Plugin code:

    try

                   {

                       QueryExpression endpointQuery = new QueryExpression("serviceendpoint");

                       endpointQuery.ColumnSet = new ColumnSet("url");

                       ConditionExpression urlCond = new ConditionExpression("url", ConditionOperator.EndsWith, "MyWebHook");

                       endpointQuery.Criteria.AddCondition(urlCond);

                       var endpointColl = Service.RetrieveMultiple(endpointQuery);

                       if (endpointColl.Entities.Count == 0)

                       {

                           throw new InvalidPluginExecutionException("Failed to retrieve the endpoint");

                       }

                       var endpoint = endpointColl[0];

                       Tracing.Trace($"Taking Enpoint URL: {endpoint["url"]}");

                       Tracing.Trace($"Endpoint: {endpoint.Id}");

                       Tracing.Trace("Posting the execution context.");

                       string response = cloudService.Execute(new EntityReference("serviceendpoint", endpoint.Id), Context);

    //**ISSUE IS HERE STRING IS ALWAYS NULL

                       if (!String.IsNullOrEmpty(response))

                       {

                           Tracing.Trace("Response = {0}", response);

                       }

                       Tracing.Trace("Done.");

                   }

    Web hook:

    [FunctionName("MyWebHook")]

           public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", WebHookType = "genericJson", Route = null)]HttpRequestMessage req, TraceWriter log)

           {

               IOrganizationService service = null;

               log.Info("In CheckAttachment");

               var contextJson = await req.Content.ReadAsStringAsync();

               var context = contextJson.GetContext();

           return req.CreateResponse(HttpStatusCode.OK, "Created");

    }

    In postman I can see "Created" is returned but not in the plugin trace:

    Posting the execution context.

    Done.

    Completed plugin execution

    It goes straight to null instead of tracing the string that should be in the response.

  • Wahaj Rashid Profile Picture
    11,321 on at
    RE: Webhooks and Azure Function

    Hi,

    If possible, can we see snapshots of your azure function and plugin code? 

  • Davina Sungum Profile Picture
    146 on at
    RE: Webhooks and Azure Function

    Hi, it works fine in postman its just coming back into Dynamics the string is empty. From all paths a http status is returned.

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at
    RE: Webhooks and Azure Function

    Hi,

    Can you please try this outside of Dynamics first? Like a postman call.

    If it works, I would recommend you to look at your function definition, are sending any parameters from Dynamics? Please make sure, your function returns HTTP status from all paths.

  • Davina Sungum Profile Picture
    146 on at
    RE: Webhooks and Azure Function

    Hi,

    Thanks for the links. I have just updated my post, there was an error in the endpoint I was calling, so the actual issues is that a null string is being returned now instead of what I'm returning with the httpstatus.OK

    Any ideas?

    Thanks

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at
  • Davina Sungum Profile Picture
    146 on at
    RE: Webhooks and Azure Function

    Hi,

    Did you resolve this? I'm trying the same thing, trying to invoke a web hook from a plugin

    web hook:

    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", WebHookType = "genericJson", Route = null)]HttpRequestMessage req, TraceWriter log)

    just returning:

               var contextJson = await req.Content.ReadAsStringAsync();

               var context = contextJson.GetContext();

               return req.CreateResponse(HttpStatusCode.OK, "Created");

    The string is null when it is returned back from the function.

    The plugin is running synchonously, tried on pre-operation and post-operation but no difference.

    Any ideas?

    Thanks

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,079 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,860 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans