Dynamics CRM 365 - 零基础入门学习后端插件语法总结(样例使用方法)

这篇博客总结了DynamicsCRM365后端插件开发的基本语法,包括如何获取上下文、组织服务,以及操作记录、选项集、日期字段的方法。还展示了查询表达式和筛选配置的示例,适合初学者参考和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dynamics CRM 365 - 零基础入门学习后端插件语法总结(样例使用方法)

整理下平时CRM开发中用到的一些基本的插件语法,或许对初学Dynamics的新手有所帮助

  1. 插件必写文
public IPluginExecutionContext context = null;//上下文
public IOrganizationServiceFactory serviceFactory = null;//组织服务工厂对象
public IOrganizationService service = null;//Org服务对象
 //创建执行上下文
context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//创建组织服务工厂对象
serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
//Org服务对象
service = serviceFactory.CreateOrganizationService(context.UserId);
//触发当前插件的那条记录
Entity entity= service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));
  1. 获取当前实体对应的查找类型id与实体
//获取当前实体的查找类型字段的id
Guid guid = ((EntityReference)entity["查找字段名称"]).Id;
//方法二
Guid guid = entity.GetAttributeValue<EntityReference>("查找字段名称").Id;
//根据当前id去new出查找字段的实体
Entity entityMember = new Entity("对应的实体名称", guid);
  1. 获取选项集类型
OptionSetValue optionSet = (OptionSetValue)entity["选项集字段"];
  1. 接收bool类型
bool result=entity.GetAttributeValue<Boolean>("两个选项字段名称");
  1. 小数取整对字段赋值
//将小数字段取出转换为double类型
double sum= ((double)entity["小数字段名称"]);
//小数四舍五入
EntityName["FileName"] = int.Parse(Math.Round(sum).ToString());
  1. 实体时间字段加一年赋值
EntityName["FileName"] = Convert.ToDateTime(entity["时间字段"]).AddYears(1);
  1. 查询(自定义)
//创建查询表达式
QueryExpression query = new QueryExpression("实体名称");
//设置显示的列
query.ColumnSet = new ColumnSet(new string[] { "列1", "列2", "列3"});
//创建条件表达式,查询会员id对应的积分账户
ConditionExpression condition = new ConditionExpression()
{
    //条件等于那一列
      AttributeName = "列名",
     Operator = ConditionOperator.Equal
};
//列名的条件,等于的id
condition.Values.Add(id);
//将条件表达式绑定到查询表达式上
query.Criteria.AddCondition(condition);
//获取查询结果集合
EntityCollection collection = service.RetrieveMultiple(query);
  1. 查询2(根据筛选配置)
string detailwhere= "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
detailwhere+= "<entity name='hy_jinhuodetails'>";
detailwhere+= "<attribute name='hy_jinhuodetailsid' />";
detailwhere+= "<attribute name='hy_name' />";
detailwhere+= "<attribute name='createdon' />";
detailwhere+= "<order attribute='hy_name' descending='false' />";
detailwhere+= "<filter type='and'>";
detailwhere+= "<condition attribute='hy_jinhuoname' operator='eq' uiname='"+currentEntity["hy_name"] +"' uitype='hy_jinhuo' value='"+currentEntity.Id+"' />";
detailwhere+= "</filter>";
detailwhere+= "</entity>";
detailwhere+= "</fetch>";
//创建条件表达式
FetchExpression condition = new FetchExpression(detailwhere);
//根据条件查询查询符合的entity集合
DataCollection<Entity> select = service.RetrieveMultiple(condition).Entities;

小Monkey还会不断补充的

到此就结束啦,快去练习一下吧!欢迎大佬和小Monkey沟通。
在这里插入图片描述

感谢大佬指正 小Monkey
如果你觉得有用的话,就留个赞吧!蟹蟹

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猴麦麦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值