Home

Showing posts with label Dynamics Powershell scripts. Show all posts
Showing posts with label Dynamics Powershell scripts. Show all posts

Friday, March 13, 2020

Deactivate views in dynamics 365 using power-shell scripts

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
$viewNames = @( "Accounts being Followed",
"Accounts I Follow",
"Won Project Service Opportunities"
);

$viewSb = [System.Text.StringBuilder]::new()

for($i = 0; $i -lt $viewNames.length; $i++)
{
 $viewSb = $viewSb.Append("<condition attribute='name' operator='eq' value='" + $viewNames[$i] +"'/>" )
}

$viewFetchXml = "<fetch>
     <entity name='savedquery'>
                       <attribute name='statecode' />
                        <attribute name='name' />
                        <attribute name='returnedtypecode' />
                        <attribute name='statuscode' />
                        <attribute name='isdefault' />
                        <filter type='or'>"+ $viewSb.ToString() +
      "</filter>       
       <order attribute='name' />
                    </entity>
                </fetch>";
      
$viewsToDeactivate = Get-CrmRecordsByFetch -Fetch $viewFetchXml -conn $crmOrg

$success = 0
$failure = 0

foreach($viewRecord in $viewsToDeactivate['CrmRecords'])
{
   Set-CrmRecordState -CrmRecord $viewRecord -StateCode 1 -StatusCode 2 -conn $crmOrg -ErrorAction SilentlyContinue -ErrorVariable viewError
   if($viewError){
   Write-Warning -Message "$($viewRecord.name) - Deactivation Failed"
   Write-Information -MessageData "$($viewRecord.name) ' - Deactivation Failed" -Tags success
   #Write-Error -Message $viewError[0].Exception.Message
   $failure++
   }
   else {
   Write-Information -MessageData "$($viewRecord.name) ' - Deactivated Successfully" -Tags failure
   Write-Host 'Information: '$viewRecord.name ' - Deactivated Successfully'
   $success++
   }
}

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

Autoumber attribute in Dynamics 365 using power-shell scripts

# This PS snippet is to create or update autonumber attribute.
# The User  Name, Password and the Organization Name are to be updated before executing this.

Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
$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

function CreateUpdateAutonumberAttribute {
    param ( 
        [Parameter(Position=0, Mandatory=$true)][string]$AttributeName,
        [Parameter(Position=1, Mandatory=$true)][string]$AttributeDisplayname,
        [Parameter(Position=2, Mandatory=$true)][string]$EntitySchemaname,
        [Parameter(Position=3, Mandatory=$true)][string]$AutoNumberFormat,
[Parameter(Position=4, Mandatory=$true)][string]$Message
    )

 try{
  Write-Output "Processing autonumber attribute $AttributeName ..."
  $autonumattribute = New-Object Microsoft.Xrm.Sdk.Metadata.StringAttributeMetadata
  $autonumattribute.SchemaName = $AttributeName
  $autonumattribute.LogicalName = $AttributeName
  $autonumattribute.DisplayName = New-Object Microsoft.Xrm.Sdk.Label($attributedisplayname,1033)
  $autonumattribute.Format = [Microsoft.Xrm.Sdk.Metadata.StringFormat]::Text
  $autonumattribute.MaxLength = 100
  $autonumattribute.AutoNumberFormat = $AutoNumberFormat 

  if($message -eq 'create'){
  $request = New-Object Microsoft.Xrm.Sdk.Messages.CreateAttributeRequest
  }
  elseif($message  -eq 'update'){
  $request = New-Object Microsoft.Xrm.Sdk.Messages.UpdateAttributeRequest
  }
  
  $request.Attribute = $autonumattribute
  $request.EntityName = $EntitySchemaname
  $response = $crmOrg.Execute($request)   
  Write-Output "... autonumber attribute $AttributeName Processed."  

  return $response
 }
 catch { 
   Write-Warning -Message $_.Exception.Message
 }
}

CreateUpdateAutonumberAttribute -AttributeName 'accountnumber' -AttributeDisplayname 'Account Number' -EntitySchemaname 'account' -AutoNumberFormat '{DATETIMEUTC:yyyyMMddhhmmss}' -Message 'update'

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 '------------------------------------------------------------'

Convert subgrid to Comments