Friday, May 2, 2014

Using Powershell to integrate with MS Dynamics CRM Online

listAccounts.ps1
The Dynamics CRM SDK comes with samples of using Powershell, but they're all around admin functions and I needed integration functions. Worse yet, my project was Dynamics CRM Online and the examples were all for on-premise.
So begins another episode of HACKERMAN, champion of the oppressed codernauts.
                                                      
 [reflection.assembly]::loadfile("C:\CRMSDK\Microsoft.Crm.Sdk.proxy.dll")                                                              
 [reflection.assembly]::loadfile("C:\CRMSDK\Microsoft.xrm.client.dll")                                                                 
 [reflection.assembly]::loadfile("C:\CRMSDK\Microsoft.xrm.portal.dll")                                                                 
 [reflection.assembly]::loadfile("C:\CRMSDK\Microsoft.Crm.Outlook.Sdk.dll")                        
 [reflection.assembly]::loadfile("C:\CRMSDK\Microsoft.xrm.sdk.deployment.dll")                                                         
 [reflection.assembly]::loadfile("C:\CRMSDK\Microsoft.xrm.sdk.dll")                                                                    
 [reflection.assembly]::loadfile("C:\CRMSDK\Microsoft.xrm.sdk.workflow.dll")                                                           
 [reflection.assembly]::loadfile("C:\CRMSDK\antiXsslibrary.dll")  
 

 
 $creds = New-Object ServiceModel.Description.ClientCredentials   
 $creds.UserName.UserName = "mmeasel@FieldAware228.onmicrosoft.com"  
 $creds.UserName.Password = "password123" 
 $devcred = New-object ServiceModel.Description.ClientCredentials                                                                      
 $devcred.UserName.Username = "FieldAware"                                                                                             
 $devcred.UserName.Password = "password123"   
 
 $crmConnection = New-Object Microsoft.Xrm.Client.CrmConnection 

 $crmConnection.ServiceUri = "https://fieldaware228.crm.dynamics.com"
 $crmConnection.ClientCredentials = $creds 
 $crmConnection.DeviceCredentials = $devcred 
 $crmConnection
 
 
 $orgService = New-Object microsoft.xrm.client.services.organizationservice($crmConnection)
 
 $orgService.Initialize 
 
 
 
 $qex = New-Object Microsoft.xrm.sdk.query.queryexpression
  $qex.entityname = "account"
  $acols = new-object microsoft.xrm.sdk.query.columnset
  # just get the ones we need
  $acols.addColumn("name")
  $acols.addColumn("address1_telephone1")
  $acols.addColumn("address1_fax")
  $acols.addColumn("accountnumber")
  $acols.addColumn("description")
  $acols.addColumn("emailaddress1")
  $acols.addColumn("websiteurl")
  $acols.addColumn("address1_addressid")
  $acols.addColumn("address1_name")
  $acols.addColumn("address1_line1")
  $acols.addColumn("address1_stateorprovince")
  $acols.addColumn("address1_postalcode")
  $acols.addColumn("address1_country")
  $acols.addColumn("primarycontactid")
  
  $qex.columnSet = $acols
  $requestMultiple = new-object microsoft.xrm.sdk.messages.retrieveMultiplerequest
  $requestMultiple.Query = $qex
  $resultset = new-object microsoft.xrm.sdk.entitycollection
  $resultset = $orgService.Execute($requestMultiple)
  $accountId = [Guid]::Empty 
  $acctdata = @{}
  
  ForEach ( $ent in $resultset.Entitycollection.Entities ) {
        $Customer = @()
        $Location = @()
        $Contact = @()
        $accountId = $ent.id
        $acctdata = $orgService.retrieve("account", $accountId, $acols)
        
        $Customer += [pscustomobject] @{
                        dynAcctId = $accountId
                        name = $acctdata.attributes['name']
                        phone = $acctdata.attributes['address1_telephone1']
                        fax = $acctdata.attributes['address1_fax']
                        account = $acctdata.attributes['accountnumber']
                        notes = $acctdata.attributes['description']
                        email = $acctdata.attributes['emailaddress1']
                        website = $acctdata.attributes['websiteurl']
                                                }
        $Location += [pscustomobject] @{
                        dynLocId = $acctdata.attributes['address1_addressid']
                        name = $acctdata.attributes['address1_name']
                        streetName = $acctdata.attributes['address1_line1']
                        locality = $acctdata.attributes['address1_stateorprovince']
                        postcode = $acctdata.attributes['address1_postalcode']
                        country = $acctdata.attributes['address1_country']
                        atype = "branch"  
                                                }
        $dynContactRef = $acctdata.attributes['primarycontactid']
        if ($dynContactRef) {
        $dynContact = $dynContactRef.Id
        
        $ccols = new-object microsoft.xrm.sdk.query.columnset
        $ccols.addColumn("firstname")
        $ccols.addColumn("lastname")
        $ccols.addColumn("emailaddress1")
        $ccols.addColumn("telephone1")
        $contactdata = $orgService.retrieve("contact", $dynContact,$ccols)
        #$contactdata
        $Contact += [pscustomobject] @{
                        dynContactId = $dynContactRef.Id
                        firstName = $contactdata.attributes['firstname']
                        lastName = $contactdata.attributes['lastname']
                        email = $contactdata.attributes['emailaddress1']
                        phone = $contactdata.attributes['telephone1']
                                }
                        }
                
         $Customer
         $Location
         $Contact
  
  }