locked
Connect with CRM using credentials through SDK RRS feed

  • Question

  • Hi Experts,

    I need to connect the CRM using OrganizationServiceProxy and need to login using credentials.

    Here is my code:

    ClientCredentials credentials = new ClientCredentials();
    OrganizationServiceProxy orgProxy = null;
    
    credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
    
    credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
    
    orgProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null);
    Any help?

    Monday, November 5, 2012 7:38 AM

Answers

  • Hi,

    if you use this:

    credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;

    you will connect to the service using the Credentials of the user running your code. You have to make sure that the user running your application belongs to the same domain as CRM and that the account has enough privileges for this.

    You can also use this to connect using a specified account:

    credentials.Windows.ClientCredential = new NetworkCredential("username", "password", "domain");

    for example:

    credentials.Windows.ClientCredential = new NetworkCredential("administrator", "myPassword", "myDomain");

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Monday, November 5, 2012 7:50 AM

All replies

  • Hi,

    what kind of deployment do you have? CRM Online, On Premise or IFD?

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Monday, November 5, 2012 7:44 AM
  • On Premise.
    Monday, November 5, 2012 7:45 AM
  • Hi,

    if you use this:

    credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;

    you will connect to the service using the Credentials of the user running your code. You have to make sure that the user running your application belongs to the same domain as CRM and that the account has enough privileges for this.

    You can also use this to connect using a specified account:

    credentials.Windows.ClientCredential = new NetworkCredential("username", "password", "domain");

    for example:

    credentials.Windows.ClientCredential = new NetworkCredential("administrator", "myPassword", "myDomain");

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Monday, November 5, 2012 7:50 AM
  • Hi Pavlos,

    Thank you for your valuable time for me all the time.

    I had a problem as you described, I was using DefaultNetworkCredentials and our code was deployed on the server and the server user was not under the same domain as CRM.

    So, that i needed to log in using one user.

    Does "myDomain" mean Organization name?

    Thanks

    Monday, November 5, 2012 7:54 AM
  • Hi,

    no, "myDomain" is the Active Directory domain that the CRM Server belongs to. For example I guess that you log on to the server using something like "myDomain\myUsername". 

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Monday, November 5, 2012 7:56 AM
  • Ok - thank you Pavlos.

    I will check this and get back to you soon!

    Monday, November 5, 2012 7:58 AM
  • Hi Pavlos,

    As you said, i have made the code changes.

    credentials.Windows.ClientCredential = new NetworkCredential(strCRMUserName, strCRMUserPassword, strActiveDirectoryDomainName);

    orgProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null);

    But while executing the response.

     RetrieveMultipleRequest retrieveSalesPersonsReq = new RetrieveMultipleRequest();
                    retrieveSalesPersonsReq.Query = salesPersonQuery;
                    RetrieveMultipleResponse retrieveSalesPersonsResponse = (RetrieveMultipleResponse)orgProxy.Execute(retrieveSalesPersonsReq);

    It is asking for user credentials again, why is it so?

    Can you please guide?



    • Edited by Dhaval247 Monday, November 5, 2012 8:34 AM
    Monday, November 5, 2012 8:34 AM
  • Hi,

    can you please share the whole code for your connection to the service? And also where are you using this code? In what kind of an application? And where is it deployed?

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Monday, November 5, 2012 8:38 AM
  • Hi Pavlos,

    Thank you for the prompt response.

    Please see all the steps:

    Problem Statement:

    We have to fetch all the leads from CRM and needs to assign them to Sales Representative using the round robin method.

    Step 1: Connection to CRM.

    private OrganizationServiceProxy EstablishConnectionToCRM(Uri OrganizationUri, Uri HomeRealmUri)
            {
                ClientCredentials credentials = new ClientCredentials();
                OrganizationServiceProxy orgProxy = null;
                string strCRMUserName = ConfigurationSettings.AppSettings["CRMUserName"].ToString();
                string strCRMUserPassword = ConfigurationSettings.AppSettings["CRMUserPassword"].ToString();
                string strActiveDirectoryName = ConfigurationSettings.AppSettings["ActiveDirectoryName"].ToString();
                try
                {
                    objLogs.InsertAuditLogs(MethodBase.GetCurrentMethod().DeclaringType.ToString(), MethodBase.GetCurrentMethod().Name, "Connecting to CRM...");
                    objLogger.Logger("Connecting to CRM...", "LeadAssignment");
    
                    //credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
    
                    credentials.Windows.ClientCredential = new NetworkCredential(strCRMUserName, strCRMUserPassword, strActiveDirectoryName);
                    //credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
                    orgProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null);
    
                    objLogs.InsertAuditLogs(MethodBase.GetCurrentMethod().DeclaringType.ToString(), MethodBase.GetCurrentMethod().Name, "Connected to CRM.");
                    objLogger.Logger("Connected to CRM.", "LeadAssignment");
                    return orgProxy;
                }
                catch (Exception ex)
                {
                    objLogs.InsertErrorLogs(MethodBase.GetCurrentMethod().DeclaringType.ToString(), MethodBase.GetCurrentMethod().Name, ex.Message);
                    return null;
                }
                finally
                {
                    orgProxy.Dispose();
                }
            }

    Step 2: Get all sales representative.

    private DataTable GetAllSalesReps(IOrganizationService orgProxy)
            {
                try
                {
                    objLogs.InsertAuditLogs(MethodBase.GetCurrentMethod().DeclaringType.ToString(), MethodBase.GetCurrentMethod().Name, "Start -- Fetch available sales representative from CRM");
                    objLogger.Logger("Start -- Fetch available sales representative from CRM", "LeadAssignment");
    
                    DataTable dtSalesReps = CreateTableSalesReps();
    
                    QueryExpression qe = new QueryExpression();
                    FilterExpression filter = new FilterExpression();
    
                    qe.ColumnSet = new ColumnSet(true);
                    qe.EntityName = "systemuser";
    
                    filter.FilterOperator = LogicalOperator.And;
                    filter.AddCondition("isdisabled", ConditionOperator.Equal, false);
    
                    LinkEntity linkFilteredUsers = new LinkEntity();
                    linkFilteredUsers.JoinOperator = JoinOperator.Inner;
    
                    linkFilteredUsers.LinkFromEntityName = "systemuser";
                    linkFilteredUsers.LinkFromAttributeName = "systemuserid";
    
                    linkFilteredUsers.LinkToEntityName = "systemuserroles";
                    linkFilteredUsers.LinkToAttributeName = "systemuserid";
    
                    qe.LinkEntities.Insert(0, linkFilteredUsers);
    
                    LinkEntity linkFilteredRoles = new LinkEntity();
                    linkFilteredRoles.JoinOperator = JoinOperator.Inner;
    
                    linkFilteredRoles.LinkFromEntityName = "systemuserroles";
                    linkFilteredRoles.LinkFromAttributeName = "roleid";
    
                    linkFilteredRoles.LinkToEntityName = "role";
                    linkFilteredRoles.LinkToAttributeName = "roleid";
                    var filterRole = new FilterExpression(LogicalOperator.And);
                    filterRole.Conditions.Add(new ConditionExpression("name", ConditionOperator.Equal, "Salesperson"));
                    linkFilteredRoles.LinkCriteria = filterRole;
    
                    linkFilteredUsers.LinkEntities.Insert(0, linkFilteredRoles);
    
                    qe.PageInfo.Count = 100;
                    qe.PageInfo.PageNumber = 1;
                    qe.Criteria = filter;
    
                    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                    request.Query = qe;
                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)orgProxy.Execute(request);
    
                    foreach (Entity activity in response.EntityCollection.Entities)
                    {
                        DataRow drSalesRepsIds = dtSalesReps.NewRow();
                        foreach (KeyValuePair<string, object> k in activity.Attributes)
                        {
                            switch (k.Key.ToLower())
                            {
                                case "systemuserid":
                                    drSalesRepsIds["dcUserID"] = k.Value;
    
                                    objLogs.InsertAuditLogs(MethodBase.GetCurrentMethod().DeclaringType.ToString(), MethodBase.GetCurrentMethod().Name, "User found with GUID : " + k.Value.ToString());
                                    objLogger.Logger("User found with GUID : " + k.Value.ToString(), "LeadAssignment");
    
                                    break;
                            }
                        }
                        dtSalesReps.Rows.Add(drSalesRepsIds);
                    }
    
                    objLogs.InsertAuditLogs(MethodBase.GetCurrentMethod().DeclaringType.ToString(), MethodBase.GetCurrentMethod().Name, "End -- Fetch available representative from CRM");
                    objLogger.Logger("End -- Fetch available representative from CRM", "LeadAssignment");
    
                    return dtSalesReps;
                }
                catch (Exception ex)
                {
                    objLogs.InsertErrorLogs(MethodBase.GetCurrentMethod().DeclaringType.ToString(), MethodBase.GetCurrentMethod().Name, ex.Message);
                    return null;
                }
            }

    While fetching the sales representative in 2nd Method, while executing below lines:

      RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                    request.Query = qe;
                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)orgProxy.Execute(request);

    It shows pop-up message and asking for Credentials for login.

    Do you need any more details?


    • Edited by Dhaval247 Monday, November 5, 2012 8:46 AM
    Monday, November 5, 2012 8:44 AM
  • Hi,

    I think that the credentials you are using are not correct. Recheck the organization service uri, the username, the password and the domain. Make sure that the username is written without the domain prefix.

    And please tell me what kind of application is it that is running the above code? A custom web page, a console application?

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Monday, November 5, 2012 8:55 AM
  • Hi Pavlos,

    I extremely sorry to waste your time behind this unusual investigation. I think i need some vacation now. I used the wrong password for login. Uff... It's working now!

    Thank you for help all the time!

    Monday, November 5, 2012 9:05 AM