2016-08-25 59 views
1

有沒有什麼辦法可以爲例如創建一個全新的字段。插件或網絡服務(然後與視圖關聯)?CRM以編程方式創建字段

我已經看過網絡,但找不到任何東西。

回答

1

您將需要執行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); 

此代碼是未經測試,而是從實例鏈接拼湊所以應該有希望的工作。