Read, create, update and detele objects in Microsoft CRM 2011 on premise via WCF service.

How to read, create, update and detele objects in Microsoft CRM 2011 on premise via WCF service.

by admin Posted on 11. August 2011

Please note that this code works for ON PREMISE installation of Microsoft CRM 2011.

In this short article I will show you how to access items in CRM 2011 via new built in WCF webservice. You should use WCF when accessing CRM from code and REST when accessing from client browser of Silverlight application.

Of course you can use old style CRM 4.0 like aproach with QueryExpression class and then RetreiveMultiple on IOrganizationService instance, but there is also easier approach.

You will need to download and extract CRM 2011 SDK. Current version is 5.0.3 and you can find it on this link : http://www.microsoft.com/downloads/en/details.aspx?FamilyID=420f0f05-c226-4194-b7e1-f23ceaa83b69

Here are the steps you need to follow :

  1. Create console application (with full .NET fw 4.0, not client profile) (you can ofcourse create WPF, WinForms or ASP.NET app, but for our example we will use console app)
  2. Locate crmsvcutil.exe in sdk\bin folder along with microsoft.crm.sdk.proxy.dll and microsoft.xrm.sdk.dll.
  3. Both assemblies add as reference to the console project and add also .NET FWs System.ServiceModel and System.Runtime.Serialization.
  4. Use crmsvcutil.exe to use early bound aproach, that will generate classes for all entities within CRM 2011 and also your custom ones. (this is essential something like adding webservice reference to webservice in CRM 4.0 times). I used commandline arguments like this

:crmsvcutil /url:http://server/organization-name/XRMServices/2011/Organization.svc /o:filename.cs /n:namespace /serviceContextName:context-name. /url – url of services, /o – output file, /n – namespace of generated classes, /serviceContextName is name of service,

but when you ommit this switch, no context will be generated at all. Context is used for accessing the entities via WCF.

Example :

CrmSvcUtil.exe /url:”http://kumar/Viveks/XRMServices/2011/Organization.svc” /out:”Entities.cs” /username:”administrator” /password:”pass@word1″ /serviceContextName:”Xrm”

Add generated .cs file to the project (copy to folder, button Show all files, include in project).

  1. Every generated entity will get it’s dedicated IQueryable collection, so for Account you should query AccountSet, for Annotation you should query AnnotationSet and so on. IQueryable means you can query the store with LINQ expressions.

For access you need only this code :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Xrm.Sdk.Client;

using Microsoft.Xrm.Sdk;

using System.ServiceModel.Description;

using service-namespace;

namespace CrmConsoleApplication2 {

class Program {

private static IOrganizationService _service;

private static OrganizationServiceProxy _serviceProxy;

static void Main(string[] args) {

IServiceConfiguration<IOrganizationService> orgConfigInfo =

ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(new Uri(“http://server/company-name/XRMServices/2011/Organization.svc&#8221;));

var creds = new ClientCredentials();

using (_serviceProxy = new OrganizationServiceProxy(orgConfigInfo, creds)) {

// This statement is required to enable early-bound type support.

_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

_service = (IOrganizationService)_serviceProxy;

List<Account> acc;

using (context srv = new context(_service)) {

acc = srv.AccountSet.Where(a => a.AccountId != null).ToList();

Account a1 = acc[0]; a1.Name = a1.Name + ” mod mod”; Account a2 = acc[1]; a2.Name = a2.Name + ” mod mod”; srv.UpdateObject(a1);
srv.UpdateObject(a2); srv.SaveChanges();
}
} } } }

And this is what you get @ runtime :

                       

 

You should also wrap OrganizationServiceProxy and also contex with using, because they both use IDisposable interface. Calling .ToList() after where on AccountSet will trigger downloading of all entities.

Please be aware, that downloading many objects will create big response from server

To create and delete record, you can use snippet like this :

                    Account newaaa = new Account();

newaaa.Name = “test acc”;

srv.AddObject(newaaa);

srv.SaveChanges();

Account newAcc = srv.AccountSet.Where(a => a.Name == “test acc”).FirstOrDefault();

srv.DeleteObject(newAcc);

srv.SaveChanges();

This will create new account, when invoking SaveChanges after AddObject, object newaaa will be filled with guid of freshly created Account. To delete object you have to “download” new object from service, you can’t use newaaa object, because context isn’t tracking the changes, so it can’t delete the object also

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s