Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nigel Wilson 945 posts 2077 karma points
    Apr 26, 2011 @ 01:15
    Nigel Wilson
    0

    ApplicationBase Event Creating Duplicate Nodes

    Hi there

    I have written the following code and setup a scheduled task to run every hour.

    The process performs a SQL query on an external database application and if appropriate creates new nodes within the Umbraco site. The nodes are created and saved, but not published. All nodes created relate to calls logged by our Help Desk.

    I have noticed that (on some occasions) multiple nodes are being created for the same call with each node having a number appended to the end, e.g. 00267239 (2), 00267239 (3), etc.

    This doesn't seem to be occuring all the time and so wonder if it relates to :

    1. Nodes being only saved and not published. As per my code below I am doing an "UpdateDocumentCache" after saving the new node.
    2. The nodes created are being automatically moved into a year/month folder structure so wonder if this is causing the "confusion" within Umbraco

     

    And here is my code (some parts omitted for brevity purposes).

    using System;
    using System.Web;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Xml;
    using umbraco.cms.businesslogic.web;
    using umbraco.BusinessLogic;

    namespace HeatDashboardDatabaseCalls
    {
        public class AddMajorIncidentReportNodes
        {
            public static void CreateNodes()
            {
                SqlConnection dataConnection = new SqlConnection(ConfigurationManager.AppSettings["HEAT-DB"].ToString());
               
                using (dataConnection)
                {
                    dataConnection.Open();
                    using (SqlCommand dataCommand = dataConnection.CreateCommand())
                    {
                        DateTime endDate = DateTime.Now;
                        DateTime startDate = endDate.AddDays(-3);
                        dataCommand.CommandText = "SQL STATEMENT HERE";

                        SqlDataReader dr = dataCommand.ExecuteReader();

                        while (dr.Read())
                        {
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.ContentXML));
                            XmlNodeList xnList = xmlDoc.SelectNodes("//MajorIncidentReport [@nodeName = '" + dr[0].ToString()  + "']");
                            if (xnList.Count == 0)
                            {
                                User author = User.GetUser(0);
                                Document doc = Document.MakeNew(dr[0].ToString(), DocumentType.GetByAlias("MajorIncidentReport"), author, Convert.ToInt32(ConfigurationManager.AppSettings["mirContainerID"].ToString()));
                                doc.getProperty("dateReceived").Value = dr[1].ToString();
                                doc.getProperty("callDescription").Value = dr[2].ToString();
                                doc.getProperty("assignmentGroup").Value = dr[3].ToString();
                                doc.Save();
                                umbraco.library.UpdateDocumentCache(doc.Id);
                                                           
                            }
                        }
                    }
                }
            }
        }
    }

     

    Can anyone shed any light / offer a suggestion as to where it is going wrong ?

    Thanks in advance.

    Nige

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 26, 2011 @ 08:43
    Sebastiaan Janssen
    1

    The node will only get pushed into the documentcache when it is actually published, so yes that is your problem. To check if a document already exists use the document api (be aware that this is an expensive call as it goes to the database, so use it sparingly).

  • Nigel Wilson 945 posts 2077 karma points
    Apr 26, 2011 @ 10:53
    Nigel Wilson
    0

    Hi Sebastiaan - thanks for confirming my suspicions.

    In this particular case I think I will publish the node and alter the logic in my XSLT so as to not display the nodes until they are ready to be displayed - maybe add a true/false field to the document type.

    Thanks again

    Nigel

Please Sign in or register to post replies

Write your reply to:

Draft