Edting collection of items is slow in Sitecore -
i've implemented on item:saved handler per question posted here: run code when publishing restriction saved in sitecore
when author changes publishing restrictions on page, iterate through each of related components page, updating publishing restrictions on each match page item. works, pages have 150 or components , process of editing each taking ever. result ui hangs 5 minutes while runs. not good.
i'm doing this:
compitem.editing.beginedit(); compitem.publishing.validfrom = pageitem.publishing.validfrom; compitem.publishing.validto = pageitem.publishing.validto; compitem.editing.endedit(true, true);
i've played around updatestatistics , silent arguments. if "silent" ui responds, of course still takes forever update run in background cause issues, since there window of time pub restrictions between page , components out of sync.
any thoughts on why updating 150 items slow? ways speed up?
here's full code:
public void onitemsaved(object sender, eventargs args) { item item = event.extractparameter(args, 0) item; if (item == null) return; //if it's page, update page component templates same publish restrictions. if(this.hasbasetemplate(item, globalid.pagebasetemplate)) { itemchanges itemchanges = event.extractparameter(args, 1) itemchanges; if (itemchanges != null && (itemchanges.fieldchanges.contains(__validfrom) || itemchanges.fieldchanges.contains(__validto))) { foreach (item in this.getpagecomponents(item)) { try { i.editing.beginedit(); i.publishing.validfrom = item.publishing.validfrom; i.publishing.validto = item.publishing.validto; i.editing.endedit(true, false); } catch(exception ex) { i.editing.canceledit(); } } } } } protected ienumerable<item> getpagecomponents(item page) { var links = page.links.getalllinks(false, true); var foundids = new hashset<id>(); var foundcomponentids = new hashset<id>(); var componentids = new list<id> { page.id }; using (var context = contentsearchmanager.getindex("sitecore_master_index").createsearchcontext()) { while (componentids.any()) { var query = context.getqueryable<linksearchresultitem>(); var predicate = predicatebuilder.false<linksearchresultitem>(); foreach (var id in componentids) { predicate = predicate.or(sri => sri.itemid == id); } query = query.where(predicate); var results = query.getresults().hits.select(h => h.document); foundids.add(componentids); componentids.clear(); componentids.addrange(results .where(sri => (sri.path.startswith("/sitecore/content/becu/global/page components/", stringcomparison.invariantcultureignorecase) || sri.itemid == page.id) && sri.links != null) .selectmany(sri => sri.links) .except(foundids)); foundcomponentids.add(results .where(sri => (sri.path.startswith("/sitecore/content/becu/global/page components/", stringcomparison.invariantcultureignorecase))) .select(sri => sri.itemid)); } } var database = page.database; return foundcomponentids.select(id => database.getitem(id)).where(i => != null); }
i recommend try wrapping edit code sitecore.data.bulkupdatecontext
follows
... using(new sitecore.data.bulkupdatecontext()) { foreach (item in this.getpagecomponents(item)) { try { i.editing.beginedit(); i.publishing.validfrom = item.publishing.validfrom; i.publishing.validto = item.publishing.validto; i.editing.endedit(true, false); } catch(exception ex) { i.editing.canceledit(); } } } ...
when item updated in sitecore, several other background processes , events result of updating item. such example indexing slow down update of large number of items @ once.
the bulkupdatecontext disable of these events , processes until update complete thereby speeding update of items.
note: have yet use bulkupdatecontext myself found several posts including this stackoverflow question claims bulkupdatecontext improves item creation speed, not updates. may apply particular version of sitecore being used @ time. may may no longer case new versions of sitecore (7.x , 8), think still worth try.
Comments
Post a Comment