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.

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