Earlier I raised an issue here Event code multiple page publishing failling. The event code is working fine when i publish single or double pages. But i get the below errors when i publish say 10 or 20 or 100 pages together. It is a random behaviour, sometimes 10 pages get published successfully and sometimes not even two pages get published. There are cases where after 10 pages gets failed in publishing, i am unable to publish single page. In such cases i have to restart all the trdion services, restart the cms, COM+ etc. And after i do that, my single page publishing starts working fine.
The list of random errors i get is :
Class does not support Automation or does not support expected interface.
The operation is not valid for the state of the transaction.
The transaction under which this method call was executing was asynchronously aborted.
But all these errors goes away when i restart everything. I believe there can be some issue in the code? Becuase when i switch off the event code those pages are getting published successfully. Below is code which is run for every page publish. So if i select 20 pages and right click and publish, the below code will run for each page. The code access the component of that page, makes a xml node of it, opens the predefined page (.xml) file and appends the xml node to the component of the predefined page and publish the predefined page finally.After the predefined page gets published via event code, I get a .xml file with all the xml nodes, so if there are 10 xml nodes in the file, publishing a page will add 11th node to it. I am using that xml file for my image gallery and news.
public void ProcessPressReleasePageForPublish(Page page, TcmUri pubTargUri)
{
Component compSource = null;
Page pageDestination = null;
Component componentDestinaton = null;
StringBuilder builder = new StringBuilder();
XmlDocument document = new XmlDocument();
string prefixUrl = string.Empty;
Publication publication = (Publication)page.ContextRepository;
try
{
this.eventHelper = new EventHelper();
string str = string.Empty;
compSource = page.ComponentPresentations[0].Component;
builder.AppendFormat("<ID>{0}</ID>", compSource.Id);
ItemFields fieldsMetaCommon = new ItemFields(compSource.Metadata, compSource.MetadataSchema);
KeywordField fieldSection = (KeywordField)fieldsMetaCommon["pr_section"];
IList<Keyword> fieldSectionkeywords = fieldSection.Values;
try
{
//Getting section keywords
string strkeywordText = string.Empty;
if (fieldSectionkeywords != null)
{
foreach (Keyword key in fieldSectionkeywords)
{
builder.AppendFormat("<Section>{0}</Section>", key.Title.ToString());
}
}
}
catch (Exception ex)
{
EventHelper.WriteEventToHTMLLog("ProcessPressReleasePageForPublish - Error in keyword area: " + ex.StackTrace);
}
string strPressTitle = string.Empty;
SingleLineTextField fieldTitle = (SingleLineTextField)fieldsMetaCommon["pr_title"];
if (fieldTitle != null && !string.IsNullOrEmpty(fieldTitle.Value))
{
strPressTitle = Convert.ToString(fieldTitle.Value);
builder.AppendFormat("<Title>{0}</Title>", strPressTitle);
}
string strSummary = string.Empty;
XhtmlField multiLineFieldBody = (XhtmlField)fieldsMetaCommon["Summary"];
if (multiLineFieldBody != null && !string.IsNullOrEmpty(multiLineFieldBody.Value))
{
strSummary = Convert.ToString(multiLineFieldBody.Value);
builder.AppendFormat("<Summary><![CDATA[{0}]]></Summary>", strSummary);
}
DateField dateFieldPublishDate = (DateField)fieldsMetaCommon["pr_date"];
if (dateFieldPublishDate != null && dateFieldPublishDate.Value != null)
{
string pubDateFormat = string.Empty;
try
{
pubDateFormat = dateFieldPublishDate.Value.GetDateTimeFormats('s')[0];
builder.AppendFormat("<Date>{0}</Date>", pubDateFormat);
}
catch (Exception)
{ EventHelper.WriteEventToHTMLLog("ProcessPressReleasePageForPublish: ERROR - Date issue"); }
}
builder.AppendFormat("<pagetcm>{0}</pagetcm>", page.Id);
ComponentLinkField linkFieldCaseStudyThumb = fieldsMetaCommon["Thumbnail"] as ComponentLinkField;
if (linkFieldCaseStudyThumb != null && linkFieldCaseStudyThumb.Value != null && !string.IsNullOrEmpty(linkFieldCaseStudyThumb.Value.ToString()))
{
Component tempComponent = linkFieldCaseStudyThumb.Value; //(or linkField.Values[0]
builder.AppendFormat("<Thumbnail>{0}</Thumbnail>", publication.MultimediaUrl + "/" + this.GetImagePublishPath(tempComponent.BinaryContent.Filename, tempComponent.Id));
}
builder.AppendFormat("<url>{0}</url>", this.eventHelper.EscapeXMLText(page.PublishLocationUrl));
string strURI;
strURI = this.eventHelper.Value("PressReleaseXMLPage");
pageDestination = (Page)publication.GetObject(strURI);
componentDestinaton = pageDestination.ComponentPresentations[0].Component;
ItemFields fieldsNewCommon = new ItemFields(componentDestinaton.Content, componentDestinaton.Schema);
MultiLineTextField multiLineFieldXmlData = (MultiLineTextField)fieldsNewCommon["XMLdata"];
if (multiLineFieldXmlData != null && !string.IsNullOrEmpty(multiLineFieldXmlData.Value))
{
document.LoadXml(multiLineFieldXmlData.Value.ToString().Trim());
XmlNode oldChild = document.SelectSingleNode("descendant::PressRelease[ID='" + compSource.Id + "']");
if (oldChild != null)
{
oldChild.ParentNode.RemoveChild(oldChild);
}
XmlNode nodePressReleases = document.SelectSingleNode("PressReleases");
XmlElement newChild = null;
newChild = document.CreateElement("PressRelease");
newChild.InnerXml = builder.ToString();
nodePressReleases.AppendChild(newChild);
str = document.InnerXml.ToString();
oldChild = null;
newChild = null;
}
else
{
str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<pressreleases><pressrelease>" + builder.ToString() + "</pressrelease></pressreleases>";
}
if (componentDestinaton.IsShared)
{
componentDestinaton.Localize();
}
if (componentDestinaton.IsCheckedOut)
{
componentDestinaton.CheckIn(true);
}
componentDestinaton.CheckOut(true);
XmlNamespaceManager xMan = new XmlNamespaceManager(componentDestinaton.Content.OwnerDocument.NameTable);
xMan.AddNamespace("ns", componentDestinaton.Content.NamespaceURI);
if (componentDestinaton.Content.SelectSingleNode("/ns:Content/ns:XMLdata", xMan) != null)
{
componentDestinaton.Content.SelectSingleNode("/ns:Content/ns:XMLdata", xMan).InnerText = str;
}
else
{
EventHelper.WriteEventToHTMLLog("ProcessPressReleasePageForPublish : ERROR- Final Update Fail");
}
bool isCheckOut = componentDestinaton.TryCheckOut();
try
{
componentDestinaton.Save(true);
}
catch (Exception ex)
{
EventHelper.WriteEventToHTMLLog("ProcessPressReleasePageForPublish - Save Error - " + ex.Message + "-->" + ex.StackTrace);
}
if (componentDestinaton.IsCheckedOut)
{
componentDestinaton.CheckIn(true);
}
List<IdentifiableObject> items = new List<IdentifiableObject>();
items.Add(pageDestination);
List<TargetType> targets = new List<TargetType> { new TargetType(pubTargUri, pageDestination.Session) };
PublishInstruction publishInstruction = new PublishInstruction(pageDestination.Session);
PublishEngine.Publish(items, publishInstruction, targets, PublishPriority.Normal);
}
catch (Exception exception)
{
EventHelper.WriteEventToHTMLLog("ProcessPressReleasePageForPublish: Error -" + exception.Message + "--> StackTrace-" + exception.StackTrace);
}
finally
{
builder = null;
document = null;
// compSource = null;
// pageDestination = null;
// componentDestinaton = null;
}
}
Do you guys see any issue in the code? Or do you think i can improve it especially becuase of the scenario i have here.