Copied to clipboard

Flag this post as spam?

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


  • Sander van de Pas 61 posts 121 karma points
    Aug 20, 2013 @ 13:31
    Sander van de Pas
    0

    Umbraco 4.11: After creating Document programmatically, node will not be found

    I want to create an archive dynamically called by an alternate template.

    When the year couldn't be found below the archive node it has to be created. When multiple nodes has the same archive year it has to be created once. But my problem is that when I created the year node for the first time and I'm looking for it for the second node, it won't be found. The archive node doesn't have Children, but they are there for sure.

    Someone else had this problem?
    Using Umbraco 4.11.

    Thanks!

     

    Grtz Sander

    The code:

    private void MoveExpirationItems(int parentId, string docTypeAlias)
            {
                List<Document> itemsToArchive = new List<Document>();
                try
                {
                    // get nodes by id
                    Document root = new Document(parentId);
                    var children = FindChildren(root, d => d.ContentType.Alias == docTypeAlias);
                    Document archive = root.Children.FirstOrDefault(d => d.ContentType.Alias == "Archive");
    
                    foreach (Document doc in children)
                    {
                        DateTime publicationDate;
                        if (!DateTime.TryParse(doc.getProperty("expirationDate").Value.ToString(), out publicationDate))
                            continue;
                        if (publicationDate <= DateTime.Today)
                        {
                            itemsToArchive.Add(doc);
                        }
                    }
    
                    foreach (var document in itemsToArchive)
                    {
                        DateTime publicationDate;
                        if (!DateTime.TryParse(document.getProperty("expirationDate").Value.ToString(), out publicationDate))
                            continue;
    
                        if (archive == null)
                        {
                            archive = Document.MakeNew("Archief", DocumentType.GetByAlias("Archive"), new User(0), parentId);
                            archive.Save();
    
                            Log.Add(LogTypes.Custom, 0, "Archive node added: " + archive.Path);
                        }
                        if (!archive.Published)
                        {
                            archive.Publish(new User(0));
                        }
    
                        Document year = archive.Children.FirstOrDefault(d => d.Text == publicationDate.ToString("yyyy"));
                        if (year == null)
                        {
                            year = Document.MakeNew(publicationDate.ToString("yyyy"), DocumentType.GetByAlias("ArchiveYear"), new User(0), archive.Id);
                            year.Save();
    
                            Log.Add(LogTypes.Custom, 0, "ArchiveYear node added: " + year.Path);
                        }
                        if (!year.Published)
                        {
                            year.Publish(new User(0));
                        }
    
                        Document month = year.Children.FirstOrDefault(d => d.Text == publicationDate.ToString("MMMM"));
                        if (month == null)
                        {
                            month = Document.MakeNew(publicationDate.ToString("MMMM"), DocumentType.GetByAlias("ArchiveMonth"), new User(0), year.Id);
                            month.Save();
    
                            Log.Add(LogTypes.Custom, 0, "ArchiveMonth node added: " + month.Path);
                        }
                        if (!month.Published)
                        {
                            month.Publish(new User(0));
                        }
    
                        Document day = month.Children.FirstOrDefault(d => d.Text == publicationDate.ToString("dd"));
                        if (day == null)
                        {
                            day = Document.MakeNew(publicationDate.ToString("dd"), DocumentType.GetByAlias("ArchiveDay"), new User(0), month.Id);
                            day.Save();
    
                            Log.Add(LogTypes.Custom, 0, "ArchiveDay node added: " + day.Path);
                        }
                        if (!day.Published)
                        {
                            day.Publish(new User(0));
                        }
    
                        //move doc  
                        document.Move(day.Id);
    
                        Log.Add(LogTypes.Custom, 0, "Archive: node moved: " + document.Text + "-" + document.Path);
                    }
                }
                catch (Exception e)
                {
                    Log.Add(LogTypes.Custom, 0, string.Format("Archive {0}: {1}", docTypeAlias, e.Message + e.StackTrace));
                }
                finally
                {
                    library.RefreshContent();
                }
            }
    private static List<Document> FindChildren(Document currentDoc, Func<Document, bool> predicate)
            {
                List<Document> result = new List<Document>();
    
                var docs = currentDoc.Children.OfType<Document>().Where(predicate);
                if (docs.Count() != 0)
                    result.AddRange(docs);
    
                foreach (var child in currentDoc.Children.OfType<Document>())
                {
                    docs = FindChildren(child, predicate);
                    if (docs.Count() != 0)
                        result.AddRange(docs);
                }
                return result;
            }
  • Sander van de Pas 61 posts 121 karma points
    Aug 20, 2013 @ 15:07
    Sander van de Pas
    0

    Shorter version :-)

    Call Archive Doc:
    Document archive = root.Children.FirstOrDefault(d => d.ContentType.Alias == "Archive");

    If archive not exists then add create archive doc:
    archive = Document.MakeNew("Archief", DocumentType.GetByAlias("Archive"), new User(0), parentId);
    archive.Save();

    For each item that should move to archive check his year exsists.
    Document year = archive.Children.FirstOrDefault(d => d.Text == publicationDate.ToString("yyyy")); 

    Otherwise create year doc.
    year = Document.MakeNew(publicationDate.ToString("yyyy"), DocumentType.GetByAlias("ArchiveYear"), new User(0), archive.Id);
    year.Save(); 

    First time he will create it, but for the second time with the same year he will create it too. The archive node has no childs he said, but they are created!

     

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Aug 20, 2013 @ 15:12
    Peter Dijksterhuis
    0

    Not sure if it still applies in this version, but after a save/publish, I tend to do: 

    library.UpdateDocumentCache(document.Id);

    Maybe this helps you as well?

    Peter

  • Charles Afford 1163 posts 1709 karma points
    Aug 21, 2013 @ 19:42
    Charles Afford
    0

    I have seen library.UpdateDocumentCache(document.Id); before i would give that a try :).  And do the refactoring above :)  If you dont understand what is happining with the linq or lambdas then post back :)

  • John French 32 posts 83 karma points
    Oct 24, 2013 @ 12:51
    John French
    0

    Hi,

    Did you fix this problem? I'm having a similar issue where I am adding a new document to a node lets say nodeX but when I do nodeX.Children the node i just created isn't in the list, but in the UI its there!

    cheers

Please Sign in or register to post replies

Write your reply to:

Draft