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); } }