In this post, we will create a console app that performs a CRUD operation on Dynamics 365.
In Visual Studio, create a new Console App:

You will see:

Add Microsoft.CrmSdk.Xrm.Tooling.CoreAssembly using NuGet:

Code:
using System.Collections.Generic; |
using System.Threading.Tasks; |
using Microsoft.Xrm.Tooling.Connector; |
using Microsoft.Crm.Sdk.Messages; |
using Microsoft.Xrm.Sdk.Query; |
namespace Carl.Dynamics365CRUD |
static void Main( string [] args) |
var connectionString = @"AuthType = Office365; Url = https://yourorg.crm.dynamics.com/;Username=youremail;Password=yourpwd" ; |
CrmServiceClient conn = new CrmServiceClient(connectionString); |
IOrganizationService service; |
service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy; |
Entity contact = new Entity( "contact" ); |
contact[ "firstname" ] = "Bob" ; |
contact[ "lastname" ] = "Smith" ; |
Guid contactId = service.Create(contact); |
Console.WriteLine( "New contact id: {0}." , contactId.ToString()); |
Entity retrievedContact = service.Retrieve(contact.LogicalName, contactId, new ColumnSet( true )); |
Console.WriteLine( "Record retrieved {0}" , retrievedContact.Id.ToString()); |
Entity updatedContact = new Entity( "contact" ); |
updatedContact = service.Retrieve(contact.LogicalName, contactId, new ColumnSet( true )); |
updatedContact[ "jobtitle" ] = "CEO" ; |
updatedContact[ "emailaddress1" ] = "test@test.com" ; |
service.Update(updatedContact); |
Console.WriteLine( "Updated contact" ); |
ColumnSet attributes = new ColumnSet( new string [] { "jobtitle" , "emailaddress1" }); |
retrievedContact = service.Retrieve(contact.LogicalName, contactId, attributes); |
foreach (var a in retrievedContact.Attributes) |
Console.WriteLine( "Retrieved contact field {0} - {1}" , a.Key, a.Value); |
service.Delete(contact.LogicalName, contactId); |
Console.WriteLine( "Deleted" ); |
Console.WriteLine(ex.Message); |
No comments:
Post a Comment