locked
CRM on Sql Database RRS feed

  • Question

  • Hello,

    i create a application in .Net u can create and edit annotation in MS CRM 2011.

    To connect to the crm i use this code:

    ConnectionStringSettings connString = new ConnectionStringSettings("XRM", @"Server=" + ServerToCrm + ";Integrated Security = SSPI;");
    
    Microsoft.Xrm.Client.CrmConnection crmConn = new Microsoft.Xrm.Client.CrmConnection(connString);
    xrm = new XrmServiceContext(crmConn);
    
    ClientCredentials cred = new ClientCredentials();
    cred.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
    
    Uri urlOrga = new Uri(strOrgService);
    Uri temp = null;
    
    _serviceProxy = new OrganizationServiceProxy(urlOrga, temp, cred, null);
     _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
    service = (IOrganizationService)_serviceProxy;

    But now i want to realize that the credentials was different. I want that i use the application and add a annotiation with the windowscredentials and than they use a sqlcredentials if he write the Information in database.

    I hope someone understand my problem, sorry for the bad english :/

    Greetz

    Styler

    • Moved by Bob Beauchemin Thursday, March 19, 2015 4:39 PM Moving to the CRM dev forum for best results
    Thursday, March 19, 2015 10:07 AM

Answers

  • I understand that you are facing issues while creating/editing annotation in CRM with the code you have shared. While the code you are using looks correct, could you please try with the sample I have shared with Charlie. I have also attached the code here for your reference.


    // Creating an OnPremise Connection
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk.Client;
    using System.ServiceModel.Description;
    
    
    namespace AllInOneOnPremise_Online_EarlyBound
    {
        static class CreateOnPremiseConnection
        {
            #region Global Variables from Connection String
            static string OnPremiseOrganizationURL = System.Configuration.ConfigurationManager.AppSettings["OnPremiseOrganizationURL"].ToString();
            static string OnPremiseUsername = System.Configuration.ConfigurationManager.AppSettings["OnPremiseUsername"].ToString();
            static string OnPremisePassword = System.Configuration.ConfigurationManager.AppSettings["OnPremisePassword"].ToString();
            static string OnPremiseDomain = System.Configuration.ConfigurationManager.AppSettings["OnPremiseDomain"].ToString();
            #endregion
    
            public static OrganizationServiceProxy GetConnection()
            {
                try
                {
                    Uri organizationURL = new Uri(OnPremiseOrganizationURL);
                    Uri homeRealmUri = null;
                    ClientCredentials credentials = new ClientCredentials();
                    credentials.Windows.ClientCredential.UserName = OnPremiseUsername;
                    credentials.Windows.ClientCredential.Password = OnPremisePassword;
                    credentials.Windows.ClientCredential.Domain = OnPremiseDomain;
    
                    //credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
                    OrganizationServiceProxy proxy = new OrganizationServiceProxy(organizationURL, homeRealmUri, credentials, new ClientCredentials() { });
                    return proxy;
    
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
    
            }
        }
    }
    

    // Creating an Online Connection
    
    using Microsoft.Crm.Sdk.Samples;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AllInOneOnPremise_Online_EarlyBound
    {
        static class CreateOnlineConnection
        {
            #region Global Variables from Connection String
            static string OnlineOrganizationURL = System.Configuration.ConfigurationManager.AppSettings["OnineOrganizationURL"].ToString();
            static string OnlineUsername = System.Configuration.ConfigurationManager.AppSettings["OnlineUsername"].ToString();
            static string OnlinePassword = System.Configuration.ConfigurationManager.AppSettings["OnlinePassword"].ToString();
            static string OnlineFedaratedDomain = System.Configuration.ConfigurationManager.AppSettings["OnlineFedaratedDomain"].ToString();
            #endregion
            public static OrganizationServiceProxy ReturnCRMServiceInstance()
            {
                // Enter your Organization.svc endpoint below
                String organizationUri = OnlineOrganizationURL;
    
                IServiceManagement<IOrganizationService> OrganizationServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(organizationUri));
                AuthenticationProviderType OrgAuthType = OrganizationServiceManagement.AuthenticationType;
                AuthenticationCredentials authCredentials = GetCredentials(OrgAuthType);
                AuthenticationCredentials tokenCredentials = OrganizationServiceManagement.Authenticate(authCredentials);
                OrganizationServiceProxy organizationProxy;
                ManagedTokenOrganizationServiceProxy managedorganizationProxy;
                SecurityTokenResponse responseToken = tokenCredentials.SecurityTokenResponse;
    
                //using (managedorganizationProxy = new ManagedTokenOrganizationServiceProxy(OrganizationServiceManagement, responseToken))
                //{
                //    managedorganizationProxy.EnableProxyTypes();
                //}
    
                using (organizationProxy = new OrganizationServiceProxy(OrganizationServiceManagement, responseToken))
                {
                    organizationProxy.EnableProxyTypes();
                }
    
                return organizationProxy;
            }
    
            private static AuthenticationCredentials GetCredentials(AuthenticationProviderType endpointType)
            {
                AuthenticationCredentials authCredentials = new AuthenticationCredentials();
                switch (endpointType)
                {
    
                    case AuthenticationProviderType.ActiveDirectory:
                        authCredentials.ClientCredentials.Windows.ClientCredential =
                            new System.Net.NetworkCredential(OnlineUsername, OnlinePassword, OnlineFedaratedDomain);
                        break;
                    case AuthenticationProviderType.LiveId:
                        authCredentials.ClientCredentials.UserName.UserName = OnlineUsername;
                        authCredentials.ClientCredentials.UserName.Password = OnlinePassword;
                        authCredentials.SupportingCredentials = new AuthenticationCredentials();
                        authCredentials.SupportingCredentials.ClientCredentials =
                            Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
                        break;
                    default: // For Federated and OnlineFederated environments.                    
                        authCredentials.ClientCredentials.UserName.UserName = OnlineUsername;
                        authCredentials.ClientCredentials.UserName.Password = OnlinePassword;
                        // For OnlineFederated single-sign on, you could just use current UserPrincipalName instead of passing user name and password.
                        // authCredentials.UserPrincipalName = UserPrincipal.Current.UserPrincipalName;  //Windows/Kerberos
                        break;
    
                }
                return authCredentials;
            }
        }
    }
    

    // Using the connection to perform CRUD operations in either CRM Online or Onpremise
    
      private static void SteveTest()
            {
                try
                {
                    string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' page='1'>
                    <entity name='teamtemplate'> <attribute name='teamtemplatename'/> <attribute name='description'/> </entity>
                     </fetch>";
    
                    //using (OrganizationServiceProxy serviceOnlineProxy = CreateOnlineConnection.ReturnCRMServiceInstance())
                    using (OrganizationServiceProxy serviceOnlineProxy = CreateOnPremiseConnection.GetConnection())
                    {
                        //    using (OrganizationServiceProxy serviceOnlineProxy = CreateOnPremiseConnection.GetConnection())
                        //{
                        EntityCollection coll = serviceOnlineProxy.RetrieveMultiple(new FetchExpression(fetchXml));
                        if (coll.Entities.Count > 0)
                        {
                            Console.WriteLine(coll.Entities.Count.ToString());
    
                        }
                    }
                }
                catch (Exception Ex)
                {
    
                    throw Ex;
                }
            }
    I have also attached the following links for your reference:

    Authentication in CRM:

    Simplified Connection

    https://msdn.microsoft.com/en-us/library/gg695810.aspx

    Microsoft CRM SDK + Office 365 Authentication

    http://msdn.microsoft.com/en-us/library/hh675404

    CRM Online Authentication

    http://msdn.microsoft.com/en-us/library/hh675404.aspx

    http://blogs.msdn.com/b/apurvghai/archive/2012/07/29/authenticate-office-365-users-with-microsoft-dynamics-crm-online.aspx

    Microsoft Dynamics CRM Online Integration with Office 365

    http://msdn.microsoft.com/en-us/library/hh670607.aspx

    Authentication with Dynamics CRM Online on Microsoft online services environment(Office 365)

    http://blogs.msdn.com/b/girishr/archive/2012/09/04/authenticating-with-dynamics-crm-online-on-ocp-office-365.aspx

    Sample: Authenticate Users with Microsoft Dynamics CRM Web Services

    http://msdn.microsoft.com/en-us/library/hh675404.aspx

    Hope this helps.

    shalabh

    • Marked as answer by Styler_05 Monday, May 11, 2015 8:07 AM
    Tuesday, March 24, 2015 7:53 PM

