CRM 以编程方式创建字段

CRM Create field programmatically

有什么方法可以为实体创建一个全新的字段,例如插件或网络服务(然后与视图关联)?

我已经通过互联网查看了,但找不到任何东西。

使用 CreateAttributeRequest 创建具有所需元数据的新属性。

以编程方式将其添加到视图并不简单。您需要编辑 Layout XML 元素并添加新创建的属性。

您将需要执行 CreateAttributeRequest 来更改 CRM 元数据。

StringAttributeMetadata stringAttribute = new StringAttributeMetadata
{
    // Set base properties
    SchemaName = "new_string",
    DisplayName = new Label("Sample String", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("String Attribute", _languageCode),
    // Set extended properties
    MaxLength = 100
};

CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
{
    EntityName = "contact",
    Attribute = stringAttribute 
};

serviceProxy.Execute(createAttributeRequest);

然后您将需要Customize the Entity View。这些在 CRM 中存储为记录,并用 XML 表示。这是一个创建示例,但您也可以进行更新。

string layoutXml = @"<grid name='resultset' object='2' jump='name' select='1' preview='1' icon='1'>
    <row name='result' id='contactid'>
        <cell name='name' width='150' /> 
        <cell name='new_string' width='150' />
    </row>
</grid>";

string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='contact'>
        <order attribute='new_string' descending='false' />
        <attribute name='new_string' />
        <attribute name='contactid' /> 
    </entity>
</fetch>";

SavedQuery sq = new SavedQuery
{
    Name = "A New Custom Public View",
    Description = "A Saved Query created in code",
    ReturnedTypeCode = "contact",
    FetchXml = fetchXml,
    LayoutXml = layoutXml,
    QueryType = 0
};

serviceProxy.Create(sq);

最后需要 Publish Customizations 以便用户可以使用更改。

PublishAllXmlRequest publishRequest = new PublishAllXmlRequest();
serviceProxy.Execute(publishRequest);

此代码未经测试,但从示例链接拼凑而成,因此应该可以正常工作。