sitecore--发布目录时如何通过代码获取所有发布项

发布于 2024-10-18 01:30:17 字数 109 浏览 3 评论 0 原文

发布目录时如何通过代码获取所有发布项目以及我应该将处理程序添加到哪个事件,publish:beginpublish:itemProcessing

How to get all the publishing items by code when a directory is being published and which event should I add my handler to, publish:begin or publish:itemProcessing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光礼记 2024-10-25 01:30:17

如果您想要设置自定义事件处理程序,请从 web.config 参考开始。

<event name="publish:begin">
    <handler type="YourNamespace.YourClass, YourLibrary" method="YourHandlerMethod" />
</event>

然后创建一个支持此引用的类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Sitecore.Diagnostics;
using Sitecore.Sites;
using Sitecore.Configuration;
using Sitecore.Caching;
using Sitecore.Events;
using Sitecore.Publishing;
using Sitecore.Data.Events;
using Sitecore.Data;
using Sitecore.Data.Items;

namespace YourNamespace {
public class YourClass {
        public void YourHandlerMethod(object sender, EventArgs args) {
            Assert.ArgumentNotNull(sender, "sender");
            Assert.ArgumentNotNull(args, "args");

            //try to get the sitecore event args
            if (args.GetType().ToString().Equals("Sitecore.Events.SitecoreEventArgs")) {
                SitecoreEventArgs sargs = (SitecoreEventArgs)args;
                foreach (object o in sargs.Parameters) {
                    //try to get the publisher object
                    if (o.GetType().ToString().Equals("Sitecore.Publishing.Publisher")) {
                        Publisher p = (Publisher)o;
                        if (p != null) {
                            Item root = p.Options.RootItem;
                            bool b = p.Options.RepublishAll;
                            if(p.Options.Mode.Equals(PublishMode.SingleItem)){
                                //only one item published
                            }
                        }
                    }
                }
            }
        }
    }
}

从此类中,您可以尝试访问发布者对象,该对象将为您提供已发布的根项目和发布选项。发布选项将告诉您是否发布了单个项目或是否发布了所有语言版本。

If you're looking to setup a custom event handler start with the web.config reference.

<event name="publish:begin">
    <handler type="YourNamespace.YourClass, YourLibrary" method="YourHandlerMethod" />
</event>

Then create a class that will support this reference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Sitecore.Diagnostics;
using Sitecore.Sites;
using Sitecore.Configuration;
using Sitecore.Caching;
using Sitecore.Events;
using Sitecore.Publishing;
using Sitecore.Data.Events;
using Sitecore.Data;
using Sitecore.Data.Items;

namespace YourNamespace {
public class YourClass {
        public void YourHandlerMethod(object sender, EventArgs args) {
            Assert.ArgumentNotNull(sender, "sender");
            Assert.ArgumentNotNull(args, "args");

            //try to get the sitecore event args
            if (args.GetType().ToString().Equals("Sitecore.Events.SitecoreEventArgs")) {
                SitecoreEventArgs sargs = (SitecoreEventArgs)args;
                foreach (object o in sargs.Parameters) {
                    //try to get the publisher object
                    if (o.GetType().ToString().Equals("Sitecore.Publishing.Publisher")) {
                        Publisher p = (Publisher)o;
                        if (p != null) {
                            Item root = p.Options.RootItem;
                            bool b = p.Options.RepublishAll;
                            if(p.Options.Mode.Equals(PublishMode.SingleItem)){
                                //only one item published
                            }
                        }
                    }
                }
            }
        }
    }
}

From this class you can try to access the publisher object which will give you the root item published and publish options. The publish options will tell you if there was a single item published or if it published all versions of languages.

撩动你心 2024-10-25 01:30:17

根据您的实际需求,将自定义处理器注入到publishItem管道中可能比使用publish:itemProcessing事件更有意义。如果您仔细查看 web.config 中的该管道(搜索“”),您会发现这些事件(publish:itemProcessingpublish:itemProcessed) 由管道的适当处理器生成。

注意:发布过程相当复杂,我不建议对正在发布的项目进行任何可能影响总体过程的操作。我不能在这里给你一个例子 - 只有你的幻想设定了限制...

另请注意,对于这些事件以及我提到的管道,你一次操作 1 个项目 - 每个项目都会调用它正在发布的项目。这可能对性能至关重要...

更新:您可以在 这篇博文。除了本身有用之外,它还包含有关该主题的更多有用链接。

Depending on your real needs, it might make more sense to inject a custom processor into the publishItem pipeline rather than use publish:itemProcessing event. If you take a closer look at that pipeline (search for "<publishItem") in web.config, you'll that those events (publish:itemProcessing and publish:itemProcessed) are generated by the appropriate processors of pipeline.

NOTE: the publishing process is rather complex and I would not recommend doing anything with the item being published that can influence the process in general. I can't give you an example here - only your fantasy sets the limits...

Note also, that with those events, as well as the pipeline I mentioned, you operate with 1 item at a time - it will be called for each item being published. This can become performance critical...

UPDATE: You can read more about pipeline in this blog post. Apart from being useful itself, it contains more useful links on the subject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文