我正在试验CRM插件。我的最终目标是创建一个插件,该插件将运行另一个程序,该程序只是将项目凭据添加到excel文件中。对于我的示例项目,我基本上只关注https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/tutorial-write-plug-in。这里提供的示例非常好,并且教会了很多关于插件的知识。我遇到的问题是,每当我尝试执行本教程中没有涉及的代码时,我都会收到错误并且调试过程甚至没有达到我的断点。当我从函数中删除我的个人代码时,它工作正常。
public class PostCreateContact : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//Process firstProc = new Process();
//firstProc.StartInfo.FileName = "notepad++.exe";
//firstProc.StartInfo.WorkingDirectory = "C:\\Program Files (x86)\\Notepad++\\notepad++.exe";
//firstProc.EnableRaisingEvents = true;
//firstProc.Start();
if (context.InputParameters.Contains("Target")&& context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
try
{
Entity followup = new Entity("task");
followup["subject"] = "Send e-mail to the new customer.";
followup["description"] = "Follow up with the customer. Check if there are any new issues that need resolution.";
followup["scheduledstart"] = DateTime.Now;
followup["scheduledend"] = DateTime.Now.AddDays(2);
followup["category"] = context.PrimaryEntityName;
if (context.OutputParameters.Contains("id"))
{
Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
string regardingobjectidType = "contact";
followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid);
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
service.Create(followup);
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
我注释掉的5行代码是我提到的个人代码。如果我要取消评论这些界限。代码不会达到我的突破点。我的断点设置在“public void Execute(IserviceProver servicerProvider)”行下面。有人可以向我解释为什么每当我插入自己的代码执行非CRM相关任务时它立即失败?
此外,除了尝试打开记事本++之外,尝试抛出消息框也不起作用。
插件的本质是它们对Dynamics 365系统中的数据进行操作,或者通过HTTP或HTTPS与外部世界进行通信。写入本地文件超出了插件的设计范围。
让插件与外界沟通的一种方法是写一个Azure-aware plugin。
如果你的系统是内部部署的,那么在the sandbox之外注册你的插件会提供更大的灵活性,虽然写入本地文件,即使技术上可行(我不相信它)仍然是一个不好的做法。
要以受支持的方式向用户发送文件,您可以将文件创建为注释附件。完成后,您只需通过电子邮件向用户发送指向Dynamics 365中该注释的链接即可。
如果您希望文件在Dynamics 365之外可用,则可以触发Azure-aware插件,该插件基于Azure的侦听器将检索Note附件文件,将其上载到SharePoint或OneDrive等云文件服务,然后通过电子邮件向用户发送链接到文件。
由于即使是沙盒插件也可以使用HTTPS,因此表面上可能会有一个插件将文件直接上传到外部云主机,但身份验证可能会非常棘手。而且,根据上传速度和文件大小,您可以在沙盒的2分钟超时时间内运行。
这些天的标准做法是假设任何本地系统有一天可能会在线移动,因此设计所有代码以在沙箱中运行。