C# 禁止编辑报价详细信息,但允许修改报价本身

C# 禁止编辑报价详细信息,但允许修改报价本身,c#,dynamics-crm-2011,dynamics-crm,C#,Dynamics Crm 2011,Dynamics Crm,不应允许除特定用户以外的所有用户编辑或更新不满足特定条件的报价详细信息,但如果他们愿意,他们必须能够修改报价 问题是,修改报价(即用户单击活动表单记录中的“修订”按钮)会触发报价详细信息的更新,我不知道如何识别发生了什么 我当前的尝试基于一个插件,其代码如下所示: public class PreQuoteProductUpdate : Plugin { // I work with CRM Developer Tools to build plugins // This goes in Up

不应允许除特定用户以外的所有用户编辑或更新不满足特定条件的报价详细信息,但如果他们愿意,他们必须能够修改报价

问题是,修改报价(即用户单击活动表单记录中的“修订”按钮)会触发报价详细信息的更新,我不知道如何识别发生了什么

我当前的尝试基于一个插件,其代码如下所示:

public class PreQuoteProductUpdate : Plugin
{
// I work with CRM Developer Tools to build plugins 
// This goes in Update Message, Pre-Operation, Server Only, pre-image called "preImage"

protected void ExecutePreQuoteProductUpdate(LocalPluginContext localContext)
{
    if (localContext == null)
    {
        throw new ArgumentNullException("localContext");
    }

    IPluginExecutionContext context = localContext.PluginExecutionContext;
    IOrganizationService srv = localContext.OrganizationService;

    Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;

    try
    {
        PluginBody(context, srv, preImageEntity);
    }
    catch (Exception ex)
    {
        throw new InvalidPluginExecutionException("Quote Details Pre-Update", ex);
    }
}

protected void PluginBody(IPluginExecutionContext context, IOrganizationService srv, Entity preImage)
{
    if(IsRevising()) return;

    CheckSomeCondition(context, srv);

    if (preImage.Attributes.ContainsKey("ica_daconfigurazione") && preImage.GetAttributeValue<bool>("ica_daconfigurazione"))
    {
        CheckUser(context, srv);
    }
}

protected void IsRevising()
{
    // I have no clue about the logic to put here: see below.
}

protected void CheckSomeCondition(IPluginExecutionContext context, IOrganizationService srv)
{
    var entity = (Entity)context.InputParameters["Target"];
    // if some fields of entity contain some specific data, throw
    // this always happens
}

protected void CheckUser(IPluginExecutionContext context, IOrganizationService srv)
{
    //allowedUser is read from a configuration entity
    var allowedUser = new Guid();
    if (context.InitiatingUserId.Equals(serviceUser.Id) == false)
        throw new InvalidPluginExecutionException("Can't edit quote details");
}
public类PreQuoteProductUpdate:Plugin
{
//我使用CRM开发工具构建插件
//这会出现在更新消息、预操作、仅服务器、称为“预映像”的预映像中
受保护的void ExecuteRequestProductUpdate(LocalPluginContext localContext)
{
if(localContext==null)
{
抛出新ArgumentNullException(“localContext”);
}
IPluginExecutionContext context=localContext.PluginExecutionContext;
IOOrganizationService srv=localContext.OrganizationService;
实体preImageEntity=(context.PreEntityImages!=null&&context.PreEntityImages.Contains(this.preImageAlias))?context.PreEntityImages[this.preImageAlias]:null;
尝试
{
插件体(上下文、srv、preImageEntity);
}
捕获(例外情况除外)
{
抛出新的InvalidPluginExecutionException(“报价详细信息预更新”,例如);
}
}
受保护的void PluginBody(IPluginExecutionContext上下文、IOrganizationService srv、实体前映像)
{
if(IsRevising())返回;
CheckSomeCondition(上下文,srv);
if(preImage.Attributes.ContainsKey(“ica_-daconfigurazione”)和&preImage.GetAttributeValue(“ica_-daconfigurazione”))
{
CheckUser(上下文,srv);
}
}
受保护的无效IsRevising()
{
//我对这里的逻辑一无所知:见下文。
}
受保护的void CheckSomeCondition(IPluginExecutionContext上下文,IOR组织服务srv)
{
var entity=(entity)context.InputParameters[“Target”];
//如果实体的某些字段包含某些特定数据,则抛出
//这种情况经常发生
}
受保护的void CheckUser(IPluginExecutionContext上下文,IOrganizationService srv)
{
//allowedUser是从配置实体读取的
var allowedUser=new Guid();
if(context.initialingUserId.Equals(serviceUser.Id)==false)
抛出新的InvalidPlugineExecutionException(“无法编辑报价详细信息”);
}
}

我知道(在
Quote
插件中)我可以通过检查
ParentContext
来知道正在进行修订,在
QuoteDetail
插件中是否有类似的内容?我尝试了一下,但得到的都是向我抛出的
NullReferenceException
s

我是否希望有
状态
/
状态
可供检查


关于我可能忽略的任何更多信息,请询问。

QuoteDetail
Pre-Create
消息(第20阶段)中注册,并在父上下文中筛选不适用于
Quote
。如果是,就返回(实际上什么也不做)。 这同样适用于
QuoteDetail
Update
消息
这两条消息都在
ReviseQuote
消息的上下文中运行

var parentContext = context.ParentContext;

// While there is a parent context...
while (parentContext != null) {
    // When parent context is for "quote", return;
    if (parentContext.PrimaryEntityName == "quote")
    {
        return;
    }
    // Assign parent's parent context to loop parent context.
    parentContext = parentContext.ParentContext;
}

你说的复习是什么意思?你的意思是如果用户刚刚创建了报价,并且他们在创建报价后立即进行了一些编辑吗?我指的是“修订”标准功能(编辑以澄清)