Using Organization.svc in Silverlight for CRM 2011 Online/OnPremise

  • Create a new Silverlight 4 Application named CRM2011OnlineSilverlightApp.
  • Add Service Reference to the Organization.Svc (https://orgname.crm4.dynamics.com/XrmServices/2011/Organization.svc).
  • crm for North America
  • crm4 for Europe
  • crm5 for Asia
    • Give the namespace as OrgServiceReference
  • Open the reference.cs and replace System.Collections.Generic.KeyValuePair with KeyValuePair
  • Add the following helper files from the SDK (..\sdk\samplecode\cs\silverlight\soapforsilverlight\soapforsilverlightsample)
  • silverlightextensionmethods.cs
  • silverlightutility.cs
  • Rename namespace SoapForSilverlightSample.CrmSdk to CRM2011OnlineSilverlightApp.OrgServiceReference in silverlightextensionmethods.cs file.
  • Add a Button and a TextBlock to our Silverlight application.
  • Add the following code.
 private void btnCreateContact_Click(object sender, RoutedEventArgs e)
 {
 // Get IOrganizationService instance
 IOrganizationService myOrgService = SilverlightUtility.GetSoapService();

Entity myContact = new Entity();
 myContact.LogicalName = "contact";

// Create the AttributeCollection
 myContact.Attributes = new AttributeCollection();

// Specify Last Name and First Name for the contact to be created
 KeyValuePair<string, object> myKVPLastName = new KeyValuePair<string, object>();
 myKVPLastName.Key = "lastname";
 myKVPLastName.Value = "Twain";

 KeyValuePair<string, object> myKVPFirstName = new KeyValuePair<string, object>();
 myKVPFirstName.Key = "firstname";
 myKVPFirstName.Value = "Shania";

myContact.Attributes.Add(myKVPLastName);
 myContact.Attributes.Add(myKVPFirstName);

 OrganizationRequest myOrgRequest = new OrganizationRequest();
 myOrgRequest.RequestName = "Create";

// call the Create method
 myOrgService.BeginCreate(myContact, myCreateHandler, myOrgService);
 }

private void myCreateHandler(IAsyncResult ar)
 {
 IOrganizationService orgService = ar.AsyncState as IOrganizationService;

Dispatcher.BeginInvoke(() =>
 {
 txtStatus.Text = "Contact Guid - " + orgService.EndCreate(ar);
 });
 }
  • Create a web resource in CRM and upload the XAP, and to test it add it to any form inside CRM.
  • Click on create to create the contact record.
  • Download the project.  (change the extension to zip from docx)
Hope it helps.

https://nishantrana.files.wordpress.com/2011/11/crm2011onlinesilverlightapp.docx


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

9 thoughts on “Using Organization.svc in Silverlight for CRM 2011 Online/OnPremise”

  1. Hi Nishant,

    I am bit stuck with updating the reference.cs file, the image you provided allows to browse through, but in my application it does not gives that option. Any idea how to go about it. I can open the file from the folder.but when I update I get some more errors…Any suggestions are welcomed.

    Thanks

    Like

  2. Hi,

    Thank you ! Just like as in the post, i am create a Task instead of contact.

    How do i ::

    1) Assign the task to a particular user. Ie, Set the OwnerId. “assignrequest’ equivalent in SOAP Endpoint.

    2. The regarding object Id was set. How do we specify the the type of the regardingobjectid?

    If you could help me with these two show stoppers of mine at the moment, it would be great !!! 🙂

    Thanks in Advance ….

    Like

  3. I tried a fetchxml for getting some records under the same above framework. BUt , it does not show me anything .. not even error on page. Could you please tell me where i am going wrong ?

    public void getDiagnosesFromCRM(string strLeadId)
    {
    string fetchDiagnose = “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “”;
    FetchExpression fetchQuery = new FetchExpression()
    {
    Query = fetchDiagnose
    };

    OrganizationRequest request = new OrganizationRequest() { RequestName = “RetrieveMultiple” };
    request[“Query”] = fetchQuery;

    IOrganizationService service = SilverlightUtility.GetSoapService();

    service.BeginExecute(request, new AsyncCallback(getDiagnosesFromCRM_Handler), service);
    }

    public void getDiagnosesFromCRM_Handler(IAsyncResult result)
    {
    try
    {
    OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
    EntityCollection results = (EntityCollection)response[“EntityCollection”];

    if (results.Entities.Count == 0)
    {
    //sb.AppendLine(“There are no Account records in the system.”);
    }
    else
    {
    List collDiagnoses = new List { };
    foreach (c4h_sl_leadDiagnoses.OrgServiceReference.Entity entity in results.Entities)
    {
    Diagnose objDiagnose = new Diagnose();
    objDiagnose.Name = entity.GetAttributeValue(“new_name”);
    collDiagnoses.Add(objDiagnose);
    }
    dgDiagnoses.ItemsSource = collDiagnoses;
    }
    }
    catch (Exception ex)
    {
    ;
    }
    }

    // A local class
    public class Diagnose
    {
    public string Name { get; set; }
    }
    ==============
    dgDiagnoses is a datagrid , which otherwise with hardcoded values, shows properly.

    The fetch xml when tested on a normal sdk framework application gave ne proper list of results!
    But in this BeginExecute and EndExecute … it does not !

    Like

Share your thoughts

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