ในบทความก่อนหน้านี้ก็เป็นเรื่องเกี่ยวกับ การสร้าง CrmDiscoveryService web reference ใน visual studio ไปแล้วคราวนี้ก็มาดูเรื่องการใช้งาน CrmDiscoveryService กันต่อ ก็อย่างที่ทราบว่า CrmDiscoveryService web service URL endpoint อยู่สามแบบขึ้นอยู่กับ รูปแบบการ authentication
Active Directory –
http://servername:port/mscrmservices/2007/AD/CrmDiscoveryService.asmx
IFD –
http://servername:port/mscrmservices/2007/IFD/CrmDiscoveryService.asmx
Online –
http://servername:port/mscrmservices/2007/Passport/CrmDiscoveryService.asmx
ในกรณี IFD และ Online ต้องได้รับ CrmTicket จาก CrmDiscoveryService ในกรณีของ Online เราต้องการข้อมูลเพิ่มเติมได้แก่ Policy และ Passport ticket ด้วย ซึ่งข้อมูลเหล่านี้จะถูกจัดเตรียมไว้ที่ CrmDiscoverService เพื่อใช้ในการ กำหนดค่าของ CrmAuthenticationToken และ Crm service instances.
คราวนี้ในกรณี On-premise ::: หรือกรณีที่เรารู้ว่า Organization name และ webservice url endpoint คืออะไรเราไม่จำเป็นจะต้องใช้ CrmDiscoveryService ก็ได้ แต่ใช้ก็ได้อีกเหมือนกันคราวนี้ก็ขั้นตอนที่จะใช้ CrmDiscoveryService ก็มีขั้นตอนดังนี้
-----------------------------------------------------------------
// Create and configure the CrmDiscoveryService Web service proxy.
CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.UseDefaultCredentials = true;
discoveryService.Url = "http://localhost/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";
// Retrieve the list of organizations that the logged on user belongs to.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse orgResponse =
(RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);
// Locate the target organization in the list.
OrganizationDetail orgInfo = null;
foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
{
if (orgDetail.OrganizationName.Equals("AdventureWorksCycle"))
{
orgInfo = orgDetail;
break;
}
}
// Check whether a matching organization was not found.
if (orgInfo == null)
throw new Exception("The specified organization was not found.");
----------------------------------------------------------------
หลังจาก ได้ได้ข้อมูล OrganizationDetail มาแล้วเรากก็สามารถใช้งาน CrmService และ CrmMetadataService โดยผ่าน URL ที่ได้มาจาก OrganizationDetail ตา Code ด้านล่าง
----------------------------------------------------------------
// Setting Crm Authentication Token
CrmAuthenticationToken myCrmAuthToken = new CrmAuthenticationToken();
// 0- AD , 1- IFD , 2 - Windows Live
myCrmAuthToken.AuthenticationType = 0;
myCrmAuthToken.OrganizationName = orgInfo.OrganizationName;
// Finally setting our CrmService
CrmService myCrmService = new CrmService();
myCrmService.CrmAuthenticationTokenValue = myCrmAuthToken;
myCrmService.Url = orgInfo.CrmServiceUrl;
-----------------------------------------------------------------
แต่อย่างที่บอก ถ้ารู้อยู่แล้วว่า CrmService และ CrmMetadataService Web service URL สำหรับ organization ที่เราต้องการ access อยู่ที่ไหนก็ไม่จำเป็นต้องใช้ผ่าน CrmDiscoverySerive ก็ได้ :)
อ้างอิง
1.
Nishant Rana’s Weblog2.
Using the CrmDiscoveryService Web Service: On-Premise