北京的夏天果然來了,又悶又熱不說,動不動就大暴雨還真不是鬧着玩的。
計劃着在Chicken Run裏面每天都寫一篇新博客,至少堅持100天,用來總結接觸Microsoft Dynamics CRM這幾年來用的上的知識點,
被大雨困在公司裏,等到回家,“今天”剩下的時間也就不多了,今日事今日畢,得加快動作了。
昨天的Custom Code Validation Tool作爲第一篇,今天簡單介紹一下在進行CRM extension中經常使用的工具CrmSvcUtil
2012/7/10 第二篇 CrmSvcUtil工具簡介
CrmSvcUtil是CRM 2011中的一個命令行程序。這個程序通過指定org,將元數據生成.net framework類。
生成的類是進行早期綁定方式開發的基礎(對於early bound vs. late bound將在之後的文章中敘述),支持IntelliSense。
同時生成的類是partial類型,支持擴展。
OrganizationServiceContext CrmOrganizationServiceContext
我們可以在下載的SDK中找到這個工具,具體位置在SDK\bin文件夾。
特點:
1. 輸出的文件類型:C# / VB
2. 強類型
3. 包含所有系統自帶的entity以及自定義entity。
4. 通過傳入命令行參數支持多種擴展
典型使用方法:(For On-Prem case)
CrmSvcUtil.exe /url:http://<servername>/<organizationname>/XRMServices/2011/Organization.svc
/out:<outputfilename>.cs /username:<username> /password:<password> /domain:<domainname>
/namespace:<outputnamespace> /serviceContextName:<service context name>
CrmSvcUtil支持很多parameter用來定製生成類的內容,比如是否包含Service Context等。
Options:
/nologo
Suppresses the banner.
/language:<language>
The language to use for the generated proxy code. This can be either 'CS' or 'VB'. The default language is 'CS'. Short form is '/l:'.
/url:<url>
A url or path to the SDK endpoint to contact for metadata.
/out:<filename>
The filename for the generated proxy code. Short form is '/o:'.
/namespace:<namespace>
The namespace for the generated proxy code. The default namespace is the global namespace. Short form is '/n:'.
/username:<username>
Username to use when connecting to the server for authentication. Short form is '/u:'.
/password:<password>
Password to use when connecting to the server for authentication. Short form is '/p:'.
/domain:<domain>
Domain to authenticate against when connecting to the server. Short form is '/d:'.
/serviceContextName:<service context name>
The name for the generated service context. If a value is passed in, it will be used for the Service Context. If not, no Service
Context will be generated
/help
Show this usage message. Short form is '/?'.
/deviceid:<deviceid>
Device ID to use when connecting to the online server for authentication. Short form is '/di:'.
/devicepassword:<devicepassword>
Device Password to use when connecting to the online server for authentication. Short form is '/dp:'.
實例:
1. 在Account中添加一個自定義字段-My Account Field
2. 添加一個自定義entity-My Test Entity
3. 添加1:N relationship到新建的My Test Entity
4. 使用CrmSvcUtil生成類
CrmSvcUtil.exe /url:http://<servername>/<organizationname>/XRMServices/2011/Organization.svc
/out:<outputfilename>.cs /username:<username> /password:<password> /domain:<domainname>
觀察生成的代碼,會發現有以下幾點特點:
① 生成的文件沒有命名空間
② 沒有OrganizationServiceContext或者使用了這個context的AccountSet等類
③ 所有的entity包括新建的幾種都包含在了文件中
〔My Account Field〕
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("new_myaccountfield")]
public string new_MyAccountField
{
get
{
return this.GetAttributeValue<string>("new_myaccountfield");
}
set
{
this.OnPropertyChanging("new_MyAccountField");
this.SetAttributeValue("new_myaccountfield", value);
this.OnPropertyChanged("new_MyAccountField");
}
}
〔My Test Entity〕
[System.Runtime.Serialization.DataContractAttribute()]
[Microsoft.Xrm.Sdk.Client.EntityLogicalNameAttribute("new_mytestentity")]
[System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "5.0.9690.2165")]
public partial class new_mytestentity : Microsoft.Xrm.Sdk.Entity, System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
{
/// <summary>
/// Default Constructor.
/// </summary>
public new_mytestentity() :
base(EntityLogicalName)
{
}
public const string EntityLogicalName = "new_mytestentity";
// ...
}
〔1:N relationship〕
/// <summary>
/// N:1 new_new_mytestentity_account
/// </summary>
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("new_parentcustiomentityid")]
[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("new_new_mytestentity_account")]
public new_mytestentity new_new_mytestentity_account
{
get
{
return this.GetRelatedEntity<new_mytestentity>("new_new_mytestentity_account", null);
}
set
{
this.OnPropertyChanging("new_new_mytestentity_account");
this.SetRelatedEntity<new_mytestentity>("new_new_mytestentity_account", null, value);
this.OnPropertyChanged("new_new_mytestentity_account");
}
}
5. 繼續使用CrmSvcUtil生成帶context和命名空間的文件
CrmSvcUtil.exe /url:http://<servername>/<organizationname>/XRMServices/2011/Organization.svc
/out:<outputfilename>.cs /username:<username> /password:<password> /domain:<domainname>
/namespace:<outputnamespace> /serviceContextName:<service context name>
新生成的代碼將會被包含在<outputnamespace>中,並且<service context name>類會被生成,它繼承自OrganizationServiceContext,
包含了AccountSet這樣的屬性供使用,更重要的是它從父類中繼承了一系列方便的方法如SaveChanges();
簡單的例子用於獲取所有My Test Entity的集合
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "new_name";
condition.Operator = ConditionOperator.NotNull;
FilterExpression filter = new FilterExpression();
filter.Conditions.Add(condition);
QueryExpression query = new QueryExpression();
query.ColumnSet.AddColumns("new_name", "new_mytestentityid");
query.Criteria.AddFilter(filter);
query.EntityName = "new_mytestentity";
EntityCollection results = service.RetrieveMultiple(query);
enjoy it.
引用資料:
1. Create Early Bound Entity Classes with the Code Generation Tool (CrmSvcUtil.exe)
2. CRM 2011 SDK
3.
Dynamics CRM 2011 Developer Extensions 紹介 - CrmSvcUtil ツールで生成できるファイルの種類