web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)
Answered

How to Import/Export Records from/to file with Microsoft.Xrm.Data.Powershell

(0) ShareShare
ReportReport
Posted on by 25

Hello together,

I'm developing in MSCRM for a couple months now and I'm facing requirements that involve Data, like Queues and certain structures beeing created in custom Entities.

For us this is a manual Step after each deployment of the Solutions and obviously is error-prone. So my idea was to Export the Records from the Development instance, convert to some Xml/JSON/..., Source Control and deploy them with the normal solution deployment to have a consistent state after the deployment and across the instances. 

So I stumbled accross this Issue of Microsoft.Xrm.Data.Powershell and the solution worked as Long as I'm not saving the record to disk:

--------------------------------------------------------------------------

#This works:
$c = Get-CrmRecord -conn $srcConn $logicalname $id $fields
New-CrmRecord -conn $destConn $c

#But I'm struggling to import $n after saving $c to disk:
Export-Clixml "$($_)/$($logicalname)/$($id).xml" -InputObject $c -Depth 10 $n = Import-Clixml "$($_)/$($logicalname)/$($id).xml" #$j = ConvertTo-Json -InputObject $c -Depth 10 #$n = ConvertFrom-Json -InputObject $j #$s = Convert-String -InputObject $c #$n = ConvertFrom-StringData $s New-CrmRecord -conn $destConn $n

------------------------------------------------------------------

I always get Messages from OrgService, that it cannot deserialize the object:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter schemas.microsoft.com/.../Services:request.
The InnerException message was 'Error in line 1 position 8374. Element 'schemas.datacontract.org/.../System.Collections.Generic:value' contains data from a type that maps to the name 'System.Management.Automation:PSObject'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'PSObject' and namespace 'System.Management.Automation'.'.  Please see InnerException for more details.

I guess my conversion changes the object itself (which is most obvious for Convert-String), but I have no idea how to solve this.
So my question is: Has anybody solved something similar and can help me with this issue?

(I dont care about the Format as long as you are able to hack the Attribute-Values on disk, like switching from true to false.)

Thanks in advance!

Best, 

Marius

*This post is locked for comments

I have the same question (0)
  • Marius Wodtke Profile Picture
    25 on at

    Push

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Have you tried configuration migration tool that is a part of SDK - www.powerobjects.com/.../microsoft-dynamics-365-crm-configuration-migration-tool

    It sounds that it does exactly what you need.

  • Marius Wodtke Profile Picture
    25 on at

    Hey Andrew,

    Thanks for the answer, but sadly this tool has a couple Problems for my application:

    a) It's a GUI tool and therefore very hard to automate,

    b) Does not allow for filtering on Export (as far as I can tell from trying and reading the linked article)

    [c) It packs everything into one file. However that can be solved with Powershell by splitting the file after export.]

    Therefore it seems to me like its more suited for getting "all records of an entity" instead of "this one record of an entity" and for one time importing by hand.

    In contrast the requirement is more like "deploy this one record with the automated daylie deployment".

    Best,

    Marius

  • Verified answer
    Marius Wodtke Profile Picture
    25 on at

    Documenting for someone stumbling across:

        $n = Import-Clixml "$($_)/$($logicalname)/$($id).xml" 
    
        $props = $n.original.PSObject.BaseObject
        foreach ($key in $props.Keys) {
            if ($props[$key].Value.Id.Guid) { #Lookup
                $x = New-Object Microsoft.Xrm.Sdk.EntityReference
                $x.Id = $props[$key].Value.Id 
                $x.LogicalName = $props[$key].Value.LogicalName 
                $x.Name = $props[$key].Value.Key
                $props[$key].Value = $x #Replace PSObject with EntityReference
            }
    
            if ($props[$key].Value.Value -ne $null) { #Optionset Value
                $y = New-Object Microsoft.Xrm.Sdk.OptionSetValue
                $y.Value = $props[$key].Value.Value
                $props[$key].Value = $y #Replace PSObject with OptionsetValue
            }
        }
    
        New-CrmRecord -conn $srcConn $n -PreserveCrmRecordId


    This is basically hacking the Record back to CRM-Types after Powershell Serialization broke it. 

    Not elegant, not bulletproof, but working. 

    You will Need to create similar Code for other complex types, e.g. MultiOptionSet. find out what the type is on the Export ($c in my Case) and reconstruct it after deserialization ($n in my Case). 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans