【发布时间】:2016-10-31 21:17:12
【问题描述】:
我是 C# 和动态插件的新手。为了学习和测试,我成功地创建了几个非常简单的插件。现在,我正在尝试更多地了解我实际上需要对插件执行的操作——我正在尝试获取自定义实体上的字段值,并使用该值来更新相关自定义的属性实体。
我的插件在自定义实体的更新消息上注册(称为 new_registration)。它在操作后异步运行。更新和触发插件的字段(选项集“状态”字段)不在任何地方的代码中使用。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
namespace PlugInTests
{
public class AdjustTimeSlots: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters != null)
{
Entity entity = (Entity)context.InputParameters["Target"];
Guid id = entity.Id;
tracingService.Trace("got input parameters");
//get time slot
string slot = (string)entity.Attributes["new_yourtimeslot"];
EntityReference eventclass = (EntityReference)entity.Attributes["new_eventregistrationrelationshipid"];
tracingService.Trace("got time slot");
//set updated entity (event/class)
Entity parentevent = new Entity("new_eventclass");
parentevent.Id = eventclass.Id;
parentevent.Attributes["new_timeslotsfordelete"] = slot;
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//update event record
tracingService.Trace("Update time slot plugin");
service.Update(parentevent);
}
}
}
}
通过测试,我已经缩小了这一行(至少最初是这样)失败的范围:
string slot = (string)entity.Attributes["new_yourtimeslot"];
我在插件跟踪日志中遇到的错误是:
给定的键不在字典中。
我已经检查并再次检查,我知道我得到的字段名称是正确的。我在如何从输入参数中获取值方面做错了吗?还是我把事情搞砸了,我什至没有意识到我可能搞砸了?任何帮助表示赞赏,谢谢。
【问题讨论】:
标签: c# plugins crm microsoft-dynamics entities