All replies

  • Hi Styler,

    Thank you for your question. 

    I am trying to involve someone more familiar with this topic for a further look at this issue. Sometime delay might be expected from the job transferring. Your patience is greatly appreciated. 

    Thank you for your understanding and support.

    Regards,


    Charlie Liao
    TechNet Community Support

    Tuesday, March 24, 2015 8:50 AM
  • I understand that you are facing issues while creating/editing annotation in CRM with the code you have shared. While the code you are using looks correct, could you please try with the sample I have shared with Charlie. I have also attached the code here for your reference.


    // Creating an OnPremise Connection
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk.Client;
    using System.ServiceModel.Description;
    
    
    namespace AllInOneOnPremise_Online_EarlyBound
    {
        static class CreateOnPremiseConnection
        {
            #region Global Variables from Connection String
            static string OnPremiseOrganizationURL = System.Configuration.ConfigurationManager.AppSettings["OnPremiseOrganizationURL"].ToString();
            static string OnPremiseUsername = System.Configuration.ConfigurationManager.AppSettings["OnPremiseUsername"].ToString();
            static string OnPremisePassword = System.Configuration.ConfigurationManager.AppSettings["OnPremisePassword"].ToString();
            static string OnPremiseDomain = System.Configuration.ConfigurationManager.AppSettings["OnPremiseDomain"].ToString();
            #endregion
    
            public static OrganizationServiceProxy GetConnection()
            {
                try
                {
                    Uri organizationURL = new Uri(OnPremiseOrganizationURL);
                    Uri homeRealmUri = null;
                    ClientCredentials credentials = new ClientCredentials();
                    credentials.Windows.ClientCredential.UserName = OnPremiseUsername;
                    credentials.Windows.ClientCredential.Password = OnPremisePassword;
                    credentials.Windows.ClientCredential.Domain = OnPremiseDomain;
    
                    //credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
                    OrganizationServiceProxy proxy = new OrganizationServiceProxy(organizationURL, homeRealmUri, credentials, new ClientCredentials() { });
                    return proxy;
    
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
    
            }
        }
    }
    

    // Creating an Online Connection
    
    using Microsoft.Crm.Sdk.Samples;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AllInOneOnPremise_Online_EarlyBound
    {
        static class CreateOnlineConnection
        {
            #region Global Variables from Connection String
            static string OnlineOrganizationURL = System.Configuration.ConfigurationManager.AppSettings["OnineOrganizationURL"].ToString();
            static string OnlineUsername = System.Configuration.ConfigurationManager.AppSettings["OnlineUsername"].ToString();
            static string OnlinePassword = System.Configuration.ConfigurationManager.AppSettings["OnlinePassword"].ToString();
            static string OnlineFedaratedDomain = System.Configuration.ConfigurationManager.AppSettings["OnlineFedaratedDomain"].ToString();
            #endregion
            public static OrganizationServiceProxy ReturnCRMServiceInstance()
            {
                // Enter your Organization.svc endpoint below
                String organizationUri = OnlineOrganizationURL;
    
                IServiceManagement<IOrganizationService> OrganizationServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(organizationUri));
                AuthenticationProviderType OrgAuthType = OrganizationServiceManagement.AuthenticationType;
                AuthenticationCredentials authCredentials = GetCredentials(OrgAuthType);
                AuthenticationCredentials tokenCredentials = OrganizationServiceManagement.Authenticate(authCredentials);
                OrganizationServiceProxy organizationProxy;
                ManagedTokenOrganizationServiceProxy managedorganizationProxy;
                SecurityTokenResponse responseToken = tokenCredentials.SecurityTokenResponse;
    
                //using (managedorganizationProxy = new ManagedTokenOrganizationServiceProxy(OrganizationServiceManagement, responseToken))
                //{
                //    managedorganizationProxy.EnableProxyTypes();
                //}
    
                using (organizationProxy = new OrganizationServiceProxy(OrganizationServiceManagement, responseToken))
                {
                    organizationProxy.EnableProxyTypes();
                }
    
                return organizationProxy;
            }
    
            private static AuthenticationCredentials GetCredentials(AuthenticationProviderType endpointType)
            {
                AuthenticationCredentials authCredentials = new AuthenticationCredentials();
                switch (endpointType)
                {
    
                    case AuthenticationProviderType.ActiveDirectory:
                        authCredentials.ClientCredentials.Windows.ClientCredential =
                            new System.Net.NetworkCredential(OnlineUsername, OnlinePassword, OnlineFedaratedDomain);
                        break;
                    case AuthenticationProviderType.LiveId:
                        authCredentials.ClientCredentials.UserName.UserName = OnlineUsername;
                        authCredentials.ClientCredentials.UserName.Password = OnlinePassword;
                        authCredentials.SupportingCredentials = new AuthenticationCredentials();
                        authCredentials.SupportingCredentials.ClientCredentials =
                            Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
                        break;
                    default: // For Federated and OnlineFederated environments.                    
                        authCredentials.ClientCredentials.UserName.UserName = OnlineUsername;
                        authCredentials.ClientCredentials.UserName.Password = OnlinePassword;
                        // For OnlineFederated single-sign on, you could just use current UserPrincipalName instead of passing user name and password.
                        // authCredentials.UserPrincipalName = UserPrincipal.Current.UserPrincipalName;  //Windows/Kerberos
                        break;
    
                }
                return authCredentials;
            }
        }
    }
    

    // Using the connection to perform CRUD operations in either CRM Online or Onpremise
    
      private static void SteveTest()
            {
                try
                {
                    string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' page='1'>
                    <entity name='teamtemplate'> <attribute name='teamtemplatename'/> <attribute name='description'/> </entity>
                     </fetch>";
    
                    //using (OrganizationServiceProxy serviceOnlineProxy = CreateOnlineConnection.ReturnCRMServiceInstance())
                    using (OrganizationServiceProxy serviceOnlineProxy = CreateOnPremiseConnection.GetConnection())
                    {
                        //    using (OrganizationServiceProxy serviceOnlineProxy = CreateOnPremiseConnection.GetConnection())
                        //{
                        EntityCollection coll = serviceOnlineProxy.RetrieveMultiple(new FetchExpression(fetchXml));
                        if (coll.Entities.Count > 0)
                        {
                            Console.WriteLine(coll.Entities.Count.ToString());
    
                        }
                    }
                }
                catch (Exception Ex)
                {
    
                    throw Ex;
                }
            }
    I have also attached the following links for your reference:

    Authentication in CRM:

    Simplified Connection

    https://msdn.microsoft.com/en-us/library/gg695810.aspx

    Microsoft CRM SDK + Office 365 Authentication

    http://msdn.microsoft.com/en-us/library/hh675404

    CRM Online Authentication

    http://msdn.microsoft.com/en-us/library/hh675404.aspx

    http://blogs.msdn.com/b/apurvghai/archive/2012/07/29/authenticate-office-365-users-with-microsoft-dynamics-crm-online.aspx

    Microsoft Dynamics CRM Online Integration with Office 365

    http://msdn.microsoft.com/en-us/library/hh670607.aspx

    Authentication with Dynamics CRM Online on Microsoft online services environment(Office 365)

    http://blogs.msdn.com/b/girishr/archive/2012/09/04/authenticating-with-dynamics-crm-online-on-ocp-office-365.aspx

    Sample: Authenticate Users with Microsoft Dynamics CRM Web Services

    http://msdn.microsoft.com/en-us/library/hh675404.aspx

    Hope this helps.

    shalabh

    • Marked as answer by Styler_05 Monday, May 11, 2015 8:07 AM
    Tuesday, March 24, 2015 7:53 PM