Connection to CRM
Asked Answered
P

5

5

I need my website to connect to CRM this is my code

        var organizationUri = new Uri(ConfigurationManager.AppSettings["CRMServerUrl"]);

        //Client credentials
        var credentials = new ClientCredentials();
        credentials.UserName.UserName = @"<user>";
        credentials.UserName.Password = "<password>";
        // Use the Microsoft Dynamics CRM Online connection string from the web.config file named "CrmConnectionStr".
        using (OrganizationServiceProxy _service = new OrganizationServiceProxy(organizationUri, null, credentials, null))
        {
            Response.Write("Connected");
        }

and this is in my web.config

 <add key="CRMServerUrl" value="http://XXXXX/XRMServices/2011/Organization.svc" />
<add key="username" value="<user>" />
<add key="password" value="<password>" />

it gives me this error message:

"A critical error has occurred. Unable to connect with CRM server. The caller was not authenticated by the service."

Pennywise answered 9/6, 2014 at 7:0 Comment(3)
@Make sure the username and passwords are correct. you can use Window AuthenticationJitters
What kind of deployment do you have? Is it CRM On-Prem, IFD or Online?Heraldry
What is the complete url of the service you are using? Are you specifying the Organization Name? that may be the trouble of not authenticating correctly.. it should be something like: myserver:myport/OrganizationName/XRMServices/2011/…Dinh
R
6

You should use the ►Simplified Connection provided by the ►Microsoft.Xrm.Client.CrmConnection type for an easier experience.

Steps are easy:

1) in the .config file add a connection string

<connectionStrings>
  <add name="CrmConnStr" connectionString="!SEE EBELOW!"/>
</connectionStrings>

2) In code, it goes like this:

// parameter is the name of the connection string
// NOTE: These "setup" declarations are slow, reuse them as much as possibile
var connection = new CrmConnection("CrmConnStr");

var service = new OrganizationService(connection);
var context = new CrmOrganizationServiceContext(connection);

About the connection string, if On-Premise WITHOUT IFD it should be

"Url=http[s]://serverurl/organization; Domain=DOMAIN; Username=USERNAME; Password=PASSWORD"
<!-- https is always nice, but it's not mandatory in this case -->

If On-Premise WITH IFD or Online it should be

"Url=https://org.server.url; Username=USERNAME; Password=PASSWORD"
<!-- https is of course mandatory here -->
<!-- 'DOMAIN=...' part is not needed because of ADFS -->
Rhea answered 9/6, 2014 at 7:43 Comment(1)
I believe you are missing Url= at the start of the example connection strings.Oswin
L
5

Use these assemblies in your project:

using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client.Services;

Create the organization service:

string connectionString = "Url=https://your_orgnisation.crm5.dynamics.com; Username=user_name; Password=your_password;";
CrmConnection connection = CrmConnection.Parse(connectionString);
OrganizationService organisationservice = new OrganizationService(connection);

Don't forget to import System.Runtime.serialization.

Legatee answered 8/1, 2015 at 12:54 Comment(0)
J
4

By looking at your code, you are not using username and password Change below lines:

    //Client credentials
    var credentials = new ClientCredentials();
    credentials.UserName.UserName =ConfigurationManager.AppSettings["username"].toString();
    credentials.UserName.Password =ConfigurationManager.AppSettings["password"].toString();

try this: Default Credentials are used,using Windows Authentication

ClientCredentials Credentials = new ClientCredentials();
Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

//This URL needs to be updated to match the servername and Organization for the environment.

Uri OrganizationUri = new Uri("http://XXXXX/XRMServices/2011/Organization.svc");
Uri HomeRealmUri = null;
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
  {          
   IOrganizationService service = (IOrganizationService)serviceProxy;
  }
Jitters answered 9/6, 2014 at 7:7 Comment(2)
i followed you code and it works but it gives an error : "The caller was not authenticated by the service."Pennywise
@Pennywise Do you have access to the CRM from browser ? and did you updated the URL in my code to yoursJitters
S
2

use Microsoft.Xrm.Tooling.Connector

    var service =new CrmServiceClient(
                "AuthType=Office365;Username=username; Password=password;Url=https://url");
Seditious answered 4/5, 2018 at 11:38 Comment(1)
This is the best answer. Xrm.Client is replaced by Xrm.Tooling. If you are connecting to an online instance, be sure to look at Build web applications using Server-to-Server (S2S) authentication Using this approach you don’t need to use a paid Dynamics 365 user license when you connect to Dynamics 365 tenants.Foxed
D
0

Not specifying the Organization Name may be the trouble of not authenticating correctly.. it should be something like:

http://myserver:myport/OrganizationName/XRMServices/2011/Organization.svc

Dinh answered 15/4, 2015 at 17:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.