private void SaveNextRecord(List <Contact> dirtyCollection, string errorMessage, Action callBack) { Contact contactToSave = dirtyCollection[0]; if (contactToSave.ContactId == null) { OrganizationServiceProxy.BeginCreate(contactToSave, delegate(object r) { try { Guid newID = OrganizationServiceProxy.EndCreate(r); contactToSave.ContactId = newID; contactToSave.EntityState = EntityStates.Unchanged; } catch (Exception ex) { // Something when wrong with create errorMessage = errorMessage + ex.Message + "\n"; } dirtyCollection.Remove(contactToSave); if (dirtyCollection.Count == 0) { callBack(); } else { SaveNextRecord(dirtyCollection, errorMessage, callBack); } }); } else { OrganizationServiceProxy.BeginUpdate(contactToSave, delegate(object r) { try { OrganizationServiceProxy.EndUpdate(r); contactToSave.EntityState = EntityStates.Unchanged; if (contactToSave.OwnerId != null) { // Assign the record OrganizationServiceProxy.RegisterExecuteMessageResponseType("Assign", typeof(AssignResponse)); AssignRequest assignRequest = new AssignRequest(); assignRequest.Target = contactToSave.ToEntityReference(); assignRequest.Assignee = contactToSave.OwnerId; OrganizationServiceProxy.Execute(assignRequest); } } catch (Exception ex) { // Something when wrong errorMessage = errorMessage + ex.Message + "\n"; } dirtyCollection.Remove(contactToSave); if (dirtyCollection.Count == 0) { callBack(); } else { SaveNextRecord(dirtyCollection, errorMessage, callBack); } }); } }
private void SaveNextRecord(List<Contact> dirtyCollection,string errorMessage,Action callBack) { Contact contactToSave = dirtyCollection[0]; if (contactToSave.ContactId == null) { OrganizationServiceProxy.BeginCreate(contactToSave, delegate(object r) { try { Guid newID = OrganizationServiceProxy.EndCreate(r); contactToSave.ContactId = newID; contactToSave.EntityState = EntityStates.Unchanged; } catch (Exception ex) { // Something when wrong with create errorMessage = errorMessage + ex.Message + "\n"; } dirtyCollection.Remove(contactToSave); if (dirtyCollection.Count == 0) callBack(); else SaveNextRecord(dirtyCollection, errorMessage, callBack); }); } else { OrganizationServiceProxy.BeginUpdate(contactToSave, delegate(object r) { try { OrganizationServiceProxy.EndUpdate(r); contactToSave.EntityState = EntityStates.Unchanged; if (contactToSave.OwnerId != null) { // Assign the record OrganizationServiceProxy.RegisterExecuteMessageResponseType("Assign", typeof(AssignResponse)); AssignRequest assignRequest = new AssignRequest(); assignRequest.Target = contactToSave.ToEntityReference(); assignRequest.Assignee = contactToSave.OwnerId; OrganizationServiceProxy.Execute(assignRequest); } } catch (Exception ex) { // Something when wrong errorMessage = errorMessage + ex.Message + "\n"; } dirtyCollection.Remove(contactToSave); if (dirtyCollection.Count == 0) callBack(); else SaveNextRecord(dirtyCollection, errorMessage, callBack); }); } }