< Back

Get-CrmRecordsByFetch

Sat Jan 18, 2020 4:01 pm

NAME Get-CrmRecordsByFetch



SYNOPSIS

Retrieves CRM records by using FetchXML query.





SYNTAX

Get-CrmRecordsByFetch [-conn <CrmServiceClient>] [-Fetch] <String> [[-TopCount] <Int32>] [[-PageNumber] <Int32>]

[[-PageCookie] <String>] [[-AllRows] <SwitchParameter>] [<CommonParameters>]





DESCRIPTION

The Get-CrmRecordsByFetch cmdlet lets you retrieve up to 5,000 records from your CRM organization by using

FetchXML query.

The output contains CrmRecords (List or retrieved records), PagingCookie (for next iteration), and NextPage (to

indicate if there are more records on the next page).



if you need to paging the result, you can also specify TopCount, PageNumber and PagingCookie.



You can obtain FetchXML by using Advanced Find tool. As FetchXML query can be multiple lines, use "@ ... @" syntax

to speficy the query.





PARAMETERS

-conn <CrmServiceClient>

A connection to your CRM organization. Use $conn = Get-CrmConnection <Parameters> to generate it.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Fetch <String>

A FetchXML query to retrieve records. You can obtain FetchXML by using Advanced Find tool. Use "@ ... @"

syntax to specify the query.



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-TopCount <Int32>

Specify how many records you need to retireve at once (up to 5,000 at once).



Required? false

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



-PageNumber <Int32>

Specify starting page number for paging. Starting from 1.



Required? false

Position? 3

Default value

Accept pipeline input? false

Accept wildcard characters? false



-PageCookie <String>

Specify cookie string for paging. Keep previous PagingCookie value for next iteration.



Required? false

Position? 4

Default value

Accept pipeline input? false

Accept wildcard characters? false



-AllRows [<SwitchParameter>]

By default the first 5000 rows are returned, this switch will bring back all results regardless of how many



Required? false

Position? 5

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS









OUTPUTS









NOTES









-------------------------- Example 1 --------------------------



Get-CrmRecordsByFetch -conn $conn -Fetch @"

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" no-lock="true">

<entity name="account">

<attribute name="name" />

<attribute name="primarycontactid" />

<attribute name="telephone1" />

<attribute name="accountid" />

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

</entity>

</fetch>

"@



This example retrieves account records by using FetchXML and results contains CrmRecords, PagingCookie and

NextPage.

Please note that copying and pasting above example may not work due to multiline issue. Please remove all

whitespace before last @"







Key Value

--- -----

CrmRecords {@{name_Property=[name, A. Datum Corporation (sample)]; name=A. Datum Corporation (s...

Count 5

PagingCookie <cookie page="1"><name last="Litware, Inc. (sample)" first="A. Datum Corporation (sa...

NextPage False



-------------------------- Example 2 --------------------------



$result = Get-CrmRecordsByFetch @"

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" no-lock="true">

<entity name="account">

<attribute name="name" />

<attribute name="primarycontactid" />

<attribute name="telephone1" />

<attribute name="accountid" />

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

</entity>

</fetch>

"@

PS C:\\>$result.CrmRecords



This example stores retrieved result into $result variable, then show records by CrmRecords property.

When omitting parameter names, you do not provide $conn, cmdlets automatically finds it.







name_Property : [name, A. Datum Corporation (sample)]

name : A. Datum Corporation (sample)

primarycontactid_Property : [primarycontactid, Microsoft.Xrm.Sdk.EntityReference]

primarycontactid : Rene Valdes (sample)

telephone1_Property : [telephone1, 555-0158]

telephone1 : 555-0158

accountid_Property : [accountid, be02caab-6c16-e511-80d6-c4346bc43dc0]

accountid : be02caab-6c16-e511-80d6-c4346bc43dc0

returnProperty_EntityName : account

returnProperty_Id : be02caab-6c16-e511-80d6-c4346bc43dc0

original : {[name_Property, [name, A. Datum Corporation (sample)]], [name, A. Datum Corporation

(sample)], [primarycontactid_Property, [primarycontactid,

Microsoft.Xrm.Sdk.EntityReference]], [primarycontactid,

Microsoft.Xrm.Sdk.EntityReference]...}

logicalname : account



name_Property : [name, Adventure Works (sample)]

name : Adventure Works (sample)

primarycontactid_Property : [primarycontactid, Microsoft.Xrm.Sdk.EntityReference]

primarycontactid : Nancy Anderson (sample)

telephone1_Property : [telephone1, 555-0152]

....



-------------------------- Example 3 --------------------------



$result1 = Get-CrmRecordsByFetch @"

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" no-lock="true">

<entity name="contact">

<attribute name="fullname" />

</entity>

</fetch>

"@



PS C:\\>$result1.CrmRecords.Count

5000



PS C:\\>$result1.NextPage

True



PS C:\\>$result1.PagingCookie

<cookie page="1"><contactid last="{890FDA88-4217-E511-80DB-C4346BC42D18}"

first="{E1D47674-4017-E511-80DB-C4346BC42D18}" /></cookie>



PS C:\\>$result2 = Get-CrmRecordsByFetch -conn $conn -Fetch @"

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" no-lock="true">

<entity name="contact">

<attribute name="fullname" />

</entity>

</fetch>

"@ -TopCount 5000 -PageNumber 2 -PageCookie $result1.PagingCookie



PS C:\\>$result2.CrmRecords.Count

453



PS C:\\>$result2.NextPage

False



This example stores retrieved result into $result1 variable, then use $result1.PagingCookie data for next

iteration.

FetchXML is exactly same but specifying additional Parameters for second command.









RELATED LINKS