Sample: Silverlight 2 and CRM


Ok, so here it is (download);   I finally had the chance to finish this sample and put it in a form that others can learn from.  Various folks both from the Silverlight team and the CRM team were involved in the process of finding ways to make it work (not listing their names here to keep their privacy) so thank you for that.  The sample is extremely simple and is just meant to illustrate how to call the CRM service from within Silverlight 2 using the proxy that Silverlight generates.  The two key pieces of the sample are:


·         A method to wire up the CRM service: It has special logic that injects the CRM authentication token so that CRM can effectively process the calls (see my previous post for a bit more background).


·         Usage of DynamicEntity when retrieving data from CRM.


Remember, the sample is provided “AS IS”; I bet there are many others out there that have come up with other patterns/helper-methods to achieve similar results.


Happy coding


Update:


I realized that I didn’t include explicit instruction on how to run this sample (I always assume that my audience is geek enough to understand everything I ramble he he) so here they are:


Requirements:


·         Visual Studio 2008 + Silverlight SDK + Silverlight Tools for Visual Studio 2008


·         Remember that in order for Silverlight to be able to call a web service the root of the server hosting the service must contain a clientaccesspolicy.xml (or equivalent) file stating that the server allows clients to issue calls to it. The project includes a sample clientaccesspolicy.xml that you can drop in the <crmwebroot> of your server. For more information about these security measures refer to the  Silverlight documentation.


·          


Instructions:


1.       Make sure to drop the policy file (clientaccesspolicy.xml) under the Web root of your CRM server.


2.       On page.xaml.cs change the values of the global variables to point to your server:


a.       _serverUrl


b.      _organizationName


3.       Open the project, compile it and run it J


a.       If you have troubles compiling/running the project make sure you don’t have namespace conflicts (check this post )


4.       If you want to host the sample outside of VS well, you will need to setup your own IIS website and/or put your files under  the \ISV\[yoursubfolder] folder (check documentation on the CRM SDK)


 


