In my previous article, i explained all Create , Update ,Delete , Retrieve using Organization service and now in my
current article, I am trying to explain how to Create, Update, Delete, Retrieve using Organization OData service.
To use Organization OData service , Add ODataOrganization.svc Service Reference in your Visual studio Solution
In First Step I am declaring and Initializing SynchronizationContext Object, To Inatalizing Object i am using some functions
current article, I am trying to explain how to Create, Update, Delete, Retrieve using Organization OData service.
To use Organization OData service , Add ODataOrganization.svc Service Reference in your Visual studio Solution
In First Step I am declaring and Initializing SynchronizationContext Object, To Inatalizing Object i am using some functions
SynchronizationContext _syncContext = SynchronizationContext.Current; //Get the ServerUrl (ServerUrl is formatted differently OnPremise than OnLine) String _serverUrl = GetServerUrl(); if (!String.IsNullOrEmpty(_serverUrl)) { //Setup Context AEDevContext _context = new AEDevContext( new Uri(String.Format("{0}/xrmservices/2011/organizationdata.svc/", _serverUrl), UriKind.Absolute)); //This is important because if the entity has new //attributes added the code will fail. _context.IgnoreMissingProperties = true; } //Function for object Initalization public static String GetServerUrl() { String serverUrl = String.Empty; //Try to get the ServerUrl from the Xrm.Page object serverUrl = GetServerUrlFromContext(); return serverUrl; } private static String GetServerUrlFromContext() { // If the Silverlight is in a form, this will get the server url ScriptObject xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm"); ScriptObject page = (ScriptObject)xrm.GetProperty("Page"); ScriptObject pageContext = (ScriptObject)page.GetProperty("context"); //Uncomment before using as webResource //String serverUrl = (String)pageContext.Invoke("getServerUrl"); //comment before using as webResource String serverUrl ="http://crm2011:5555/ITSoft"; //The trailing forward slash character from CRM Online needs to be removed. if (serverUrl.EndsWith("/")) { serverUrl = serverUrl.Substring(0, serverUrl.Length - 1); } return serverUrl; }Code to Create Account
private void CreateAccount() { Account _account = new Account(); _account.Name = "Account created using Silverligh ODataService"; _context.AddToAccountSet(_account); _context.BeginSaveChanges(CreateAccount, _account); } private void CreateAccount(IAsyncResult result) { _context.EndSaveChanges(result); Account _account = result.AsyncState as Account; }Code to Update Account
private void UpdateAccount(Guid _accountid) { Account _account = new Account(); _account.Name = "Update account using Silverligh ODataService"; _account.AccountId = _accountid; _context.AttachTo("AccountSet", _account); _context.UpdateObject(_account); _context.BeginSaveChanges(UpdateAccount, _account); } private void UpdateAccount(IAsyncResult result) { _context.EndSaveChanges(result); Account _account = result.AsyncState as Account; }Code to Delete Account
private void DeleteAccount(Account _account) { _context.DeleteObject(_account); _context.BeginSaveChanges(DeleteAccount, _account); } private void DeleteAccount(IAsyncResult result) { Account deletedAccount = result.AsyncState as Account; _context.EndSaveChanges(result); }Code to Retrieve Account
private void RetrieveAccount(Guid _accountid) { DataServiceQuery<Account> query = (DataServiceQuery<Account>)_context .AccountSet.Where<Account>(a => a.AccountId == _accountid); query.BeginExecute(RetrieveAccount, query); } private void RetrieveAccount(IAsyncResult result) { DataServiceQuery<Account> results = result.AsyncState as DataServiceQuery<Account>; Account _account = new DataServiceCollection<Account>(results .EndExecute(result)).First<Account>(); }Code to Retrieve Multiple Account
private void RetrieveMultipleAccount() { DataServiceQuery<Account> accounts = (DataServiceQuery<Account>)this._context .AccountSet.Take<Account>(10); accounts.BeginExecute(RetrieveMultipleAccount, accounts); } private void RetrieveMultipleAccount(IAsyncResult result) { //Retrieve the query that was DataServiceQuery<Account> results = result.AsyncState as DataServiceQuery<Account>; ObservableCollection<Account> _accounts = new DataServiceCollection<Account>(results.EndExecute(result)); }
No comments:
Post a Comment