3

我正在尝试以编程方式发布媒体项目,但我遇到了 PublishOptions 对象的问题。在运行时,当我上传媒体项目时,实例化 PublishOptions 对象的代码行会中断。我收到以下消息:

"Value cannot be null. Parameter Name: item"

堆栈跟踪是:

at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
at Sitecore.Data.Managers.ItemProvider.ApplySecurity(Item item, SecurityCheck securityCheck)
at Sitecore.Data.Managers.ItemProvider.GetRootItem(Language language, Version version, Database database, SecurityCheck securityCheck)
at Sitecore.Data.Managers.ItemManager.GetRootItem(Language language, Version version, Database database)
at Sitecore.Nexus.Data.DataCommands.ResolvePathCommand.(String itemPath, Database database)
at Sitecore.Nexus.Data.DataCommands.ResolvePathCommand.(String itemPath, Database database)
at Sitecore.Nexus.Data.NexusDataApi.ResolvePath(String itemPath, Database database)
at Sitecore.Data.Engines.DataCommands.ResolvePathCommand.DoExecute()
at Sitecore.Data.Engines.EngineCommand`2.Execute()
at Sitecore.Data.Engines.DataEngine.ResolvePath(String itemPath)
at Sitecore.Data.Managers.ItemProvider.ResolvePath(String itemPath, Database database)
at Sitecore.Data.Managers.ItemProvider.GetItem(String itemPath, Language language, Version version, Database database, SecurityCheck 

这是 PublishOptions 对象被实例化的代码(它正在中断):

public void OnItemSaved(Object sender, EventArgs args)
    {
        var item = Event.ExtractParameter(args, 0) as Item;

        using (new SecurityDisabler())
        {
            if (item != null)
            {
                if (item.Paths.IsMediaItem)
                {
                    var source = new Database("master");
                    var target = new Database("web");


                    var options = new PublishOptions(source, target, PublishMode.SingleItem, item.Language, DateTime.Now)
                                      {
                                          RootItem = item,
                                          Deep = true,

                                      };

                    var publisher = new Publisher(options);

                    publisher.PublishAsync();
                }
            }
        }
    }

我收到此错误的原因可能是什么?

4

2 回答 2

5

试试这个,看看这是否有效,我猜它与数据库有关。

public void OnItemSaved(Object sender, EventArgs args)
    {
        var item = Event.ExtractParameter(args, 0) as Item;

        using (new SecurityDisabler())
        {
            if (item != null)
            {
                if (item.Paths.IsMediaItem)
                {
                    var source = Factory.GetDatabase("master");
                    var target = Factory.GetDatabase("web");

                    var options = new PublishOptions(source, target, 
                                                     PublishMode.SingleItem, item.Language, 
                                                     DateTime.Now)
                                      {
                                          RootItem = item,
                                          Deep = true,
                                      };

                    var publisher = new Publisher(options);

                    publisher.PublishAsync();
                }
            }
        }
    }
于 2013-11-13T18:22:35.990 回答
0

我认为你陷入了一个典型的错误。item:saved 也会在您的项目发布时触发。

我最初的猜测是,您应该将支票扩大到

if ( item != null && item.Database.Name == "master" )

以防止您的代码尝试发布该项目,因为 item:saved 事件在发布期间在“web”上触发。

于 2013-11-13T16:53:11.847 回答