crm操作安全字段

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;

    /// <summary>
    /// 安全字段
    /// </summary>
    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;

        /// <summary>
        /// 创建安全字段
        /// </summary>
        public void Create(IOrganizationService service)
        {
            Entity en = new Entity() { LogicalName = entityName };
            en["name"] = "new_name";
            fieldSecurityProfileId = service.Create(en);
        }

        /// <summary>
        /// 给安全字段加入团队
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="teamId">团队</param>
        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);
        }

        /// <summary>
        /// 给安全字段移除团队
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="teamId">团队</param>
        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);
        }

        /// <summary>
        /// 给安全字段加入用户
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="teamId">用户</param>
        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);
        }

        /// <summary>
        /// 给安全字段移除用户
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="teamId">用户</param>
        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);
        }

        /// <summary>
        /// 把安全字段和实体关联
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="entity">实体</param>
        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);
        }

        /// <summary>
        /// 删除实体里面的安全字段
        /// </summary>
        /// <param name="service">服务</param>
        public void RemoveAttribute(IOrganizationService service)
        {
            service.Delete(FieldPermission, fieldSecurityProfileId);
        }

        /// <summary>
        /// 删除安全字段
        /// </summary>
        public void Delete(IOrganizationService service)
        {
            service.Delete(entityName, fieldSecurityProfileId);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值