Copied to clipboard

Flag this post as spam?

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


  • André Lange 108 posts 410 karma points
    Jan 31, 2019 @ 09:58
    André Lange
    0

    Getting content for newsletter from multiple pages.

    Using Umbraco v. 7.12

    Hi, i am trying to create some functionality for a newsletter, which is supposed to be sent out once a week, which would contain everything that has been created in the last week on the site.

    So every news article, event etc. I have a headline, teaser and image on every single document type, which i will use for the content in the newsletter, and ofcourse a url for the page.

    My question is this.

    I have created a surface controller, where i have found my root page, and from there i can look at my content. But if i go .children on the root, i would only be going down 1 level.

    How do i look on my ENTIRE site, and look for everything created in the last x amount of days ?

    i so far have this as far as code goes:

    Other than just saying .children on each and looping through them, is there not a better way ?

      public void InitiateNewsLetter()
        {//http://localhost:50797/Umbraco/Surface/NewsLetterSurface/InitiateNewsLetter
            // this is the point that should be called...
    
            var content = GetContent(-14);
            if (content != null)
            {// there is content, go ahead and create the newsletter.
                var emailList = GetEmailOfMembers();
                if (emailList.Count() > 0)
                {   // there is emails present
                    // create the newsletter... somehow...
    
                }
            }
            else
            {
                //no content to show.. well.... huh..
            }
            // meant as this is new, this is what has happened last week in case you missed it...
            // Very important, if there is no new content, it should not send out anything...!
        }
        public ActionResult GetContent(int days)
        {
            var root = Umbraco.TypedContentAtRoot().First(); // gets the main root of the site.
            var memberRoot = root.DescendantsOrSelf<MemberPage>().FirstOrDefault(); // get the memberpage "root".
    
            //memberRoot.Descendants
    
            var children = memberRoot.Children;
            var date = DateTime.Now.AddDays(days);
    
            var content = children.Where(x => x.CreateDate >= date); // gets content that is older than the set date.
            if (content != null)
            {
                if (content.Count() > 0)
                {
                    // map the content....
    
                    return null;
                }
                else
                {   //No content present, return null.
                    return null;
                }
            }
            else
            {
                return null;
            }
            // get content from site, that has been created in the last 14 days. 
            // It will run once a week
        }
        public IEnumerable<string> GetEmailOfMembers()
        {
            var memberHelper = new UmbracoHelper(UmbracoContext.Current);
            int totalRecords;
    
            var members = Services.MemberService.GetAll(0, 200, out totalRecords).ToList();
    
            var mappedMembers = UserContainerMapper.Map<UserModel>(members, Umbraco);
            // get the email of ALL members, as an ienumerable string, and return that.
            var emailList = mappedMembers.Select(x => x.Email); // should contain all emails needed.
    
            return emailList;
        }
        public void SendMail()
        {
            // send the email to all members
        }
    
  • Koen van Ras 56 posts 361 karma points c-trib
    Jan 31, 2019 @ 12:32
    Koen van Ras
    101

    Isn't .Descendants what you're looking for?

    You should be able to get all the descendants by xpath with the following line of code:

    Umbraco.UmbracoContext.ContentCache.GetByXPath(string.Format("id({0})[@isDoc]/descendant::*[@isDoc]", memberRoot.Id))
    
  • André Lange 108 posts 410 karma points
    Feb 01, 2019 @ 06:59
    André Lange
    0

    It is probably descendants yes.. Hmm. I have never used Xpath before, can you explain it in relation to your example ?

Please Sign in or register to post replies

Write your reply to:

Draft