D365 Data Retrieval: FetchXml, Paging Cookies, and PowerShell

Open Windows PowerShell

Powershell

It seems like you're preparing your environment to work with Dynamics 365 using PowerShell. The commands you provided are steps to install Microsoft.Xrm.Data.The PowerShell module sets the execution policy and ensures that TLS 1.2 is enabled for secure communication with Dynamics 365. Here's a breakdown of each command.

Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser

Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser: This command installs the Microsoft.Xrm.Data.PowerShell module, which provides cmdlets to interact with Dynamics 365 data using PowerShell. By specifying -Scope CurrentUser, the module is installed for the current user only.

Windows

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned –Scope CurrentUser

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned –Scope CurrentUser: This command sets the execution policy to RemoteSigned, which allows the execution of scripts downloaded from the internet while requiring that all scripts have a digital signature from a trusted publisher. Again, -Scope CurrentUser specifies that this setting applies only to the current user.

Execution

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

[System.Net.ServicePointManager]:: SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12: This command ensures that TLS 1.2 is enabled for secure communication with Dynamics 365. Dynamics 365 requires TLS 1.2 for secure connections, so it's important to set this protocol to establish a connection successfully.

CurrentUser

After running these commands, you should be able to work with Dynamics 365 data using PowerShell commands provided by Microsoft.Xrm.Data.PowerShell module in a secure manner. Make sure to replace <your-D365-instance> and <your-access-token> with the appropriate values for your Dynamics 365 environment and authentication method.

$Username = "APY***@Trailcrm.com"
$Password = "apy****"

$pwd = ConvertTo-SecureString $Password -AsPlainText -Force

$credentials = New-Object System.Management.Automation.PSCredential($Username, $pwd)

$conn = Connect-CrmOnline -Credential $credentials -ServerUrl "https://apytrailcrm****.crm.dynamics.com/" -ForceOAuth

ISE

After establishing connect write the below code to get more Contact records using FetchXMl.

$pagingCookie = ""
$page = 1
$count = 5000

$executeMultipleSettings = [Microsoft.Xrm.Sdk.ExecuteMultipleSettings]::new()
$executeMultipleSettings.ContinueOnError = $true
$executeMultipleSettings.ReturnResponses = $true

# Using do while loop with FetchXml with below code will get more records
do {
    $varFetch = Get-CrmRecordsByFetch -conn $CRMConnection -Fetch @"
    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
        <entity name="contact">
            <attribute name="fullname" />
            <attribute name="contactid" />
            <order descending="false" attribute="fullname" />
            <filter type="and">
                <condition attribute="statecode" operator="eq" value="0" />
            </filter>
        </entity>
    </fetch>
"@

    $counter = [pscustomobject] @{ Value = 0 }
    $groupSize = 100
    $records = (Get-CrmRecordsByFetch -conn $CRMConnection -Fetch $varFetch -AllRows).CrmRecords
    $records | Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) }

    $testGroup | Foreach-Object {
        Start-ThreadJob -ArgumentList @($conn, $_.Group, $executeMultipleSettings) {
            param($CRMConnection, $group, $executeMultipleSettings)
        }
    }
} while ($varFetch.NextPage -ne $false)

# Retrieve records
$records = Get-CrmRecord -EntityLogicalName "contact" -PageSize 5000

# Check if more records are available
$nextPage = $records.MoreRecords
$pageSize = $records.PagingCookie

Microsoft


Similar Articles