2016-07-21 14 views
0

特定のテンプレートがない限り、誰かがアイテムをフォルダにコピーまたは移動しないようにしようとしています。私はitem:createdとitem:movingのカスタムイベントハンドラを作成することに決めました。アイテムの場合:アイテムが間違ったタイプのものであれば、それを削除するだけです。 item:movingの場合は、移動操作を終了します。今作成したイベント::私はアイテムのために、次のコードを持っているSitecoreでアイテムを削除しようとするとエラーが発生する

public void OnItemCreated(object sender, EventArgs args) 
    { 
     var createdArgs = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs; 

     Sitecore.Diagnostics.Assert.IsNotNull(createdArgs, "args"); 
     if (createdArgs != null) 
     { 
      Sitecore.Diagnostics.Assert.IsNotNull(createdArgs.Item, "item"); 
      if (createdArgs.Item != null) 
      { 
       var item = createdArgs.Item; 

       if (item.Parent != null) 
       { 
        //see if the item is being placed under a Navigation Item type or under the Navigation folder 
        if (item.Parent.TemplateName == "Navigation Item" || item.ParentID.ToString() == "{6ED240C9-1B69-48E2-9FD9-6C45CD8ABE63}") 
        { 
         if (item.TemplateName != "Navigation Item") 
         { 
          using (new Sitecore.SecurityModel.SecurityDisabler()) 
          { 
           // Delete the item, warn user 
           item.DeleteChildren(); 
           item.Delete(); 

           SheerResponse.Alert("Sorry, you can only add items based on the \"Navigation Item\" template here"); 

          } 

         } 
        } 
       } 

      } 
     } 

項目が削除されますないが、しかし、エラーが無いメッセージをポップアップ表示されます。スタックトレースは次のとおりです。

Server Error in '/' Application. 

    item 

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidOperationException: item 

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

    Stack Trace: 


    [InvalidOperationException: item] 
     Sitecore.Tasks.BaseArchiveTask.Remove() +139 
     Sitecore.Tasks.ItemEventHandler.UpdateArchiving(Item item, Boolean force) +359 
     Sitecore.Tasks.ItemEventHandler.OnItemCopied(Object sender, EventArgs args) +109 
     Sitecore.Events.EventSubscribers.RaiseEvent(String eventName, Object[] parameters, EventResult result) +388 
     Sitecore.Events.Event.RaiseEvent(String eventName, Object[] parameters) +349 
     System.EventHandler`1.Invoke(Object sender, TEventArgs e) +0 
     Sitecore.Data.Engines.EngineCommand`2.RaiseEvent(EventHandler`1 handlers, Func`2 argsCreator) +129 
     Sitecore.Data.Engines.DataCommands.CopyItemCommand.Executed() +21 
     Sitecore.Data.Engines.EngineCommand`2.Execute() +173 
     Sitecore.Data.Managers.ItemProvider.CopyItem(Item source, Item destination, Boolean deep, String copyName, ID copyId) +783 
     Sitecore.Data.Managers.ItemManager.CopyItem(Item source, Item destination, Boolean deep, String copyName, ID copyId) +182 
     Sitecore.Workflows.WorkflowContext.CopyItem(Item item, Item destination, String copyName, ID copyID, Boolean deep) +127 
     Sitecore.Workflows.WorkflowContext.CopyItem(Item item, Item destination, String copyName) +173 
     Sitecore.Shell.Framework.Pipelines.CopyItems.CopyItem(Item target, Item itemToCopy) +135 
     Sitecore.Shell.Framework.Pipelines.CopyItems.Execute(CopyItemsArgs args) +293 

    [TargetInvocationException: Exception has been thrown by the target of an invocation.] 
     System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 
     System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +128 
     System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146 
     Sitecore.Pipelines.Processor.Invoke(PipelineArgs args) +364 
     Sitecore.Nexus.Pipelines.NexusPipelineApi.Resume(PipelineArgs args, Pipeline pipeline) +297 
     Sitecore.Web.UI.Sheer.ClientPage.ResumePipeline() +224 
     Sitecore.Web.UI.Sheer.ClientPage.OnPreRender(EventArgs e) +779 
     Sitecore.Shell.Applications.ContentManager.ContentEditorPage.OnPreRender(EventArgs e) +24 
     System.Web.UI.Control.PreRenderRecursiveInternal() +107 
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +7675 

これを引き起こす原因は何ですか?

ありがとうございました。

+1

なぜあなたは動いているイベントを使っていますか? – Gatogordo

+1

あなたのイベントをアイテムにしてみてください:item:createdの代わりにcreatingを作成してください。 – Anton

答えて

0

他の人はあなたがアイテムを使用する必要があり、言っただけのように:代わりにイベントを作成し、ここにあなたが何をする必要があるかです:

public class OnItemCreating 
    { 
    public void OnItemCreating(object sender, EventArgs args) 
    { 
     using (new SecurityDisabler()) 
     { 
     ItemCreatingEventArgs arg = Event.ExtractParameter(args, 0) as ItemCreatingEventArgs; 

     if (arg .Item != null) 
      { 
       var item = arg .Item; 

       if (item.Parent != null) 
       { 
        //see if the item is being placed under a Navigation Item type or under the Navigation folder 
        if (item.Parent.TemplateName == "Navigation Item" || item.ParentID.ToString() == "{6ED240C9-1B69-48E2-9FD9-6C45CD8ABE63}") 
        { 
         if (item.TemplateName != "Navigation Item") 
         { 
          using (new Sitecore.SecurityModel.SecurityDisabler()) 
          { 

           ((SitecoreEventArgs)args).Result.Cancel = true; 
           SheerResponse.Alert("Sorry, you can only add items based on the \"Navigation Item\" template here"); 

          } 

         } 
        } 
       } 

      } 
     } 
    } 
    } 

と構成のために、あなたが持っている必要があります。

<event name="item:creating"> 
    <handler type="YourNameSpace.OnItemCreating, YourAssembly" method="OnItemCreating" /> 
</event> 
+0

私はあなたが指示どおりに行ったが、何らかの理由で私のコードがまったく実行されていない...「コピー先」はアイテムを作成しない:イベントを作成する?私はアイテムの下にハンドラーを配置しました。作成し、すべてをダブルチェックしましたので、今何が起こっているのか分かりません。 – user3298634

+0

@ user3298634新しいアイテムを作成するときにコードが実行されますか?そうでなければ、item:コピーイベントを試してみて、それが呼び出されたかどうかを確認してください。 –

+0

item:creatingイベントは決して引き起こされません。イベントのタイミングを高く設定し、ログを確認して確認しました。私はitem:handlerのハンドラを作成しました。 – user3298634

関連する問題