public static void DataverseConnector()
{
try
{
string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";
using (ServiceClient serviceClient = new ServiceClient(conn))
{
if (serviceClient.IsReady)
{
// Retrieve the accounts
var query = new QueryByAttribute("account")
{
ColumnSet = new ColumnSet("name")
};
query.AddAttributeValue("address1_city", "Redmond");
EntityCollection accounts = serviceClient.RetrieveMultiple(query);
//Convert the EntityCollection to a EntityReferenceCollection
var accountReferences = new EntityReferenceCollection();
accounts.Entities.ToList().ForEach(x => {
accountReferences.Add(x.ToEntityReference());
});
// The contact to associate to the accounts
var contacttoAssociateWithAccount = new EntityReference("contact",new Guid("edb87117-ce7e-ed11-81ab-000d3ac9cc70"));
// The relationship to use
var relationship = new Relationship("account_primary_contact");
// Use the Associate method
serviceClient.Associate(contacttoAssociateWithAccount.LogicalName, contacttoAssociateWithAccount.Id, relationship, accountReferences);
}
else
{
Console.WriteLine("A web service connection was not established.");
}
}
// Pause the console so it does not close.
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
No comments:
Post a Comment