using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 視圖 /// </summary> public class UserQueryHelper { public static readonly string entityName = "userquery"; public Guid viewid = Guid.Empty; public IOrganizationService service; /// <summary> /// 建立視圖 /// </summary> public void Create() { #region fetchXml string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='opportunity'> <order attribute='estimatedvalue' descending='false' /> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> </filter> <attribute name='name' /> <attribute name='estimatedvalue' /> <attribute name='estimatedclosedate' /> <attribute name='customerid' /> <attribute name='opportunityratingcode' /> <attribute name='closeprobability' /> <link-entity alias='opportunitycustomeridcontactcontactid' name='contact' from='contactid' to='customerid' link-type='outer' visible='false'> <attribute name='emailaddress1' /> </link-entity> <attribute name='opportunityid' /> </entity> </fetch>"; #endregion #region layoutXml string layoutXml = @"<grid name='resultset' object='3' jump='name' select='1' preview='1' icon='1'> <row name='result' id='opportunityid'> <cell name='name' width='150' /> <cell name='customerid' width='150' /> <cell name='estimatedclosedate' width='150' /> <cell name='estimatedvalue' width='150' /> <cell name='closeprobability' width='150' /> <cell name='opportunityratingcode' width='150' /> <cell name='opportunitycustomeridcontactcontactid.emailaddress1' width='150' disableSorting='1' /> </row> </grid>"; #endregion Entity en = new Entity() { LogicalName = entityName }; en["name"] = "自定義商機視圖"; en["returnedtypecode"] = "opportunity"; en["querytype"] = 0; en["fetchxml"] = fetchXml; en["layoutXml"] = layoutXml; en["querytype"] = 0; viewid = service.Create(en); } /// <summary> /// 將用戶查詢分派給另外一用戶或團隊 /// </summary> /// <param name="assignee">用戶或團隊引用</param> public void Assign(EntityReference assignee) { AssignRequest request = new AssignRequest(); request.Target = new EntityReference() { LogicalName = entityName, Id = viewid }; request.Assignee = assignee; AssignResponse response = (AssignResponse)service.Execute(request); } /// <summary> /// 執行由 ID 指定的先前保存的查詢 /// </summary> public void ExecuteByIdUserQuery() { ExecuteByIdUserQueryRequest request = new ExecuteByIdUserQueryRequest(); request.EntityId = new EntityReference() { LogicalName = entityName, Id = viewid }; ExecuteByIdUserQueryResponse response = (ExecuteByIdUserQueryResponse)service.Execute(request); string result = response.String; } /// <summary> /// 刪除指定安全主體(用戶或團隊)對用戶查詢的全部訪問權限 /// </summary> /// <param name="revokee">用戶或團隊引用</param> public void RevokeAccess(EntityReference revokee) { RevokeAccessRequest request = new RevokeAccessRequest(); request.Target = new EntityReference() { LogicalName = entityName, Id = viewid }; request.Revokee = revokee; RevokeAccessResponse response = (RevokeAccessResponse)service.Execute(request); } /// <summary> /// 刪除視圖 /// </summary> public void Delete() { service.Delete(entityName, viewid); } }