开发者

Update a CRM 2011 record using LINQ example

Can someone please post a confirmed example using linq to retrieve and update a record in CRM Dynamics 2011.

Microsoft claims this is "unsuppor开发者_如何学编程ted" but I have a suspicion it is possible.


I use the "early bound" approach where you generate C# entity classes using the CrmSvcUtil.exe tool, but make sure you use the /codecustomization switch that you'll find in various examples. You'll need the latest version of the CRM 2011 SDK, and must run CrmSvcUtil.exe from the \bin folder of that (don't use the version that installs with CRM).

Your project will need to reference Microsoft.Xrm.Client, Microsoft.Xrm.Sdk and Microsoft.Crm.Sdk.Proxy plus a few others from the .Net framework (look at the build errors to see what you're missing, then add them until it builds).

Here is a basic code snippet that retrieves a Contact entity, updates one of its fields, then saves it back to CRM:

CrmDataContext dc = new CrmDataContext("Xrm");

Contact contact = (from c in dc.ContactSet
                   where ...whatever...
                   select c).FirstOrDefault();

contact.FirstName = "Jo";

dc.SaveChanges();

(Note that CrmDataContext is the name of my data context. You can set this name using one of the CrmSvcUtil command line switches).

You'll also need to add a few things to your web.config:

<configSections>
   <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" />
</configSections>

<connectionStrings>
  <add name="Xrm" connectionString="Server=http://<your crm url>; Domain=<your domain>; Username=<a crm user id>; Password=<their password>" />
</connectionStrings>

<microsoft.xrm.client>
   <contexts>
      <add name="Xrm" type="" />
   </contexts>
</microsoft.xrm.client>

This is assuming you are running CRM on your corporate network, so the account and domain specified in the connection string would be an AD account, who is set up as a CRM user with relevant permissions to retrieve and update entities.


This is a rough example of using the ODATA provider connected to the online provider

             var serverConfig = GetServerConfig(sessionKey);
            // Connect to the Organization service. 
            // The using statement ensures that the service proxy will be properly disposed.

            using (var serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
            {
                // This statement is required to enable early-bound type support.
                serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                using (var orgContext = new CrmServiceContext(serviceProxy))
                {
                    return orgContext.AccountSet.Where(item => item.Id == id).Select().Single();
                }
            }

there's also a good example in the SDK:

CRM2011Sdk\sdk\samplecode\cs\wsdlbasedproxies\online

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