Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Crm plugins

Posted on by Microsoft Employee

I need someone to please help me be able to send emails out from CRM using C# Plugin.There's so many bits and pieces out on the internet and I'm having trouble piecing it all together.

I was able to register the plugin and it's executing on the server, but having a hard time figuring out how to implement this part into the plugin. 

 


Here's the rest of the code. I didn't correctly call the CreateMail( ) function, puzzled how to add the IOrg service and Contact Guid.

 public class SendEmail
    {
       #region Class Level Members
        // Define the IDs needed for this sample.
        private Guid _emailId;
        private Guid _contactId;
        private Guid _userId;
        private OrganizationServiceProxy _serviceProxy;

        #endregion Class Level Members

        #region How To Sample Code
        /// <summary>
        /// Send an e-mail message.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                // Connect to the Organization service. 
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    // Call the method to create any data that this sample requires.
                   CreateMail();


                    // Use the SendEmail message to send an e-mail message.
                    SendEmailRequest sendEmailreq = new SendEmailRequest
                    {
                        EmailId = _emailId,
                        TrackingToken = "",
                        IssueSend = true
                    };

                    SendEmailResponse sendEmailresp = (SendEmailResponse)_serviceProxy.Execute(sendEmailreq);
                    Console.WriteLine("Sent the e-mail message.");              
                     
                }
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Crm plugins

    Ok thank you, what happened to your blog?

  • Suggested answer
    nghieppham Profile Picture
    nghieppham 4,755 on at
    RE: Crm plugins

    Hi Tekwise,

    Please download this sample code code.msdn.microsoft.com/How-to-call-External-WCF-42c4490d . In the case, you want to use no-ip to public WCF service, you have to go to no ip site, register account and use it, it require you have Network knowledge.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Crm plugins

    Could you share your code for implementing the interface from IPlugin?

    I 'm not sure if I'm clear on how you did that. Thank you!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Crm plugins

    Nghiep - how do you make the WCF service public to the internet?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Crm plugins

    Hi Nghiep, I get all of that, the part where I''m lost is how to write the code for the interface. Can you share your code?

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
  • Suggested answer
    nghieppham Profile Picture
    nghieppham 4,755 on at
    RE: Crm plugins

    Hi Tekwise,

    To use the WCF services, you need to do these things:

    1. WCF have to host on IIS, if you are in develop environment, you right click on WCF project, select debug, select start the new instance. It will be hosted at your localhost.

    2. Go to your plug-in, add service reference, you click on discovery, it will be show your wcf service. Then you add it.

    3. Then you just create 1 instance like below code:

    CrmService.Service1Client client = new Service1Client();
    //GenerateServiceClient();
    client.WriteFile(@"D:\CRMFiles", "Account.xml"); 

    CrmService is your namespace. 

    After that, you build and register your plugin, you have to run your wcf service, so that crm can see it. 

    1 note: In CRM Online, you have to public your wcf service to internet. 

    Regards,

    Nghiep Pham

    Please click on Verify, I still support you. :) 

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Crm plugins

    can u show me the code for the write file interface? i need help showing this is implemented for the interface. It's not clear to me where or how you are using the writefile method. I keep running into all sorts of errors. please help ,thank you

    Iservice1 file

    namespace TESSTCRMLibrary
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: Add your service operations here
    
           
    
           
       }
       
        // Use a data contract as illustrated in the sample below to add composite types to service operations.
        // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WCF_CRMLibrary.ContractType".
        [DataContract]
        public class CompositeType
        {
            bool boolValue = true;
            string stringValue = "Hello ";
    
            [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }
    
            [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }
    
    
           
         
    }
        public class writeFile
        {
    
            void WriteFile(string filePath, string fileName)
            {
                if (File.Exists(fileName))
                    File.Create(filePath + "\\" + fileName + DateTime.Now.ToShortDateString());
                else
                    File.Create(filePath + "\\" + fileName);
            }
    
        }
         interface WriteFile
        {
    
             void WriteFile(string filePath, string fileName);
           
        }
       
    }

    Service 1 file

        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
            public WriteFile WriteFile(string filePath, string fileName)
            {
    
               void;
            }
        }



    Plugin file

      WCFCrmLibrary.IService1 cs = new WCFCrmLibrary.IService1();


  • nghieppham Profile Picture
    nghieppham 4,755 on at
    RE: Crm plugins

    Hi Tekwise,

    For question 1: writefile( ), you have create in Interface, and then implement it at your services.

    For question 2: As I mentioned, Isolation mode plug in does not allow you access web service by IP, you have to access by friendly name. If you have server onpremise, you can host in your iis and test it. But if you want to test on CRM Online, you have to public your wcf service that CRM Online can see it. Hence, No Ip is a solution for you to map your local service and public to internet, you need to learn how to it, you can go to their website if you have static ip, you no need to use No Ip.

    Regards,

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Crm plugins

    Hi two questions:

    1) Where do i put the writefile( ) inside the public interface IService1? or separate method in the IService1?

    looks like the iservice1 is a public interface, and i cant just put that function in there w public in the name, it says the modifer 'public' is not valid for this item

    2) no-Ip - where is the host address? Where can i find this in my crm?

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans