sitecore-- how to get all the publishing items by code when a directory is being published
How to get all the publishing items by code when a director开发者_开发技巧y is being published and which event should I add my handler to, publish:begin
or publish:itemProcessing
?
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.
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.
精彩评论