Thursday, June 9, 2011

Create Entity in Microsoft Dynamics CRM 2011 from SIlverlight

This tutorial will show you how to create an entity record in Microsoft Dynamics CRM 2011 using C# in Silverlight.

First things first.  You have to set up your Silverlight app to make a web services connection to CRM.   The best tutorial I have found for this is located here in the MSDN:

Now once that is done you can create a record in CRM using the following syntax.  In the following example I am creating a record of type "new_testdata", which is a custom entity I have created.

Here is the call in C#:

//note that you need to specify the type of request here
OrganizationRequest request = new OrganizationRequest() { RequestName = "Create" };
Entity entity = new Entity();
entity.LogicalName = "new_testdata";

//One of the biggest things you deal with in SilverLight is the way you work with properties and Attributes
CrmSdk.KeyValuePair<string, object> attName = new CrmSdk.KeyValuePair<string, object>();
attName.Key = "new_name";
attName.Value = "Data - " + DateTime.Now.ToString();
entity.Attributes = new AttributeCollection();

//request properties need to be explicitly named as strings
request["Target"] = entity; 

IOrganizationService service = SilverlightUtility.GetSoapService();

//depending on how you do things your calls will most likely be asynchronous
service.BeginExecute(request, new AsyncCallback(CrmCreate_Callback), service);

Since this is an asynchronous call my callback method "CrmCreate_Callback" that returns the entities Guid from the response looks like this:

private void CrmCreate_Callback(IAsyncResult result)
        OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
        Guid results = (Guid)response["id"];
        this.ReportMessage("id: " + results.ToString());
    catch (Exception ex)
        throw ex;

You will notice that the biggest change in thinking and syntax will be that you have to specify your request and response properties as strings explicitly in code.

Really though once you understand the differences in working with the two concepts it really isn't a tough nut to crack.


  1. Hi,

    Thanks for the extremely helpful info, using it in my plugin right now.
    I was wondering if there is an overview for using this syntax? On MSDN there only is a sample of retrievemultiple, and I need to figure out how to use associate with SOAP..
    There is a requestname overview on MSDN, but that isn't very helpful if you don't know what parameters you should provide..
    For example, how did you figure out that you should use 'target' for the createrequest?

    Thx in advance,

    Arne Deruwe

  2. If you look at the normal request syntax you will see what members you need.

    Also, the MSDN pages for the requests all have a list of members in a link towards the bottom of the page that will also specify which are required even if the main request page doesn't have any real code examples. The MSDN is usually right for this but I have found mistakes.

  3. I am trying to implement this, but I am crashing at this point.

    OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);

    I am adding two items to the attributes collection when trying to create a new address record during a button click on the Account form. The parentID and the ObjectTypeCode (mainly just to see if I could get it working, more will be added later)

    Any clue you could offer as to why this may be happening would be great!


  4. Sorry, the specific error I am getting is

    The Remote Server has returned an error: Not Found.

  5. Is your URL correct for your service reference?

  6. Fairly certain, otherwise it wouldn't be able to update in visual studio correct?

    I have switched over to trying SilverCRMSOAP (but still using the same concepts you show above), which I thought had functionality to automatically connect based on the context of where ever the application is being run from and I am still getting the same error.

    I'm hoping it's not some kind of environment error, but since I can connect to CRM from my dev machine I can't see it being a permissions problem of some kind.

  7. I personally have gotten the individual steps to work in MSDN article to work for me. I tried SilverCRMSOAP library once and failed and then just have followed the manual steps in the article since.

  8. That's what I tried first.

    Aww well I'll keep plugging away, thanks! =)

  9. I think you are correct about the URL then if you can do some calls but not others. I am only familiar with the specific pattern for using web services presented above and also using the MSDN article for setup.

  10. Well I seem to have gotten it a step further. Is there anyway to set the uri without going through an html page? Can I do it while the program is running through initialization?

  11. I might be confused, but the proxy or service should have a URL attribute you can modify. Does it not?

  12. If there was I couldn't find it, I gave up on this method and went to a WCF service while feeding in some hard coded info (I know bad me!) on the connection/server and that worked out for me.

    Thanks for the help though, I'll revisit this method when I have more time in the future!

  13. How can i change the requirement level of attribute using silverlignt?