Home

Friday, March 13, 2020

Deactivate forms in Dynamics 365 using power-shell

Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
#$crmOrg = Get-CrmConnection –InteractiveMode
$password = ConvertTo-SecureString "***" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential("username@domain.com",$password)
$crmOrg = Get-CrmConnection -Credential $credentials -DeploymentRegion NorthAmerica -OnlineType Office365 -OrganizationName "dev-dev" -MaxCrmConnectionTimeOutMinutes 5

Write-Host '------------------------------------------------------------'
Write-Host 'Information: Connection established successfully....'
Write-Information -MessageData "Connection established successfully...." -Tags success
$formNames = @( "Account for interactive experience","Contacts Form"
);

$formsSb = [System.Text.StringBuilder]::new()
for($i = 0; $i -lt $formNames.length; $i++)
{
$formsSb = $formsSb.Append("<condition attribute='name' operator='eq' value='" + $formNames[$i] +"'/>" )
}

$formFetchXml = "<fetch>
<entity name='systemform'>
<attribute name='name' />
<attribute name='isdefault' />
<attribute name='formactivationstate' /> 
<filter type='or'>"+ $formsSb.ToString() +
"</filter>
<order attribute='name' />
</entity>
</fetch>";

$formsToDeactivate = Get-CrmRecordsByFetch -Fetch $formFetchXml -conn $crmOrg
$success = 0
$failure = 0

foreach($formRecord in $formsToDeactivate['CrmRecords'])
{
Set-CrmRecord -EntityLogicalName systemform -Fields @{"formactivationstate"=New-CrmOptionSetValue(0) } -Id $formRecord.formid -conn $crmOrg -PrimaryKeyField formid -ErrorAction SilentlyContinue -ErrorVariable formError
if($formError){
Write-Warning -Message "$($formRecord.name) - Deactivation Failed"
Write-Information -MessageData "$($formRecord.name) ' - Deactivation Failed" -Tags failure
#Write-Error -Message $formError[0].Exception.Message
$failure++
}
else {
Write-Information -MessageData "$($formRecord.name) ' - Deactivated Successfully" -Tags success
Write-Host 'Information: '$formRecord.name ' - Deactivated Successfully'
$success++
}
}

Write-Host '------------------------------------------------------------'
Write-Host 'Information: Total count of forms to be Deactivated - ' $formsToDeactivate.Count
Write-Information -MessageData "Total count of forms to be Deactivated - &($viewsToDeactivate.Count)" -Tags success
Write-Host 'Information: Total count of forms Deactivated Successfully- ' $success
Write-Information -MessageData "Total count of forms Deactivated Successfully - $($success)" -Tags success
Write-Host 'Information: Total count of forms failed to Deactivate - ' $failure
Write-Information -MessageData "Total count of forms failed to Deactivate - $($failure)" -Tags failure
Write-Host '------------------------------------------------------------'

No comments:

Post a Comment

Convert subgrid to Comments