Dynamic CRM 2013學習筆記(三)快速創建實體 EntityCreater


 

一、實體簡介

實體用於在 Microsoft Dynamics CRM 中建立業務數據模型和管理業務數據。例如,可以使用客戶、市場活動和事件(案例)等實體跟蹤和支持銷售、市場營銷和服務活動。實體具有一組屬性,每個屬性表示一個特定類型的數據項。例如,客戶實體具有 Name、Address 和 OwnerId 屬性。從概念上講,實體類似於數據庫表,實體屬性對應於表列。在 Microsoft Dynamics CRM 中創建實體記錄(或記錄)類似於在數據庫表中添加記錄。實體划分成三個類別:系統、業務和自定義。作為處理業務數據的開發人員,您將使用業務和自定義實體。Microsoft Dynamics CRM 使用系統實體處理所有內部流程,如工作流和異步作業。不能刪除或自定義系統實體。

業務實體是默認安裝的 Microsoft Dynamics CRM 的一部分,它們顯示在自定義用戶界面中。例如,客戶、聯系人和信件都是業務實體。安裝 Microsoft Dynamics CRM 后,可以向其中添加自定義實體以滿足組織的特定業務需求。在 Microsoft Dynamics CRM 解決方案中,可以將業務和自定義實體以及屬性設置為可自定義或不可自定義。可以通過以下方式修改可自定義實體:重命名可自定義實體、添加新屬性或更改各種設置,如重復檢測或隊列支持設置。不能修改不可自定義的實體。

 

二、存在的問題

  1. 我們一般在CRM系統上通過界面來創建實體,但這個效率太低了,而且有些屬性不能修改,這樣有時就會出現這樣的問題:有時為了修改實體的一個屬性,但這個屬性卻是不能修改的,這時我們不得不把整個實體刪除,再把字段一個個的添加。
  2. 如果項目里人多了,雖然有命名規范文檔,比如,我們一般把lookup類型的字段后面加上id,但有時一不小心有兄弟就會忘記了。

 

三、解決方法

於是就想着通過工具來生成實體,網上搜索后,沒發現有什么好的實體生成工具。於是只得自己動手,創建entity及各類型的屬性:

1. 實體創建

  1: CreateEntityRequest request = new CreateEntityRequest
  2: {
  3:     HasNotes = false,
  4:     HasActivities = false,
  5:     Entity = new EntityMetadata
  6:     {
  7:         IsActivity = false,
  8:         SchemaName = customEntityName,
  9:         DisplayName = new Label(entity.DisplayName, 1033),
 10:         DisplayCollectionName = new Label(entity.DisplayName, 1033),
 11:         OwnershipType = OwnershipTypes.UserOwned,
 12:         IsAvailableOffline = true
 13:     }
 14: };

2. string屬性

  1: [AttributeType("Single Line of Text")]
  2: public class StringAttributeeCreater : AttributeCreater
  3: {
  4:     public override AttributeMetadata Create()
  5:     {
  6:         AttributeMetadata attribute = new StringAttributeMetadata
  7:         {
  8:             SchemaName = Field.PhysicalName,
  9:             RequiredLevel = GetRequiredLevel(),
 10:             MaxLength = 100,
 11:             FormatName = StringFormatName.Text,
 12:             DisplayName = new Microsoft.Xrm.Sdk.Label(Field.DisplayName, 1033)
 13:         };
 14:         return attribute;
 15:     }
 16: }
3. optionset 屬性
  1: [AttributeType(OptionSetAttributeeCreater.OptionSet_STR)]
  2: public class OptionSetAttributeeCreater : AttributeCreater
  3: {
  4:     public const string OptionSet_STR = "Option Set";
  5:     public override AttributeMetadata Create()
  6:     {
  7:         PicklistAttributeMetadata attribute = new PicklistAttributeMetadata
  8:         {
  9:             RequiredLevel = GetRequiredLevel(),
 10:             SchemaName = Field.PhysicalName,
 11:             DisplayName = new Microsoft.Xrm.Sdk.Label(Field.DisplayName, 1033),
 12:             OptionSet = new OptionSetMetadata()
 13:         };
 14: 
 15:         attribute.OptionSet.IsGlobal = true;
 16:         if (!string.IsNullOrEmpty(Field.Remark))
 17:         {
 18:             attribute.OptionSet.Name = Field.Remark;
 19:         }
 20:         else if (!string.IsNullOrEmpty(Field.TypeRule))
 21:         {
 22:             string[] arr = Field.TypeRule.Split('\n');
 23: 
 24:             foreach (string item in arr)
 25:             {
 26:                 string[] arr2 = item.Split(',', ',');
 27:                 if (arr2.Length == 2)
 28:                 {
 29:                     int value = int.Parse(arr2[0]);
 30:                     attribute.OptionSet.Options.Add(new OptionMetadata(new Microsoft.Xrm.Sdk.Label(arr2[1], 1033), value));
 31:                 }
 32:             }
 33:         }
 34:         return attribute;
 35:     }
 36: }
其它屬性以此類推,這里就不一一介紹了,大家可以查看下sdk,里面有詳細的介紹。

四、EntityCreater工具

1. 通過excel列出實體名和字段名
image 
            (EntityCreaterGuide.xlsx)

下面是個例子:

image

 

2. 首次使用EntittyCreater里,要修改下CRMEntityCreater.exe.config里面的連接信息

<add name="name" connectionString="Url=http://xxxx:5555/xxx/; Domain=xxxx; Username=xxxx; Password=xxx;"/>

運行EntityCreater,並把保存好的excel文件拖入左上角的框框里,這時它會把里面所有的sheet都顯示出來,我們可以依次創建這些實體:

image

3. 點擊左邊中間的按鈕 Generate Phical Name, 自動生成 phisical name:

image

 

4.這時,就可以通過點擊 Create Entity來創建實體了。如果只是添加些字段,也可以只勾選Fields:

image

 

5. 創建完成后,我們可以通過Save to Excel來保存生成的Phical Name到excel里,還可以點擊View Result來查看創建過程中的log信息

 

 

工具下載  (提取碼 11cc)

 

Dynamic CRM 2013學習筆記 系列匯總


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM