Reputation: 499
Can someone please post a confirmed example using linq to retrieve and update a record in CRM Dynamics 2011.
Microsoft claims this is "unsupported" but I have a suspicion it is possible.
Upvotes: 0
Views: 5307
Reputation: 1894
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
Upvotes: 0
Reputation: 31
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.
Upvotes: 3