Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

CALLING WEB API POST REQUEST THROUGH PLUGIN

Posted on by Microsoft Employee

i have created a web api that sends email using smtp client. 

now i have called that api in my plugin which is a post request. 

The problem is i am able to send plain text through email body but if it is html content the body is null. 

Why is it so?

Please provide me with the solution

I m using sandbox enviornment. Online CRM

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    Any Suggestions for the above?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    Business Process Error

    Error occuredThe remote server returned an error: (500) Internal Server Error. If you contact support, please provide the technical details

    This is the error i get if i try sending html content from crm through plugin

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    <div style="font-size: 12px; font-family: Tahoma, Verdana, Arial;"><span style="color: rgb(48, 49, 49); font-family: "Segoe UI", Tahoma, Arial, sans-serif; font-size: 15px; background-color: rgb(238, 238, 238);"><h1>Test Email+WebAPI</h1></span></div>

    this value i am getting in  "objEmail.body = ObjPrimaryEntity.GetAttributeValue<string>("description");"

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    Hi,

    I check on my side it's working fine.

    just one change i pass hard code email body.

    string decodebody = System.Net.WebUtility.HtmlDecode("<h1>Test Email+WebAPI</h1>");

    Check what is the body fetch from "objEmail.body = ObjPrimaryEntity.GetAttributeValue<string>("description");"

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Email;

    using System.Net;

    using System.Net.Http;

    using System.Net.Http.Headers;

    using System.Net.Mail;

    using System.Web;

    using System.Web.Http;

    using System.Web.Mvc;

    using System.Web.Script.Serialization;

    using System.Configuration;

    using System.IO;

    using System.Runtime.Serialization.Json;

    namespace DDU.CALLAPI_PLUGIN

    {

       public class Class1 : IPlugin

       {

           #region Class Member Declaration

           IPluginExecutionContext pluginexecutioncontext;

           IOrganizationServiceFactory servicefactory;

           IOrganizationService service;

           #endregion

           public void Execute(IServiceProvider serviceProvider)

           {

               #region Class Member Definition

               pluginexecutioncontext = (IPluginExecutionContext)serviceProvider.GetService((typeof(IPluginExecutionContext)));

               servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService((typeof(IOrganizationServiceFactory)));

               service = servicefactory.CreateOrganizationService(pluginexecutioncontext.UserId);

               #endregion

               Entity ObjPrimaryEntity = (Entity)pluginexecutioncontext.InputParameters["Target"];

               //Entity ObjPrimaryEntity = (Entity)service.Retrieve("email", pluginexecutioncontext.PrimaryEntityId, new ColumnSet(true));

               EmailActivityModel objEmail = new EmailActivityModel();

               objEmail.To = ObjPrimaryEntity.GetAttributeValue<EntityCollection>("to").Entities[0].GetAttributeValue<EntityReference>("partyid").Id; //gets id

               Entity obRegistration = (Entity)service.Retrieve("new_registration", objEmail.To, new ColumnSet(true));

               objEmail._To = obRegistration != null ? Convert.ToString(obRegistration.Attributes["new_emailaddress"]) : string.Empty; //gets email address

               objEmail.From = ObjPrimaryEntity.GetAttributeValue<EntityCollection>("from").Entities[0].GetAttributeValue<EntityReference>("partyid").Id;//gets id

               Entity obSystemUser = (Entity)service.Retrieve("systemuser", objEmail.From, new ColumnSet(true));

               objEmail._From = obSystemUser != null ? Convert.ToString(obSystemUser.Attributes["internalemailaddress"]) : string.Empty; //gets email address

               objEmail.Subject = ObjPrimaryEntity.GetAttributeValue<string>("subject");

               objEmail.body = ObjPrimaryEntity.GetAttributeValue<string>("description");

               #region Post - Send email

               string decodebody = System.Net.WebUtility.HtmlDecode(objEmail.body);

               ASCIIEncoding encoding = new ASCIIEncoding();

               string postData = "To=" + objEmail._To;

               postData += ("&Subject=" + objEmail.Subject);

               postData += ("&body=" + decodebody);

               postData += ("&From=" + objEmail._From);

               postData += ("&CC=" + objEmail.CC);

               byte[] Postdata = encoding.GetBytes(postData);

               #endregion

               #region Prepare Web Request

               // Prepare web request...

               try

               {

                   WebRequest request = WebRequest.Create("uatservice.azurewebsites.net/.../SendEmail&quot;);

                   request.Method = "POST";

                   request.ContentLength = Postdata.Length;

                   request.ContentType = "application/x-www-form-urlencoded";

                   Stream dataStream = request.GetRequestStream();

                   //  dataStream.Length = 1073741823;

                   dataStream.Write(Postdata, 0, Postdata.Length);

                   dataStream.Close();

                   WebResponse response = request.GetResponse();

                   string desc = ((HttpWebResponse)response).StatusDescription;

                   dataStream = response.GetResponseStream();

                   StreamReader reader = new StreamReader(dataStream);

                   string responseFromServer = reader.ReadToEnd();

                   reader.Close();

                   dataStream.Close();

                   response.Close();

               }

               catch (InvalidPluginExecutionException ex)

               {

               }

               #endregion Prepare Web request

           }

           public struct PostData

           {

               public string _To { get; set; }

               public string FromEmail { get; set; }

               public string Body { get; set; }

               public string Subject { get; set; }

           }

       }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    Can you shared you plugin code?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    i have used this code in my console app

    string encodebody = System.Net.WebUtility.HtmlDecode(objEmail.body);

    to decode body content.

    When i send mail through console application the mail is sent successfully and html tags are also rendered. But what to do in case of plugin.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    There are not issue with plugin. (problem in webapi code)

    please try to send email using console application.

    1. email body "<p>This is a test email for api</p>"

    2. email body "<p>This is a test email for api</p>"

    3. email boy "<![CDATA[<p>This is a test email for api</p>]]>"

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    Hardik,

    my plugin triggers on post operation

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CALLING WEB API POST REQUEST THROUGH PLUGIN

    Hi,

    You need to check smsclient class property. IsBodyHtml = true;

    or

    htmlencoding/htmldecoding

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans