function queryMetadata() {
const client = Hsl.WebApi.getClient('9.0');
client.retrieve({ entityType: 'EntityDefinition', LogicalName: 'account' }, ['LogicalName'], {
Attributes: {
select: null,
},
ManyToManyRelationships: ['SchemaName'],
ManyToOneRelationships: ['SchemaName'],
OneToManyRelationships: ['SchemaName'],
Keys: {
select: null,
},
}).then(resp => {
Hsl.Dialog.alert(`Max length for attributes:
Name: ${getMaxLength('name')}
Telephone1: ${getMaxLength('telephone1')}
Telephone2: ${getMaxLength('telephone2')}
Telephone3: ${getMaxLength('telephone3')}
`);
function getMaxLength(attributeLogicalName) {
const attr = resp.Attributes.filter((x) => x.LogicalName === attributeLogicalName)[0];
if (!attr)
throw new Error(`Unable to find attribute ${attributeLogicalName}`);
return attr.MaxLength;
}
}).catch(Hsl.Dialog.showError);
}
function queryFilteringAttributesRetrieved() {
const client = Hsl.WebApi.getClient('9.0');
client.querySingle('EntityDefinition', {
select: null,
filter: `LogicalName eq 'account'`,
expand: {
Attributes: {
select: ['LogicalName'],
filter: 'IsCustomAttribute eq true',
},
},
}).then(resp => {
const lookupAttrs = resp.Attributes.map((x) => x.LogicalName).join(', ');
if (lookupAttrs.length) {
Hsl.Dialog.alert(`Account has the following custom attributes: ${lookupAttrs}.`);
}
else {
Hsl.Dialog.alert(`Account has no custom attributes.`);
}
}).catch(Hsl.Dialog.showError);
}
function queryLabels() {
const client = Hsl.WebApi.getClient('9.0');
client.query('EntityDefinition', {
filter: `Microsoft.Dynamics.CRM.In(PropertyName='LogicalName', PropertyValues=['account', 'contact'])`,
select: ['LogicalName', 'DisplayName', 'DisplayCollectionName'],
expand: {
Attributes: {
select: ['LogicalName', 'DisplayName'],
}
}
}).then(resp => {
const contactMetadata = resp.value.filter(x => x.LogicalName === 'contact')[0];
const nameMetadata = contactMetadata.Attributes.filter((x) => x.LogicalName === 'fullname')[0];
const nameLabel = nameMetadata.DisplayName.UserLocalizedLabel.Label;
Hsl.Dialog.alert(nameLabel);
}).catch(Hsl.Dialog.showError);
}
function retrieveAllMetadata() {
const client = Hsl.WebApi.getClient('9.0');
const parameters = Hsl.WebApi.parameterBuilder();
parameters.set('RetrieveAsIfPublished', true);
parameters.setEnum('EntityFilters', 'Microsoft.Dynamics.CRM.EntityFilters', 'Entity, Attributes, Relationships');
client.function({
name: 'RetrieveAllEntities',
parameters,
}).then(c => console.log(c), e => console.error(e));
}
function getOptionMetadata(entityName, attrName) {
const client = Hsl.WebApi.getClient('9.0');
// See https://msdn.microsoft.com/en-us/library/mt607522.aspx
return client.querySingle('EntityDefinition', {
filter: `LogicalName eq ${Hsl.WebApi.encodeString(entityName)}`,
select: 'MetadataId',
}).then(entityMetadata => {
const filter = `LogicalName eq ${Hsl.WebApi.encodeString(attrName)}`;
return client.get(`/EntityDefinitions(${entityMetadata.MetadataId})/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata/?$select=LogicalName&$filter=${encodeURIComponent(filter)}&$expand=OptionSet,GlobalOptionSet`);
}).then(resp => {
const optSet = resp.OptionSet || resp.GlobalOptionSet;
return optSet.Options.map((opt) => ({
value: opt.Value,
label: opt.Label.UserLocalizedLabel.Label,
}));
});
}