Public/Metadata.ps1

#GetAllAttributesForEntity
function Get-CrmEntityAttributes{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1)]
        [string]$EntityLogicalName
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest
        $request.LogicalName = $EntityLogicalName
        $request.EntityFilters = [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Attributes
        $request.RetrieveAsIfPublished = $true
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse]$conn.Execute($request)
        return $response.EntityMetadata.Attributes
    }
    catch
    {
        throw
    }
}

#GetAllEntityMetadata
function Get-CrmEntityAllMetadata{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$false, Position=1)]
        [bool]$OnlyPublished=$true,
        [parameter(Mandatory=$false, Position=2)]
        [string]$EntityFilters
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $filter = Resolve-CrmEntityFilters -EntityFilters $EntityFilters
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveAllEntitiesRequest
        $request.EntityFilters = $filter
        $request.RetrieveAsIfPublished = (-not $OnlyPublished)
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveAllEntitiesResponse]$conn.Execute($request)
        return $response.EntityMetadata
    }
    catch
    {
        throw
    }
}

#GetEntityAttributeMetadataForAttribute
function Get-CrmEntityAttributeMetadata{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1)]
        [string]$EntityLogicalName,
        [parameter(Mandatory=$true, Position=2)]
        [string]$FieldLogicalName
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveAttributeRequest
        $request.EntityLogicalName = $EntityLogicalName
        $request.LogicalName = $FieldLogicalName
        $request.RetrieveAsIfPublished = $true
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveAttributeResponse]$conn.Execute($request)
        return $response.AttributeMetadata
    }
    catch
    {
        throw
    }
}

#GetEntityDisplayName
function Get-CrmEntityDisplayName{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1, ParameterSetName="EntityLogicalName")]
        [string]$EntityLogicalName
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest
        $request.LogicalName = $EntityLogicalName
        $request.EntityFilters = [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Entity
        $request.RetrieveAsIfPublished = $true
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse]$conn.Execute($request)
        return Get-CrmLocalizedLabelText -Label $response.EntityMetadata.DisplayName
    }
    catch
    {
        throw
    }
}

#GetEntityDisplayNamePlural
function Get-CrmEntityDisplayPluralName{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1)]
        [string]$EntityLogicalName
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest
        $request.LogicalName = $EntityLogicalName
        $request.EntityFilters = [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Entity
        $request.RetrieveAsIfPublished = $true
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse]$conn.Execute($request)
        return Get-CrmLocalizedLabelText -Label $response.EntityMetadata.DisplayCollectionName
    }
    catch
    {
        throw
    }
}

#GetEntityMetadata
function Get-CrmEntityMetadata{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1)]
        [string]$EntityLogicalName,
        [parameter(Mandatory=$false, Position=2)]
        [string]$EntityFilters
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $filter = Resolve-CrmEntityFilters -EntityFilters $EntityFilters
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest
        $request.LogicalName = $EntityLogicalName
        $request.EntityFilters = $filter
        $request.RetrieveAsIfPublished = $true
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse]$conn.Execute($request)
        return $response.EntityMetadata
    }
    catch
    {
        throw
    }
}

#GetEntityName
function Get-CrmEntityName{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1)]
        [int]$EntityTypeCode
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveAllEntitiesRequest
        $request.EntityFilters = [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Entity
        $request.RetrieveAsIfPublished = $true
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveAllEntitiesResponse]$conn.Execute($request)
        $entityMetadata = $response.EntityMetadata | Where-Object { $_.ObjectTypeCode -eq $EntityTypeCode } | Select-Object -First 1
        if($null -eq $entityMetadata)
        {
            return $null
        }
        return $entityMetadata.LogicalName
    }
    catch
    {
        throw
    }
}

#GetEntityTypeCode
function Get-CrmEntityTypeCode{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1)]
        [string]$EntityLogicalName
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest
        $request.LogicalName = $EntityLogicalName
        $request.EntityFilters = [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Entity
        $request.RetrieveAsIfPublished = $true
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse]$conn.Execute($request)
        return $response.EntityMetadata.ObjectTypeCode
    }
    catch
    {
        throw
    }
}

#GetGlobalOptionSetMetadata
function Get-CrmGlobalOptionSet{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1)]
        [string]$OptionSetName
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveOptionSetRequest
        $request.Name = $OptionSetName
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveOptionSetResponse]$conn.Execute($request)
        return $response.OptionSetMetadata
    }
    catch
    {
        throw
    }
}

#GetPickListElementFromMetadataEntity
function Get-CrmEntityOptionSet{
# .ExternalHelp Campgemini.Xrm.Data.PowerShell.Help.xml
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,
        [parameter(Mandatory=$true, Position=1)]
        [string]$EntityLogicalName,
        [parameter(Mandatory=$true, Position=2)]
        [string]$FieldLogicalName
    )
    $conn = VerifyCrmConnectionParam -conn $conn -pipelineValue ($PSBoundParameters.ContainsKey('conn'))
    try
    {
        $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveAttributeRequest
        $request.EntityLogicalName = $EntityLogicalName
        $request.LogicalName = $FieldLogicalName
        $request.RetrieveAsIfPublished = $true
        $response = [Microsoft.Xrm.Sdk.Messages.RetrieveAttributeResponse]$conn.Execute($request)
        $attributeMetadata = $response.AttributeMetadata
        if($attributeMetadata -is [Microsoft.Xrm.Sdk.Metadata.EnumAttributeMetadata])
        {
            return $attributeMetadata.OptionSet.Options
        }
        if($attributeMetadata -is [Microsoft.Xrm.Sdk.Metadata.BooleanAttributeMetadata])
        {
            return @($attributeMetadata.OptionSet.TrueOption, $attributeMetadata.OptionSet.FalseOption)
        }
        throw "Attribute '$FieldLogicalName' on entity '$EntityLogicalName' is not an option set or boolean attribute."
    }
    catch
    {
        throw
    }
}

function Resolve-CrmEntityFilters {
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [AllowNull()]
        [AllowEmptyString()]
        [string]$EntityFilters
    )
    if([string]::IsNullOrWhiteSpace($EntityFilters))
    {
        return [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Default
    }
    switch($EntityFilters.ToLower())
    {
        "all" { return [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::All }
        "attributes" { return [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Attributes }
        "entity" { return [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Entity }
        "privileges" { return [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Privileges }
        "relationships" { return [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Relationships }
        default { return [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Default }
    }
}

function Get-CrmPrimaryIdAttribute {
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$true)]
        [Microsoft.Xrm.Sdk.IOrganizationService]$conn,

        [parameter(Mandatory=$true)]
        [string]$EntityLogicalName
    )

    $request = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest
    $request.LogicalName = $EntityLogicalName
    $request.EntityFilters = [Microsoft.Xrm.Sdk.Metadata.EntityFilters]::Entity
    $request.RetrieveAsIfPublished = $true
    $response = [Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse]$conn.Execute($request)
    return $response.EntityMetadata.PrimaryIdAttribute
}

function Get-CrmLocalizedLabelText {
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [AllowNull()]
        $Label
    )
    if($null -eq $Label)
    {
        return $null
    }
    if($null -ne $Label.UserLocalizedLabel)
    {
        return $Label.UserLocalizedLabel.Label
    }
    if($null -ne $Label.LocalizedLabels -and $Label.LocalizedLabels.Count -gt 0)
    {
        return $Label.LocalizedLabels[0].Label
    }
    return $null
}