Create Retrieve Update and Delete using oData (Rest endpoint) in Silverlight (CRM 2011)

Hi,

Sharing a basic example that performs CRUD operation using Rest endpoint in Silverlight.

namespace odataSilverlight
{
 public partial class MainPage : UserControl
 {
 public MainPage()
 {
 InitializeComponent();
 }

GGContext context;
 private void UserControl_Loaded(object sender, RoutedEventArgs e)
 {
 var orgServiceUrl = "http://r78-7-amscrm/GG/XRMServices/2011/OrganizationData.svc";

 // initialize the context
 context = new GGContext(new Uri(orgServiceUrl));

// using client http stack
 context.HttpStack = System.Data.Services.Client.HttpStack.ClientHttp;
 context.UseDefaultCredentials = false;
 context.Credentials = new NetworkCredential("administrator", "Secure*8", "AMS");
 // create contact record
 Contact contact = new Contact();
 contact.LastName = "Rana";
 contact.FirstName = "Akshaj";

// add the new contact to the contact set
 context.AddToContactSet(contact);
 // call BeginSaveChanges method and specify the callback method and the contact object
 context.BeginSaveChanges(CreateContactHandler, contact);

}
 private void CreateContactHandler(IAsyncResult result)
 {
 // in the call back method call the EndSaveChanges method
 // it returns DataServiceResponse object.
 // we can get the error information from this object in case an operation fails
 context.EndSaveChanges(result);
 // id of the created contact record
 Guid createdContactGuid = ((Contact)result.AsyncState).ContactId;

// Retrieve the created contact record
 DataServiceQuery<Contact> queryContact = (DataServiceQuery<Contact>)
 context.ContactSet.
 AddQueryOption("$filter", "ContactId eq guid'" + createdContactGuid + "'");
 queryContact.BeginExecute(RetrieveContactHandler, queryContact);

}

private void RetrieveContactHandler(IAsyncResult result)
 {
 DataServiceQuery<Contact> contacts = result.AsyncState as DataServiceQuery<Contact>;
 Contact retrievedContact = new DataServiceCollection<Contact>(contacts.EndExecute(result)).First<Contact>();

// Update the retrieved contact record's first name
 retrievedContact.FirstName = "Mickey";

context.UpdateObject(retrievedContact);
 context.BeginSaveChanges(UpdateContactHandler, retrievedContact);

}

private void UpdateContactHandler(IAsyncResult result)
 {
 context.EndSaveChanges(result);
 Contact contact = (Contact)result.AsyncState;

// Delete the updated contact record
 context.DeleteObject(contact);
 context.BeginSaveChanges(DeleteContactHandler, contact);
 }
 private void DeleteContactHandler(IAsyncResult result)
 {
 context.EndSaveChanges(result);
 }
 }
}

Hope it helps


Author: Nishant Rana

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

3 thoughts on “Create Retrieve Update and Delete using oData (Rest endpoint) in Silverlight (CRM 2011)”

  1. Hi Nishant

    I have certain data in Azure datastorage. The Rest End point of that Azure Data Storage are exposed.

    I want to retrieve that data with the help of CRM webresource(jscript) by calling external REST service of Azure data storage and want to show that data in my CRM web page.

    Have u ever tried it? Please help me with some reference code of similar requirement or some good material.

    Please help.

    Like

  2. Hi Nishant,
    I have some issues while updating a activity record.I am getting the following error.
    Please have a look at the error.
    I am getting the error while invoking the context.EndSaveChanges(result);

    {System.InvalidOperationException: An error occurred while processing this request. —> System.Data.Services.Http.WebException: Internal error at ‘HttpWebResponse.NormalizeResponseStatus’.
    at System.Data.Services.Http.XHRHttpWebResponse.NormalizeResponseStatus(Int32& statusCodeParam)
    at System.Data.Services.Http.XHRHttpWebResponse..ctor(XHRHttpWebRequest request, Int32 statusCode, String responseHeaders)
    at System.Data.Services.Http.XHRHttpWebRequest.CreateResponse()
    at System.Data.Services.Http.XHRHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
    at System.Data.Services.Client.SaveResult.AsyncEndGetResponse(IAsyncResult asyncResult)
    — End of inner exception stack trace —
    at System.Data.Services.Client.BaseAsyncResult.EndExecute[T](Object source, String method, IAsyncResult asyncResult)
    at System.Data.Services.Client.DataServiceContext.EndSaveChanges(IAsyncResult asyncResult)
    at ActivityEditor.MainPage.c__DisplayClass27.b__26()}

    I checked for the status and it is open and active.And I am not modifying any status so I think the issue is something other than I am thinking.
    Please anyone who is readding this and have solved the problem already.Kindly suggest any ways in order to solve the issue.

    Greets
    Dibyasingh Tripathy

    Like

  3. Hi.
    Helpful article !

    I have to update the Owner of the record…assign it to a Team. So, am I right that my only option is to use SOAP service endpoint in my Silverlight web resource rather than ODATA service endpoint?
    Please clarify.

    Thanks.

    Swaroop

    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