Monday, 6 March 2017

Console Application Connect to MSCRM online 2016

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;

namespace TestMsApp



{
public class Program


{
static IOrganizationService _service ;

static void Main(string[] args)



{
// ConnectToMSCRM("rahuljim@rahuljim.onmicrosoft.com ", "password" "https://damics.com/XRMServices/2011/Discovery.svc");
ConnectToMSCRM("user id", "password", "https://***2011/Organization.svc");
        
Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;




if (userid == Guid.Empty) return;//Check for CRM Connection Establishment. If Not return, other wise will proceed to next step
try


{
string queryString = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>




<entity name='account'>

<attribute name='name' />

<attribute name='primarycontactid' />

<attribute name='telephone1' />

<attribute name='accountid' />

<order attribute='name' descending='false' />

<filter type='and'>

<condition attribute='statecode' operator='eq' value='0' />

<condition attribute='statuscode' operator='eq' value='1' />

</filter>

</entity>
</fetch>";

string fetchAcccount = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>




<entity name='account'>

<all-attributes />

<order attribute='name' descending='false' />

<filter type='and'>

<condition attribute='accountnumber' operator='eq' value='{0}' />

</filter>

</entity>
</fetch>";

fetchAcccount = String.Format(fetchAcccount, "AF3HN2S4");

EntityCollection ec = _service.RetrieveMultiple(new FetchExpression(fetchAcccount));

if (ec.Entities.Count > 0)



{
string output = string.Empty;
foreach (var item in ec.Entities)



{
//String
if (item.Attributes.Contains("name")) //Check for fullname value exists or not in Entity Collection
output += "Full Name : " + item.Attributes["name"] + "\n";



}
Console.WriteLine(output);
Console.ReadKey();
// Update Account
Guid parentAcc = new Guid("C3390F45-D5FF-E611-8116-C4346BDCFDE1");
Entity account = new Entity();



account.Id = ec.Entities[0].Id;
account.LogicalName = "account";
account["websiteurl"] = "google@gmail.com";
account["parentaccountid"] = new EntityReference("account", parentAcc);
account["address1_addresstypecode"] = new OptionSetValue(1);



_service.Update(account);

 

}

 

}
catch (Exception ex)



{
Console.WriteLine(ex.Message);



}

}
public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)



{
try


{
ClientCredentials credentials = new ClientCredentials();



credentials.UserName.UserName = UserName;

credentials.UserName.Password = Password;
Uri serviceUri = new Uri(SoapOrgServiceUri);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);



proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;



}
catch (Exception ex)



{
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();



}

}

}

}

No comments:

Post a Comment