Michael Philip's Blog

Writing about MS Tech with strong focus on Azure & CRM

Connect To Dynamcis 365 Online using Power Shell

Leave a comment

Hi,

In this post I am going to show a basic example using CRM SDK through power shell to connect to CRM .

Power shell is so useful when you need to execute operations through SDK without Visual studio installed in your environment.

Code :

clear;

#navigate to SDK Folder
cd C:\CRMSDK\SDK\Bin

#execute script
.\RegisterXRMTooling.ps1

#add crm snappin
Add-PSSnapin Microsoft.Xrm.Tooling.Connector

#Get Tool Alias
Set-Alias installUtil $env:windir\Microsoft.NET\Framework64\v4.0.30319\installutil.exe

#copy to gac
installUtil /u Microsoft.Xrm.Sdk.dll;

#Initialize connection
$CRMConn =Get-CrmConnection –ConnectionString “Url=https://MOQCRM20.crm6.dynamics.com/; Username=###; Password=###; authtype=Office365”

#Get Organization service proxy
$orgService=$CRMConn.OrganizationServiceProxy ;

#construct query
$query=”<fetch mapping=’logical’ distinct=’false’ aggregate=’true’>
<entity name=’account’>
<attribute name=’account’ alias=’recordcount’ aggregate=’count’/>
</entity>
</fetch>”;

#Create Query expression

$queryExp = New-Object -TypeName Microsoft.Xrm.Sdk.Query.FetchExpression -ArgumentList $query ;

#Get Results
$results=$orgService.RetrieveMultiple($queryExp);

#Extract Count
$countItem=[Microsoft.Xrm.Sdk.AliasedValue]$results.Entities[0][“recordcount”]

Write-Host $countItem.Value

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s