Comments (29)
  1. Silverstarter says:

    im trying out your new example atm

    theres one problem

    it aborts with a

    runtime error at line 453

    Sys.InvalidOperationExeption:InitializeError error #2103 in control ‘Xaml1’ invalid or not well-formed application. check manifest

    (translated by myself, if the errormessage isnt too good 😉 )

    it that a known problem? could it be a problem with blocked access? (sitting behind a corp. firewall which prevents access here and there)

    anything i could try out myself? didnt found a way to debug, cause it aborts even before any silverlight code is reached

    greetings

    Sivlerstarter

  2. Silverstarter says:

    hmm it seems like i ve found the(?) problem

    the service reference (CrmSdk) is not reachable

    could that be the problem?

  3. Silverstarter: Make sure you are setting the url of your server properly; also look at the comments at the top of Page.cs … you need to have a crossdomain.xml file in the root of your CRM server  (I included such file as part of the sample you can can drop it in the root of your crm server).  For more information about cross domanin policies and silverlight please refer to the silverlight documentation.

  4. Aswanee says:

    Dear humlezg

    I have same problem when try to open any page have SilverLight component this page not on my pc it in any other site

    and i tried to open the same pages form other machin and it successed !!! the error appain as Javascript error as “Error: Sys.InvalidOperationException:InitializeError Error#2103 in control’Xaml1′:2103An error has occurred”

    Also I already install VS2005, VS2008 on the same Machine

  5. Aswanee:

    You probably have an older version of the silverlight RUNTIME.  Make sure you uninstall any previous beta versions of the Silverlight runtime and install the RTM version.  If that doesn’t solve your problem I would suggest you consult the silverlight forums/support.

  6. jocelyn says:

    Hi, i just downloaded and tested the project. I think the problem was that if you go into the properties of the project, you’ll find that the namespace is "CRMSilverlightDirect" instead of "CrmSilverlightDirect". when I changed that to make it consistent to the rest of the project, it worked great for me! thanks for posting this sample!

  7. I was doing some research on Silverlight 2 &amp; Dynamics CRM integration and found this post by Humberto

  8. &#160; There are multiple ways that you can call the CRM Services from Silverlight 2.&#160; I&#8217;ve

  9. &#160; There are multiple ways that you can call the CRM Services from Silverlight 2.&#160; I&#8217;ve

  10. &#160; There are multiple ways that you can call the CRM Services from Silverlight 2.&#160; I&#8217;ve

  11. Silvertryer says:

    Do you have this up and running somewhere that I could see before going through the download/install.

  12. Jocelyn says:

    I was doing some research on Silverlight 2 &amp; Dynamics CRM integration and found this post by Humberto

  13. rudgr says:

    Hi,

    Great post!

    If I understand correctly, since Silverlight webservice calls are made asynchronously, theoretically the following could happen:

    t0: DisplayAccounts(…); >>> _lastIssuedRequest = RetrieveAccounts;

    t1: DisplayContacts(…); >>> _lastIssuedRequest = RetrieveContacts;

    t2: Service_ExecuteCompleted (for t0) >>> _lastIssuedRequest == RetrieveContacts; (oops! completed event was for RetrieveAccounts)

    t3: Service_ExecuteCompleted (for t1) >>> _lastIssuedRequest == RetrieveContacts

    Is there another way to determine which method initiated the request that resulted in the Service_ExecuteCompleted event?

    Thanks in advance!

  14. rudgr

    Yes you are correct, that can happen when working with asynch calls. I noticed the potential problem when developing the sample but didn’t spend much time dealing with it.  The only alternative that comes to mind would be to recursively "chain" your calls… that would make them sequential;… the code would be somewhat ugly tough.

  15. jocelyn says:

    I was doing some research on Silverlight 2 &amp; Dynamics CRM integration and found this post by Humberto

  16. jocelyn says:

    I was doing some research on Silverlight 2 &amp; Dynamics CRM integration and found this post by Humberto

  17. jocelyn says:

    I was doing some research on Silverlight 2 &amp; Dynamics CRM integration and found this post by Humberto

  18. Rudgr says:

    Hi,

    Any idea how to get the GUID of the CRM user doing the request? (i.e. WhoAmIRequest?)

    Thanks!

    rudgr

  19. Gjukawa says:

    The sample is not working on my end with SL2 RTM and VS 2008. After resolving the above mentioned errors, I am getting this error

    "The name already exists in the tree"

  20. rudgr

    Yes, the WhoAmIRequest will give the what you are looking for.  Just cradt the request (using the proxy that SL generates) and use the .execute method

  21. Gjukawa,

    I haven’t seen that error before. Sorry :/

  22. VJ APU says:

    Hi

    So I fixed the namespace problem as per Jocelyns post and now I get the page loaded and I can enter the seach critera.

    Next we get below. Any idea. Is it that crossdomain stuff?

    System.ServiceModel.CommunicationException was unhandled by user code

     Message="An error occurred while trying to make a request to URI ‘http://127.0.0.1/mscrmservices/2007/crmservice.asmx‘. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. Please see the inner exception for more details."

     StackTrace:

          at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)

          at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)

          at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)

          at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)

          at CrmSilverlightDirect.CrmSdk.CrmServiceSoapClient.CrmServiceSoapClientChannel.EndExecute(IAsyncResult result)

          at CrmSilverlightDirect.CrmSdk.CrmServiceSoapClient.CrmSilverlightDirect.CrmSdk.CrmServiceSoap.EndExecute(IAsyncResult result)

          at CrmSilverlightDirect.CrmSdk.CrmServiceSoapClient.OnEndExecute(IAsyncResult result)

          at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

     InnerException: System.Security.SecurityException

          Message=""

          StackTrace:

               at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)

               at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)

               at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)

          InnerException: System.Security.SecurityException

               Message="Security error."

               StackTrace:

                    at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)

                    at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)

                    at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)

               InnerException:

  23. Mahesh says:

    Hi,

    After installing the code and ruuing the project i m getting below error:

    Sys.InvalidOperationExeption:InitializeError error #2103 in control ‘Xaml1’ invalid or not well-formed application. check manifest

    I have change the URL and Orgarnization name also.

    I have updated the service refernce also.

    Please let me where i m wrong..

    Thanks & Regards,

    Mahesh

  24. Mahesh not sure what is it that you are facing but the error seems to indicate that you modified a Xaml piece (in page.xaml perhaps??). Make sure your xaml is valid and that all the VS tools that you have are for SL 2 (I haven’t tried it with the SL beta 3 tools).

    Regards

  25. VJ APU,

    Yes, the exception seems to be pretty self explanatory.  Look at http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx for more info

  26. Siam4u says:

    Most of the time there CRM service reference which i have added is throwing a TIMEOUT Error.

  27. bobhopeisgod says:

    after having attempted with this example and a few others, it appears that nothing about this is truly async. i was under the impression that the point behind silverlight not handing us the ability to do synchronous calls was so that we would not be blocking the UI thread, but running this simple example shows the UI thread blocked both instantiating a service client, (and even worse) plus all subsequent "async" calls. is there some way of making these run on a background thread so it’s not locking up the UI? even this example with the "loading" and "searching" are locking the UI so those don’t actually appear at all until AFTER the work is done

  28. Adeel says:

    Now i want to insert record in crm using this applicatio?

    How can i do this using this application?

Comments are closed.

Skip to main content