using System;using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Messages;////// 安全字段/// public class FieldSecurityProfileHelper{ public static readonly string entityName = "fieldsecurityprofile"; public static readonly string FieldPermission = "FieldPermission"; public Guid fieldSecurityProfileId = Guid.Empty; public Guid fieldPermissionId = Guid.Empty; ////// 创建安全字段 /// public void Create(IOrganizationService service) { Entity en = new Entity() { LogicalName = entityName }; en["name"] = "new_name"; fieldSecurityProfileId = service.Create(en); } ////// 给安全字段添加团队 /// /// 服务 /// 团队 public void AddTeam(IOrganizationService service, Guid teamId) { AssociateRequest request = new AssociateRequest(); request.Relationship = new Relationship("teamprofiles_association"); request.Target = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId }; request.RelatedEntities = new EntityReferenceCollection(); request.RelatedEntities.Add(new EntityReference() { LogicalName = "team", Id = teamId }); service.Execute(request); } ////// 给安全字段移除团队 /// /// 服务 /// 团队 public void RemoveTeam(IOrganizationService service, Guid teamId) { DisassociateRequest request = new DisassociateRequest(); request.Relationship = new Relationship("teamprofiles_association"); request.Target = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId }; request.RelatedEntities = new EntityReferenceCollection(); request.RelatedEntities.Add(new EntityReference() { LogicalName = "team", Id = teamId }); service.Execute(request); } ////// 给安全字段添加用户 /// /// 服务 /// 用户 public void AddUser(IOrganizationService service, Guid userId) { AssociateRequest request = new AssociateRequest(); request.Relationship = new Relationship("systemuserprofiles_association"); request.Target = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId }; request.RelatedEntities = new EntityReferenceCollection(); request.RelatedEntities.Add(new EntityReference() { LogicalName = "systemuser", Id = userId }); service.Execute(request); } ////// 给安全字段移除用户 /// /// 服务 /// 用户 public void RemoveUser(IOrganizationService service, Guid userId) { DisassociateRequest request = new DisassociateRequest(); request.Relationship = new Relationship("systemuserprofiles_association"); request.Target = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId }; request.RelatedEntities = new EntityReferenceCollection(); request.RelatedEntities.Add(new EntityReference() { LogicalName = "systemuser", Id = userId }); service.Execute(request); } ////// 把安全字段和实体关联 /// /// 服务 /// 实体 public void CreateAttribute(IOrganizationService service, string entity) { Entity en = new Entity() { LogicalName = FieldPermission }; //实体名称 en["entityname"] = entity; en["canread"] = new OptionSetValue(FieldPermissionType.Allowed); en["attributelogicalname"] = "new_attributename"; en["fieldsecurityprofileid"] = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId }; fieldSecurityProfileId = service.Create(en); } ////// 删除实体里面的安全字段 /// /// 服务 public void RemoveAttribute(IOrganizationService service) { service.Delete(FieldPermission, fieldSecurityProfileId); } ////// /// 删除安全字段 /// /// public void Delete(IOrganizationService service) { service.Delete(entityName, fieldSecurityProfileId); }}