### Install Prerelease Version of Rnwood.Dataverse.Data.PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/docs/getting-started/installation.md Installs the latest development build (prerelease) of the Rnwood.Dataverse.Data.PowerShell module. Use this option to access the newest features or test upcoming changes. ```powershell Install-Module Rnwood.Dataverse.Data.PowerShell -AllowPrerelease -Scope CurrentUser ``` -------------------------------- ### Install Rnwood.Dataverse.Data.PowerShell Module Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/docs/getting-started/installation.md Installs the Rnwood.Dataverse.Data.PowerShell module for the current user. This command fetches and installs the module from the PowerShell gallery. ```powershell Install-Module Rnwood.Dataverse.Data.PowerShell -Scope CurrentUser ``` -------------------------------- ### Install Specific Version of Rnwood.Dataverse.Data.PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/docs/getting-started/installation.md Installs a specific version of the Rnwood.Dataverse.Data.PowerShell module. This is useful for ensuring script compatibility and controlled deployments. ```powershell Install-Module Rnwood.Dataverse.Data.PowerShell -RequiredVersion 100.0.0 -Scope CurrentUser ``` -------------------------------- ### Get All Members of a Marketing List Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Illustrates retrieving all members of a marketing list. The first example uses a FetchXML query with Microsoft.Xrm.Data.PowerShell, while the second leverages a simpler SQL query via Invoke-DataverseSql in Rnwood.Dataverse.Data.PowerShell. ```powershell $fetch = @" "@ $members = Get-CrmRecordsByFetch -conn $conn -Fetch $fetch ``` ```powershell # Using SQL (much simpler) $members = Invoke-DataverseSql -Connection $conn -Sql @" SELECT c.fullname, c.emailaddress1 FROM contact c INNER JOIN listmember lm ON c.contactid = lm.entityid WHERE lm.listid = '$marketingListId' "@ ``` -------------------------------- ### Get Quick Find Search Fields (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Provides examples for retrieving the search fields configured for Dataverse Quick Find. This helps understand how global search is set up for different entities. It queries the 'savedquery' entity and parses the FetchXML to extract search criteria. ```powershell # Microsoft.Xrm.Data.PowerShell # Get all QuickFind views (querytype of 4) $views = Get-CrmRecords -EntityLogicalName savedquery ` -FilterAttribute querytype -FilterOperator eq -FilterValue 4 ` -Fields name,fetchxml,returnedtypecode $results = @() foreach($view in $views.CrmRecords) { $entityname = $view.returnedtypecode $xml = [xml]$view.fetchxml $filters = $xml.fetch.entity.filter.condition | Where-Object { $_.value -eq "{0}" } $results += [PSCustomObject]@{ Entity = $entityname SearchFieldCount = $filters.Count SearchFields = ($filters.attribute -join ", ") } } $results | Sort-Object Entity | Format-Table ``` ```powershell # Rnwood.Dataverse.Data.PowerShell # Get all QuickFind views using SQL $views = Invoke-DataverseSql -Connection $conn -Sql @" SELECT savedqueryid, name, fetchxml, returnedtypecode FROM savedquery WHERE querytype = 4 ORDER BY returnedtypecode "@ $results = @() foreach($view in $views) { $xml = [xml]$view.fetchxml $filters = $xml.fetch.entity.filter.condition | Where-Object { $_.value -eq "{0}" } $results += [PSCustomObject]@{ Entity = $view.returnedtypecode ViewName = $view.name SearchFieldCount = $filters.Count SearchFields = ($filters.attribute -join ", ") } } $results | Format-Table ``` -------------------------------- ### Execute WhoAmI Request using Cmdlet Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Illustrates retrieving the current user's information using the Get-DataverseWhoAmI cmdlet. This cmdlet provides a concise way to get user, business unit, and organization details. ```powershell $whoami = Get-DataverseWhoAmI -Connection $conn $userId = $whoami.UserId # Also provides: BusinessUnitId, OrganizationId ``` -------------------------------- ### Example: Get All Contacts (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseRecord.md This example demonstrates how to retrieve all contact records from Dataverse and displays all non-system columns. It first establishes a connection to the Dataverse instance. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Get-DataverseRecord -tablename contact ``` -------------------------------- ### Get Organization Settings Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Demonstrates retrieving organization settings. Microsoft.Xrm.Data.PowerShell uses Get-CrmRecord to fetch all fields, while Rnwood.Dataverse.Data.PowerShell offers both a similar record retrieval and a more direct SQL query for specific settings. ```powershell $orgId = (Invoke-CrmWhoAmI).OrganizationId $org = Get-CrmRecord -EntityLogicalName organization -Id $orgId -Fields * ``` ```powershell $whoami = Get-DataverseWhoAmI -Connection $conn $org = Get-DataverseRecord -Connection $conn -TableName organization -Id $whoami.OrganizationId # Or using SQL to get specific settings $settings = Invoke-DataverseSql -Connection $conn -Sql @" SELECT name, isemailenabledfordelegation, maxuploadfilesize, allowlegacyclientexperience FROM organization "@ ``` -------------------------------- ### Get Entity Metadata using SQL Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Demonstrates retrieving entity metadata using SQL queries against the Dataverse metadata view in Rnwood.Dataverse.Data.PowerShell. This approach leverages the Invoke-DataverseSql cmdlet. ```powershell # Using SQL queries against metadata $entityInfo = Invoke-DataverseSql -Connection $conn -Sql @" SELECT name, displayname, primaryidattribute, primarynameattribute FROM metadata.entity WHERE name = 'account' "@ ``` -------------------------------- ### Retrieve Dataverse Solutions Using Wildcard Patterns Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseSolution.md This example demonstrates retrieving solutions whose unique names start with a specific pattern using wildcards. The -UniqueName parameter accepts wildcard characters for flexible searching. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Get-DataverseSolution -UniqueName "Contoso*" UniqueName Name Version IsManaged ---------- ---- ------- --------- ContosoSales Contoso Sales 1.0.0.0 False ContosoMarketing Contoso Marketing 2.1.0.0 False ``` -------------------------------- ### Example: Compare Solution File with Target Environment (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Compare-DataverseSolutionComponents.md Demonstrates how to compare a local solution zip file with the active solution in the target Dataverse environment. It first establishes a connection to the Dataverse environment and then executes the comparison. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> $conn = Get-DataverseConnection -Url "https://yourorg.crm.dynamics.com" -Interactive PS C:\> Compare-DataverseSolutionComponents -SolutionFile "C:\Solutions\MySolution_1_0_0_0.zip" ``` -------------------------------- ### Example: Get Contacts by First Name (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseRecord.md This example retrieves contact records where the 'firstname' starts with 'Rob' and only returns the 'firstname' column. It utilizes the `-FilterValues` parameter with a 'Like' operator. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Get-DataverseRecord -tablename contact -columns firstname -filtervalues @{ "firstname:Like" = "Rob%" } ``` -------------------------------- ### Example: Create Active Quick Create Form (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Set-DataverseForm.md Illustrates creating a new quick create form for the 'account' entity, including a description and marking it as active. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Set-DataverseForm -Entity 'account' -Name 'Account Quick Create' -FormType 'QuickCreate' -Description 'Quick create form for accounts' -IsActive -PassThru ``` -------------------------------- ### Upload Multiple Web Resources from Folder using PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md This example shows how to upload multiple web resources from a specified folder. The 'complex' approach requires iterating through files, manually encoding content, and creating individual entities. The 'simple' method efficiently processes an entire folder with a single command. Dependencies include the PowerShell module and a Dataverse connection. ```powershell # Complex - requires looping and manual handling $files = Get-ChildItem -Path "C:\webresources" -File -Filter "*.js" foreach ($file in $files) { $fileContent = [System.IO.File]::ReadAllBytes($file.FullName) $base64 = [System.Convert]::ToBase64String($fileContent) $webResource = New-Object Microsoft.Xrm.Sdk.Entity("webresource") $webResource.Attributes["name"] = "new_/$($file.Name)" $webResource.Attributes["displayname"] = $file.BaseName $webResource.Attributes["webresourcetype"] = New-Object Microsoft.Xrm.Sdk.OptionSetValue(3) $webResource.Attributes["content"] = $base64 $conn.Create($webResource) } ``` ```powershell # Simple - single command processes entire folder Set-DataverseWebResource -Connection $conn -Folder "C:\webresources" \ -PublisherPrefix "new" -FileFilter "*.js" -Publish ``` -------------------------------- ### Import Dataverse Solution Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Illustrates importing a Dataverse solution from a zip file. It shows how to read the file bytes and use the Invoke-DataverseImportSolution cmdlet with various options. ```powershell $solutionBytes = [System.IO.File]::ReadAllBytes("C:\Solutions\MySolution.zip") # Using specialized cmdlet (simplest) Invoke-DataverseImportSolution -Connection $conn ` -CustomizationFile $solutionBytes ` -PublishWorkflows $true ` -OverwriteUnmanagedCustomizations $false ``` -------------------------------- ### Update Rnwood.Dataverse.Data.PowerShell Module Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/docs/getting-started/installation.md Updates the Rnwood.Dataverse.Data.PowerShell module to the latest available version. The -Force parameter ensures that the update is applied even if the module is already installed. ```powershell Update-Module Rnwood.Dataverse.Data.PowerShell -Force ``` -------------------------------- ### Example: Reverse Comparison (Environment to File) (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Compare-DataverseSolutionComponents.md Shows how to perform a reverse comparison, where the state of the solution in the Dataverse environment is compared against a local solution file. This helps identify what's present in the environment but not in the file. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> $conn = Get-DataverseConnection -Url "https://yourorg.crm.dynamics.com" -Interactive PS C:\> Compare-DataverseSolutionComponents -SolutionFile "C:\Solutions\MySolution.zip" -ReverseComparison ``` -------------------------------- ### Example: Compare Two Solution Files (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Compare-DataverseSolutionComponents.md Illustrates comparing two different solution zip files to identify changes between versions. This is useful for tracking modifications in solution development. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Compare-DataverseSolutionComponents -SolutionFile "C:\Solutions\MySolution_v1.zip" -TargetSolutionFile "C:\Solutions\MySolution_v2.zip" ``` -------------------------------- ### Get All Sections from a Dataverse Form Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseFormSection.md This example demonstrates how to retrieve all sections from a specified Dataverse form. It first establishes a connection, then gets the form object, and finally calls Get-DataverseFormSection with the FormId. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> $form = Get-DataverseForm -Entity 'contact' -Name 'Information' PS C:\> Get-DataverseFormSection -FormId $form.FormId ``` -------------------------------- ### List All Dataverse Solutions Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Provides examples for listing all Dataverse solutions, including their unique name, friendly name, and version. It demonstrates using Get-DataverseRecord and Invoke-DataverseSql. ```powershell # Using Get-DataverseRecord $solutions = Get-DataverseRecord -Connection $conn -TableName solution -Columns uniquename,friendlyname,version $solutions | Select-Object uniquename,friendlyname,version # Or using SQL Invoke-DataverseSql -Connection $conn -Sql @" SELECT uniquename, friendlyname, version FROM solution WHERE ismanaged = 0 ORDER BY friendlyname "@ ``` -------------------------------- ### Set PowerShell Execution Policy Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/docs/getting-started/installation.md Configures the PowerShell execution policy for the current user to allow loading unsigned scripts from remote sources. This is a prerequisite for installing modules from the PowerShell gallery. ```powershell Set-ExecutionPolicy –ExecutionPolicy RemoteSigned –Scope CurrentUser ``` -------------------------------- ### Retrieve All Dataverse Solutions Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseSolution.md This example demonstrates how to retrieve all solutions present in the Dataverse environment. It first establishes a connection and then calls the Get-DataverseSolution cmdlet without any parameters. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Get-DataverseSolution UniqueName Name Version IsManaged PublisherName ---------- ---- ------- --------- ------------- MySolution My Solution 1.0.0.0 False Contoso Active Active Solution 1.0.0.0 True Microsoft Default Default Solution 1.0.0.0 True Microsoft ``` -------------------------------- ### Get Personal Views for Current User in Dataverse PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Fetches personal views for the current user. The Microsoft module requires getting the user ID first and then filtering 'userquery' records. Rnwood.Dataverse.Data.PowerShell simplifies this using Get-DataverseWhoAmI and Invoke-DataverseSql for direct querying. ```powershell $userId = (Invoke-CrmWhoAmI).UserId $personalViews = Get-CrmRecords -EntityLogicalName userquery \ -FilterAttribute ownerid -FilterOperator eq -FilterValue $userId \ -Fields name,returnedtypecode ``` ```powershell $whoami = Get-DataverseWhoAmI -Connection $conn $userId = $whoami.UserId # Using SQL $personalViews = Invoke-DataverseSql -Connection $conn -Sql @" SELECT name, returnedtypecode, fetchxml FROM userquery WHERE ownerid = '$userId' ORDER BY returnedtypecode, name "@ $personalViews | Format-Table ``` -------------------------------- ### Connect to Dataverse with Client Certificate (File) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseConnection.md Establishes a connection to a Dataverse environment using client certificate authentication with a certificate file. This example uses an unencrypted PFX file, meaning no password is required. Ensure the certificate file is accessible and properly configured. ```powershell PS C:\> $c = Get-DataverseConnection -Url https://myorg.crm11.dynamics.com -ClientId "12345678-1234-1234-1234-123456789abc" -CertificatePath "./mycert.pfx" ``` -------------------------------- ### Download Multiple Web Resources using PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md This snippet demonstrates downloading multiple web resources. The 'complex' method involves querying for resources by type, looping through results, decoding content, and saving to files. The 'simple' method automates this, allowing filtering by type or name. A Dataverse connection is required. ```powershell # Complex - requires querying, looping, and manual file handling $query = New-Object Microsoft.Xrm.Sdk.Query.QueryExpression("webresource") $query.ColumnSet = New-Object Microsoft.Xrm.Sdk.Query.ColumnSet("name", "content") $query.Criteria.AddCondition("webresourcetype", [Microsoft.Xrm.Sdk.Query.ConditionOperator]::Equal, 3) $results = $conn.RetrieveMultiple($query) foreach ($webResource in $results.Entities) { $name = $webResource.Attributes["name"].Replace("/", "_") $content = [System.Convert]::FromBase64String($webResource.Attributes["content"]) [System.IO.File]::WriteAllBytes("C:\download$name", $content) } ``` ```powershell # Simple - automatically saves all matching resources Get-DataverseWebResource -Connection $conn -WebResourceType 3 -Folder "C:\download" # Or with wildcards for name filtering Get-DataverseWebResource -Connection $conn -Name "new_*" -Folder "C:\download" ``` -------------------------------- ### GitHub Actions Environment Deployment Example Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/docs/use-cases/ci-cd-pipelines.md An example illustrating how to configure GitHub Actions jobs to deploy to different environments (development and production). It shows how to specify the environment for secrets and set up dependencies between jobs. ```yaml # Deploy to different environments jobs: deploy-dev: runs-on: ubuntu-latest environment: development steps: # ... uses secrets.DATAVERSE_URL from development environment deploy-prod: runs-on: ubuntu-latest environment: production needs: deploy-dev # Runs after dev deployment steps: # ... uses secrets.DATAVERSE_URL from production environment ``` -------------------------------- ### Get Entity Metadata using SDK Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Retrieves all entity metadata from Dataverse and filters it to find the metadata for the 'account' entity. This uses the Get-CrmEntityAllMetadata function. ```powershell $metadata = Get-CrmEntityAllMetadata -conn $conn -EntityFilters Entity $accountMetadata = $metadata | Where-Object {$_.LogicalName -eq "account"} ``` -------------------------------- ### Download a Web Resource using PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Illustrates how to download a web resource from Dataverse. The 'complex' method involves querying for the web resource by name, decoding its base64 content, and writing it to a file. The 'simple' method automates this process with a single cmdlet. A Dataverse connection is necessary for both. ```powershell # Complex - requires manual base64 decoding and file writing $query = New-Object Microsoft.Xrm.Sdk.Query.QueryExpression("webresource") $query.ColumnSet = New-Object Microsoft.Xrm.Sdk.Query.ColumnSet("name", "content") $query.Criteria.AddCondition("name", [Microsoft.Xrm.Sdk.Query.ConditionOperator]::Equal, "new_/script.js") $result = $conn.RetrieveMultiple($query) if ($result.Entities.Count -gt 0) { $webResource = $result.Entities[0] $content = [System.Convert]::FromBase64String($webResource.Attributes["content"]) [System.IO.File]::WriteAllBytes("C:\downloaded-script.js", $content) } ``` ```powershell # Simple - automatically handles base64 decoding and file writing Get-DataverseWebResource -Connection $conn -Name "new_script" -Path "C:\downloaded-script.js" ``` -------------------------------- ### Get a Specific Section by Name from Dataverse Form Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseFormSection.md This example retrieves a single, specific section from a Dataverse form by its name. It requires the FormId and the SectionName. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Get-DataverseFormSection -FormId $formId -SectionName 'ContactDetails' ``` -------------------------------- ### Get Sections from a Specific Tab in Dataverse Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseFormSection.md This example shows how to retrieve all sections within a particular tab of a Dataverse form. It requires the FormId and the TabName as parameters. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> $formId = '12345678-1234-1234-1234-123456789012' PS C:\> Get-DataverseFormSection -FormId $formId -TabName 'General' ``` -------------------------------- ### Import Sitemap from File with PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Demonstrates importing a sitemap from an XML file into Dataverse using Microsoft.Xrm.Data.PowerShell's entity creation or Rnwood.Dataverse.Data.PowerShell's Set-DataverseSitemap or Set-DataverseRecord cmdlets. ```powershell $sitemapXml = Get-Content -Path "MySitemap.xml" -Raw $entity = New-Object Microsoft.Xrm.Sdk.Entity("sitemap") $entity.Attributes["sitemapname"] = "ImportedSitemap" $entity.Attributes["sitemapxml"] = $sitemapXml $sitemapId = $conn.Create($entity) ``` ```powershell $sitemapXml = Get-Content -Path "MySitemap.xml" -Raw # Using specialized cmdlet (simplest) $sitemapId = Set-DataverseSitemap -Connection $conn \ -Name "ImportedSitemap" \ -SitemapXml $sitemapXml \ -PassThru # Or using generic cmdlet $sitemapId = Set-DataverseRecord -Connection $conn -TableName sitemap -Fields @{ sitemapname = "ImportedSitemap" sitemapxml = $sitemapXml } ``` -------------------------------- ### Get Specific Section from Specific Tab in Dataverse Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseFormSection.md This example retrieves a named section from a named tab within a Dataverse form. It requires the FormId, TabName, and SectionName. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Get-DataverseFormSection -FormId $formId -TabName 'Details' -SectionName 'AdditionalInfo' ``` -------------------------------- ### Get All Users in a Dataverse Team Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Illustrates retrieving all users within a specific Dataverse team. It shows how to achieve this using FetchXML with Get-DataverseRecord and a more straightforward SQL query with Invoke-DataverseSql. ```powershell $teamId = "guid-here" # Using FetchXML $fetchXml = @" "@ $members = Get-DataverseRecord -Connection $conn -FetchXml $fetchXml # Or using SQL (simpler) $members = Invoke-DataverseSql -Connection $conn -Sql @" SELECT u.fullname, u.internalemailaddress FROM systemuser u INNER JOIN teammembership tm ON u.systemuserid = tm.systemuserid WHERE tm.teamid = '$teamId' "@ ``` -------------------------------- ### Create a New Sitemap with PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Illustrates creating a new Dataverse sitemap by providing XML content, using Microsoft.Xrm.Data.PowerShell's Entity object or Rnwood.Dataverse.Data.PowerShell's Set-DataverseSitemap or Set-DataverseRecord cmdlets. ```powershell $sitemapXml = @" "@ $entity = New-Object Microsoft.Xrm.Sdk.Entity("sitemap") $entity.Attributes["sitemapname"] = "MySitemap" $entity.Attributes["sitemapxml"] = $sitemapXml $sitemapId = $conn.Create($entity) ``` ```powershell $sitemapXml = @" "@ # Using specialized cmdlet (simplest) $sitemapId = Set-DataverseSitemap -Connection $conn -Name "MySitemap" -SitemapXml $sitemapXml -PassThru # Or using generic cmdlet $sitemapId = Set-DataverseRecord -Connection $conn -TableName sitemap -Fields @{ sitemapname = "MySitemap" sitemapxml = $sitemapXml } ``` -------------------------------- ### Get All Business Process Flow Stages Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Retrieves all business process flow stages. Microsoft.Xrm.Data.PowerShell uses Get-CrmRecords with a '*' field selection, while rnwood.dataverse.data.powershell offers a simpler Get-DataverseRecord or a more powerful Invoke-DataverseSql option. ```powershell # Retrieve all stages $stages = Get-CrmRecords -conn $conn -EntityLogicalName processstage -Fields * -AllRows # Display with processed information $stages.CrmRecords | Select-Object primaryentitytypecode, ` @{name='processname'; expression={$_.processid}}, @{name='processid';expression={$_.processid_Property.Value.Id}}, ` processstageid, stagecategory, stagename | Sort-Object primaryentitytypecode ``` ```powershell # Using Get-DataverseRecord (simpler) $stages = Get-DataverseRecord -Connection $conn -TableName processstage ` -Columns primaryentitytypecode,processid,processstageid,stagecategory,stagename # Or using SQL for better querying $stages = Invoke-DataverseSql -Connection $conn -Sql @" SELECT ps.primaryentitytypecode, w.name as processname, ps.processid, ps.processstageid, ps.stagecategory, ps.stagename FROM processstage ps INNER JOIN workflow w ON ps.processid = w.workflowid ORDER BY ps.primaryentitytypecode, ps.stagename "@ $stages | Format-Table ``` -------------------------------- ### Example: Create Form with Custom FormXml (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Set-DataverseForm.md Demonstrates creating a new form using the content from a local FormXml file and publishing it immediately. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> $formXml = Get-Content -Path 'CustomForm.xml' -Raw PS C:\> Set-DataverseForm -Entity 'contact' -Name 'Advanced Form' -FormType 'Main' -FormXmlContent $formXml -Publish ``` -------------------------------- ### Get a Specific Form with FormXml Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Retrieves a specific system form by its ID, including its FormXml. Microsoft.Xrm.Data.PowerShell uses Get-CrmRecord with specific fields, while rnwood.dataverse.data.powershell provides a dedicated Get-DataverseForm cmdlet with an option to include FormXml. ```powershell # Complex SDK query required $formId = 'a1234567-89ab-cdef-0123-456789abcdef' $form = Get-CrmRecord -conn $conn -EntityLogicalName systemform -Id $formId -Fields formxml,name ``` ```powershell # Simple and clean with dedicated cmdlet $formId = 'a1234567-89ab-cdef-0123-456789abcdef' $form = Get-DataverseForm -Connection $conn -Id $formId -IncludeFormXml ``` -------------------------------- ### Use WhatIf to preview changes - PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Set-DataverseOrganizationSettings.md This example shows how to use the -WhatIf parameter to preview the effects of updating OrgDbOrgSettings without actually making any changes. It's useful for verifying the intended modifications before committing them. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> $updates = [PSCustomObject]@{ MaxUploadFileSize = 10485760 } PS C:\> Set-DataverseOrganizationSettings -InputObject $updates -OrgDbOrgSettings -WhatIf What if: Performing the operation "Update organization settings" on target "OrgDbOrgSettings in organization record ..." ``` -------------------------------- ### Query Sitemap using Get-DataverseSitemap and Invoke-DataverseSql Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Demonstrates fetching sitemap data using the specialized Get-DataverseSitemap cmdlet with an application name filter and alternatively using Invoke-DataverseSql with a SQL query. ```powershell $sitemap = Get-DataverseSitemap -Connection $conn -AppUniqueName "myapp" $sitemap = Invoke-DataverseSql -Connection $conn -Sql @" SELECT s.sitemapid, s.sitemapname, s.sitemapxml, a.uniquename as appuniquename FROM sitemap s INNER JOIN appmodule a ON s.sitemapid = a.appmoduleid WHERE a.uniquename = 'myapp' "@ ``` -------------------------------- ### Delete a Single Dataverse Record by ID Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Remove-DataverseRecord.md This example shows how to delete a specific Dataverse record using its unique GUID. It requires establishing a connection and then calling Remove-DataverseRecord with the table name and the record's ID. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> remove-dataverserecord -tablename contact -id 4CE66D51-C605-4429-8565-8C7AFA4B9550 ``` -------------------------------- ### Delete a Solution using SDK Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Illustrates deleting a solution by its unique name using Microsoft.Xrm.Data.PowerShell. It first retrieves the solution's ID and then uses the $conn.Delete method. ```powershell $solutionUniqueName = 'mysolution' $solutions = Get-CrmRecords -EntityLogicalName solution -FilterAttribute uniquename -FilterOperator eq -FilterValue $solutionUniqueName if ($solutions.Count -eq 1) { $solutionId = $solutions.CrmRecords[0].ReturnProperty_Id $conn.Delete("solution", $solutionId) } ``` -------------------------------- ### Remove Dataverse Web Resource by ID (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Remove-DataverseWebResource.md This example shows how to delete a Dataverse web resource by providing its unique GUID. It involves connecting to the Dataverse environment and then using the -Id parameter with the Remove-DataverseWebResource cmdlet. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> Remove-DataverseWebResource -Id "12345678-1234-1234-1234-123456789abc" ``` -------------------------------- ### Get All Forms for an Entity Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Retrieves all system forms for a specified entity. Microsoft.Xrm.Data.PowerShell requires a direct SDK query using QueryExpression, while rnwood.dataverse.data.powershell offers a dedicated and simpler Get-DataverseForm cmdlet, with options to filter by form type. ```powershell # Not directly supported - required direct SDK queries $query = New-Object Microsoft.Xrm.Sdk.Query.QueryExpression -ArgumentList "systemform" $query.ColumnSet = New-Object Microsoft.Xrm.Sdk.Query.ColumnSet($true) $query.Criteria.AddCondition("objecttypecode", [Microsoft.Xrm.Sdk.Query.ConditionOperator]::Equal, "contact") $forms = Get-CrmRecords -conn $conn -Query $query ``` ```powershell # Using dedicated form cmdlet (simplest and recommended) $forms = Get-DataverseForm -Connection $conn -Entity 'contact' # Display form information $forms | Format-Table FormId, Name, Type, IsActive, IsDefault # Get only Main forms $mainForms = Get-DataverseForm -Connection $conn -Entity 'contact' -FormType 'Main' ``` -------------------------------- ### Get Active Stages for a Specific Entity Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Fetches active business process flow stages for a specific entity. Microsoft.Xrm.Data.PowerShell uses Get-CrmRecordsByFetch with a FetchXML query, while rnwood.dataverse.data.powershell provides a more concise SQL query using Invoke-DataverseSql. ```powershell $fetch = @" "@ $leadStages = Get-CrmRecordsByFetch -conn $conn -Fetch $fetch ``` ```powershell # Using SQL (much simpler) $leadStages = Invoke-DataverseSql -Connection $conn -Sql @" SELECT stagename, stagecategory, processid FROM processstage WHERE primaryentitytypecode = 'lead' ORDER BY stagename "@ ``` -------------------------------- ### Count Records for All Entities Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Retrieves and counts records for all entities in Dataverse. It queries metadata to get entity names and then iterates to count records, outputting the top 10 by count. Includes error handling for unqueryable entities. ```powershell $conn = Get-DataverseConnection -Url "https://yourorg.crm.dynamics.com" -Interactive # Get all entities using SQL query $tables = Invoke-DataverseSql -Connection $conn -Sql @" SELECT name FROM metadata.entity WHERE isprivate = 0 "@ $results = @() foreach ($table in $tables) { $tableName = $table.name try { $count = Get-DataverseRecord -Connection $conn -TableName $tableName -RecordCount $results += [PSCustomObject]@{ LogicalName = $tableName RecordsCount = $count } } catch { Write-Warning "Could not query $tableName : $_" } } # Top 10 by count $results | Sort-Object RecordsCount -Descending | Select-Object -First 10 ``` -------------------------------- ### Execute WhoAmI Request using SDK Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md Shows how to execute the WhoAmIRequest SDK message to retrieve information about the currently logged-in user and organization. The response contains UserId, BusinessUnitId, and OrganizationId. ```powershell $whoamiRequest = New-Object Microsoft.Crm.Sdk.Messages.WhoAmIRequest $response = $conn.Execute($whoamiRequest) $userId = $response.UserId ``` -------------------------------- ### Upload a Web Resource using PowerShell Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Examples-Comparison.md This snippet demonstrates uploading a web resource (e.g., a JavaScript file) to Dataverse. The 'complex' method involves manual base64 encoding and entity creation, while the 'simple' method uses a dedicated cmdlet for automation. Dependencies include the respective PowerShell modules and a Dataverse connection object. ```powershell # Complex - requires building Entity objects and manual base64 encoding $fileContent = [System.IO.File]::ReadAllBytes("C:\script.js") $base64 = [System.Convert]::ToBase64String($fileContent) $webResource = New-Object Microsoft.Xrm.Sdk.Entity("webresource") $webResource.Attributes["name"] = "new_/script.js" $webResource.Attributes["displayname"] = "My Script" $webResource.Attributes["webresourcetype"] = New-Object Microsoft.Xrm.Sdk.OptionSetValue(3) # JavaScript $webResource.Attributes["content"] = $base64 $webResourceId = $conn.Create($webResource) ``` ```powershell # Simple - automatically handles file reading and base64 encoding Set-DataverseWebResource -Connection $conn -Name "new_script" -Path "C:\script.js" \ -DisplayName "My Script" -PublisherPrefix "new" ``` -------------------------------- ### Search Icons by Name Pattern with Get-DataverseIconSetIcon Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseIconSetIcon.md This example illustrates filtering icons based on a name pattern using the -Name parameter with wildcards. It can be used to find icons that start with, end with, or contain specific substrings. ```powershell Get-DataverseIconSetIcon -Name "user*" Get-DataverseIconSetIcon -Name "*setting*" ``` -------------------------------- ### Join with Filter on Linked Entity (PowerShell) Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/docs/core-concepts/querying.md Filters contacts based on criteria applied to their linked parent account. This example retrieves contacts only from accounts whose names start with 'Contoso' and are active (statecode 0). ```powershell Get-DataverseRecord -Connection $c -TableName contact -Links @{ 'contact.accountid' = 'account.accountid' filter = @{ name = @{ operator = 'Like'; value = 'Contoso%' } statecode = @{ operator = 'Equal'; value = 0 } } } ``` -------------------------------- ### Check Dataverse Solution Version Before Upgrade Source: https://github.com/rnwood/rnwood.dataverse.data.powershell/blob/main/Rnwood.Dataverse.Data.PowerShell/docs/Get-DataverseSolution.md This example demonstrates how to retrieve a specific solution and then programmatically check its version against a desired version to determine if an upgrade is needed. ```powershell PS C:\> Get-DataverseConnection -Url https://myorg.crm.dynamics.com -Interactive -SetAsDefault PS C:\> $solution = Get-DataverseSolution -UniqueName "MySolution" PS C:\> if ($solution.Version -lt [Version]"2.0.0.0") { >> Write-Host "Solution needs upgrade from $($solution.Version) to 2.0.0.0" >> } ```