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


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

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.
    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

  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 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

Leave a reply to Karan Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.