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)

Crm plugin with sql server command

(0) ShareShare
ReportReport
Posted on by 587

Hi

I'm trying to insert in SQL server database hosted in go daddy site in plugin for crm

this is the first time that i'm dealing or creating plugin so please i need help

i got this error when i'm trying to insert

Unexpected exception from plug-in (Execute): Test_Plugin.Plugins.PreValidateAccountCreate: System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.


any idea how to do it and thanks

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    from your tag you are using CRM Online, this means your plugin is sandboxed and can't use external dll, in addition you are also limited to call certain type of urls from the sandbox (not IP address)

    you can try to merge the requested dll, but the right approach will be to create a webservice that will act as bridge between your crm plugin and the sql server db

  • Abbod Hamwi Profile Picture
    587 on at

    thanks for fast answering

    by web service like wcf ? 

  • Guido Preite Profile Picture
    54,086 Moderator on at

    yes, wfc or webapi or rest

  • Abbod Hamwi Profile Picture
    587 on at

    sorry for asking

    but as i told you i'm new with these things

    even with wcf

    so now i create the wcf service

    how to call it from plug in or like how to host the service on go daddy to be able to call it from crm plug in  

  • Guido Preite Profile Picture
    54,086 Moderator on at

    google is your friend

    code.msdn.microsoft.com/How-to-call-External-WCF-42c4490d

    remember that you are inside sandbox so the url of the wcf service needs to be a domain (like www.mydomain.com/myservice and not an IP address)

  • Abbod Hamwi Profile Picture
    587 on at

    yes this is the problem

    could i upload  the service on go daddy or where should i upload it

    and thanks for answering

  • Guido Preite Profile Picture
    54,086 Moderator on at

    to host a WCF service you need a windows server with IIS installed

  • Stuie Profile Picture
    255 on at

    Hi

    I have created a Plugin in CRM online that triggers on Update message.

    I take the data from the record updated using QueryExpression and have the values in vars ready to send.

    I have a wcf on an IIS server ready to accept this data. There are 7 values in total.

    Can you tell me the syntax to send the values to wcf from the plugin?

                            BasicHttpBinding myBinding = new BasicHttpBinding();
                            myBinding.Name = "BasicHttpBinding_ICrmDataInsert";
                            myBinding.Security.Mode = BasicHttpSecurityMode.None;
                            myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                            myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                            myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
    
                            EndpointAddress endPointAddress = new EndpointAddress("http:/xyz.com/WebService/CrmDataInsert.svc");
                            ChannelFactory<ICrmDataInsert> factory = new ChannelFactory<ICrmDataInsert>(myBinding, endPointAddress);
                            ICrmDataInsert channel = factory.CreateChannel();
                            // How Do I Post Data ??
                            factory.Close();
  • Abed Haniyah Profile Picture
    4,287 on at

    Here is a quick sample code how to send a value from CRM plugin to an external web services:

    Web service code :

    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.Data
    Imports System.Data.Sql

    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    ' <System.Web.Script.Services.ScriptService()> _
    <WebService(Namespace:="http://tempuri.org/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class WebService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function IsContact(ByVal ID As String) As String
    Dim ConnectionString As String = "Data Source=CRM\SQL;Initial Catalog=Demo_DB;User ID=sa;Password=P#$$S2016"
    Dim SqlConn As New SqlClient.SqlConnection
    Dim SQlCmd As New SqlClient.SqlCommand
    Dim SQLDr As SqlClient.SqlDataReader
    SqlConn.ConnectionString = ConnectionString
    SQlCmd.Connection = SqlConn

    SQlCmd.CommandText = "SELECT [fullname] FROM [dbo].[TContact] WHERE [ContactId] ='" & ID & "'"

    Try
    SqlConn.Open()
    SQLDr = SQlCmd.ExecuteReader
    If SQLDr.Read() Then
    Return SQLDr.Item(1).ToString
    Else
    Return False
    End If


    SqlConn.Close()
    Catch ex As Exception
    SqlConn.Close()
    Return ""

    End Try


    End Function

    End Class

    Plugin Code 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Client;
    using Microsoft.Crm.Sdk;
    using ServiceReference1;

    namespace Test_Call_WS
    {
    public class Test_Call_WS : IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {
    //Context = Info passed to the plugin at runtime
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {

    Entity entity = (Entity)context.InputParameters["Target"];

    if (entity.LogicalName != "contact")
    {
    return;
    }
    //Service = access to data for modification
    IOrganizationService service = factory.CreateOrganizationService(context.UserId);

    // Adding Basic Http Binding and its properties.
    BasicHttpBinding myBinding = new BasicHttpBinding();
    myBinding.Name = "BasicHttpBinding_Service";
    myBinding.Security.Mode = BasicHttpSecurityMode.None;
    myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
    myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
    myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

    // Endpoint Address defining the asmx Service to be called.
    EndpointAddress endPointAddress = new EndpointAddress(@"http://CRM:88/WebService.asmx");

    // Call to the Web Service using the Binding and End Point Address.

    ServiceReference1.WebServiceSoapClient myClient = new ServiceReference1.WebServiceSoapClient(myBinding, endPointAddress);

    string ab = myClient.IsContact(entity.Id.ToString());

    if (ab != "")
    {
    throw new InvalidPluginExecutionException(ab);
    }
    else
    {
    throw new InvalidPluginExecutionException("Failure");
    }

    }

    }
    }
    }

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