locked
Using CRM Connection String RRS feed

  • Question

  • I recently came across the documentation for the Crm Connection string here.  It seemed like a great solution for some of the small apps we want to do rather than the more complex approach we have been using to get a ServiceContext in order to access data, do LINQ queries etc.  Then I realized that I do not really understand how the CrmConnection.Parse Method can be used to replace our current approach.

    Here is what we do now:

    ServerConnection.Configuration serverConfig = serverConnect.GetServerConfiguration(); // Password prompt in here
    _serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig);
    svcContext = new ServiceContext(_serviceProxy);
    

    So we need to end up with the equivilent of the svcContext in the above code.

    The documentation for CrmConnection.Parse shows the following snipet:

    var connection = CrmConnection.Parse("Url=http://crm.contoso.com/xrmContoso; Domain=CONTOSO; Username=jsmith; Password=passcode;");
    

    I'm not sure how I get from the connection var above to the "svcContect" that I need.  Any help (hopefully with a code example) will be most appreciated.

    Monday, June 11, 2012 6:25 PM

Answers

  • CrmConnection conn = CrmConnection.Parse("Server=<server>:<port>/<org>;Domain=<domain>;Username=<user>;Password=<password>");
    CrmOrganizationServiceContext context = new CrmOrganizationServiceContext(conn);
    
    var contacts =
    	(
    		from c in context.CreateQuery("contact")
    		where ((string)c["firstname"]).StartsWith("F")
    		select c
    	).ToList();

    The generic ServiceContext above will allow you to write LINQ queries, but only using the generic object model (Entity/Attribute).  If you wish to access the early bound, you will have to use CrmSvcUtil.exe to pre-generate them:

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


    --pogo (pat) @ pogo69.wordpress.com



    Monday, June 11, 2012 10:14 PM

All replies

  • CrmConnection conn = CrmConnection.Parse("Server=<server>:<port>/<org>;Domain=<domain>;Username=<user>;Password=<password>");
    CrmOrganizationServiceContext context = new CrmOrganizationServiceContext(conn);
    
    var contacts =
    	(
    		from c in context.CreateQuery("contact")
    		where ((string)c["firstname"]).StartsWith("F")
    		select c
    	).ToList();

    The generic ServiceContext above will allow you to write LINQ queries, but only using the generic object model (Entity/Attribute).  If you wish to access the early bound, you will have to use CrmSvcUtil.exe to pre-generate them:

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


    --pogo (pat) @ pogo69.wordpress.com



    Monday, June 11, 2012 10:14 PM
  • Thanks Pogo, just what I needed.
    Tuesday, June 12, 2012 2:26 PM