dynamics crm 共享取消共享操作

  • A+
所属分类:.NET技术
摘要

//改文章参考了http://www.mamicode.com/info-detail-209704.html

Entity entity = (Entity)context.InputParameters[“Target”]; //使用该方式,只有改变了属性值才可以获取
//获得前期事件和后期实体映像
Entity preEntity = (Entity)context.PreEntityImages[“Accountname”]; //操作之前
Entity postEntity = (Entity)context.PostEntityImages[“Accountstage”];//操作之后
使用该方式的话就不用在插件里面,再取一次实体的其它属性了,会将属性值传入插件

//改文章参考了http://www.mamicode.com/info-detail-209704.html

Entity entity = (Entity)context.InputParameters["Target"]; //使用该方式,只有改变了属性值才可以获取
//获得前期事件和后期实体映像
Entity preEntity = (Entity)context.PreEntityImages["Accountname"]; //操作之前
Entity postEntity = (Entity)context.PostEntityImages["Accountstage"];//操作之后
使用该方式的话就不用在插件里面,再取一次实体的其它属性了,会将属性值传入插件

注意的是,在创建,删除中不可使用。

 

可参考该篇文章 https://www.bbsmax.com/A/1O5Eook3z7/
//例子:当前客户共享团队 客户就是指定共享的实体 而团队就是要求共享的实体

共享调用:
EntityReference Record = new EntityReference { Id = entity.Id, LogicalName = entity.LogicalName };
Grant(adminService, (EntityReference)entity["tm_centralize"], Record);

共享方法
//service:服务
//teamOrSystem,:要求共享的实体
//Record:指定共享的实体
private static void Grant_Read(IOrganizationService service, EntityReference teamOrSystem, EntityReference Record)
{
GrantAccessRequest grantAccessRequest = new GrantAccessRequest
{
PrincipalAccess = new PrincipalAccess
{
Principal = teamOrSystem,
AccessMask = AccessRights.ReadAccess | AccessRights.WriteAccess //赋予的权限
},
Target = Record,
};

service.Execute(grantAccessRequest);

}

//取消共享
Entity Pusers = service.Retrieve("Account", users.GetAttributeValue<EntityReference>("Accountid").Id, new ColumnSet("tm_centralize"));
RevokeUser(service, entPre.GetAttributeValue<EntityReference>("tm_centralize"), new EntityReference(entPre.LogicalName, entPre.Id));

private void RevokeUser(IOrganizationService service, EntityReference teamOrSystem, EntityReference Record)
{
RevokeAccessRequest revokeAccessRequest = new RevokeAccessRequest
{
Revokee = teamOrSystem,
Target = Record,
};

service.Execute(revokeAccessRequest);

}

//查询单条
EntityReference entity = (EntityReference)context.InputParameters["Target"];
Entity aaccountTargetEntity = adminService.Retrieve("tm_purchaseamount", entity.Id, new ColumnSet(new string[] { "tm_year", "tm_account" }));

 

plugin中查询
string mFetchXml = string.Format(@"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='tm_purchaseamount'>
<attribute name='tm_purchaseamountid' />
<attribute name='tm_name' />
<attribute name='createdon' />
<order attribute='tm_name' descending='false' />
<filter type='and'>
<condition attribute='tm_year' operator='eq' value='{0}' />
<condition attribute='tm_account' operator='eq' uiname='=C=' uitype='account' value='{1}' />
<condition attribute='statecode' operator='eq' value='0' />
</filter>
</entity>
</fetch>
", ((OptionSetValue)entity["tm_year"]).Value.ToString(), ((EntityReference)entity["tm_account"]).Id.ToString());

EntityCollection accountEntites = adminService.RetrieveMultiple(new FetchExpression(mFetchXml));
if (accountEntites.Entities.Count > 0)
{
throw new InvalidPluginExecutionException( ((OptionSetValue)entity["tm_year"]).Value.ToString() + " Year already exists!");
}