Copied to clipboard

Flag this post as spam?

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


  • Jonathan Ben Avraham 43 posts 216 karma points
    Jun 08, 2016 @ 12:28
    Jonathan Ben Avraham
    0

    Trying to grab and export IContent to XML using Linq and Umbraco ContentService

    Hi All,

    I'll start by explaining what I would like to achieve:

    I would like to grab content from umbraco (Blog posts and comments) and store them in an XML which looks like this:

    Please note: BlogPosts and Comments are separate DocumentTypes.

    <BlogPosts>
      <Blog>
        <Title>This is a blog post</Title>
        <BodyText>&lt;p&gt;This is a second blog post's content&lt;/p&gt;</BodyText>
        <PublishDate>06/06/2016 00:00:00</PublishDate>
        <Author>0</Author>
        <Image>1117</Image>
        <Comments>
            <Author>John Smith</Author>
            <Comment>This is an awesome comment</Comment>
        </Comments>
      </Blog>
    </BlogPosts>
    

    This is what I have got so far:

        namespace UmbracoBlogsExportPackage.App_Code
    {
        public class ExportAllBlogsController : UmbracoAuthorizedApiController
        {
            [System.Web.Http.AcceptVerbs("GET", "POST")]
            public void ExportAll()
            {
                List<BlogPosts> BlogPostList = new List<BlogPosts>();
                BlogPostList = getPostList();
                //List<BlogComment> blogCommentList = new List<BlogComment>();
                //blogCommentList = getCommentList();
    
                string attachment = "attachment; filename= ExpBlogPosts.xml;";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.AddHeader("Content-Disposition", attachment);
                HttpContext.Current.Response.ContentType = "text/xml";
                HttpContext.Current.Response.AddHeader("Pragma", "public");
                HttpContext.Current.Response.CacheControl = "private";
    
                XDocument xdoc = new XDocument();
                    foreach (var index in BlogPostList)
                    {
                            var xEle = new XElement("BlogPosts",
                                from blog in BlogPostList
                                where blog.Title != null
                                where blog.BodyText != null
                                where blog.Author != null
                                where blog.Image != null
                                where blog.PublishDate != null
                                select new XElement("Blog",
                                                new XElement("Title", blog.Title),
                                                new XElement("BodyText", blog.BodyText),
                                                new XElement("PublishDate", blog.PublishDate),
                                                new XElement("Author", blog.Author),
                                                new XElement("Image", blog.Image),
                                                new XElement("Comments", blog.Comments)
                                                //from comment in blogCommentList
                                                //select new XElement("Comments",
                                                //    new XElement("Author", comment.Author),
                                                //    new XElement("Comment", comment.Comment)
                                                //    )
                                                )
                            );
    
                           xdoc.Add(xEle);
                           HttpContext.Current.Response.Write(xdoc);
                           HttpContext.Current.Response.End();
                           HttpContext.Current.Response.Flush();
                    }
    
    
            }
    
            public List<BlogPosts> getPostList()
            {
                var contentType = UmbracoContext.Current.Application.Services.ContentTypeService
                    .GetContentType("umbNewsItem");
                var contentService = UmbracoContext.Current.Application.Services.ContentService;
                var nodes = contentService.GetContentOfContentType(contentType.Id);
    
                return nodes.Select(node => new BlogPosts()
                {
                    Title = node.GetValue("title").ToNullSafeString(),
                    BodyText = node.GetValue("bodyText").ToNullSafeString(),
                    PublishDate = node.GetValue("publishDate").ToNullSafeString(),
                    Author = node.GetValue("author").ToNullSafeString(),
                    Image = node.GetValue("image").ToNullSafeString(),
                    Comments = getCommentList(node.Id)
                }).ToList();
            }
    
            public List<BlogComment> getCommentList(int nodeid)
            {
                var contentType = UmbracoContext.Current.Application.Services.ContentTypeService.GetContentType("comments");
                var contentService = UmbracoContext.Current.Application.Services.ContentService;
                var x = contentService.GetChildren(nodeid);
                var nodes = contentService.GetContentOfContentType(contentType.Id);
    
                return nodes.Select(node => new BlogComment()
                    {
                        Author = node.GetValue("author").ToNullSafeString(),
                        Comment = node.GetValue("comment").ToNullSafeString()
                    }).ToList();
            }
        }
    }
    

    Currently I get this output:

    <BlogPosts>
      <Blog>
        <Title>Random Blog Post</Title>
        <BodyText>&lt;p&gt;This is a new random blog post's content&lt;/p&gt;</BodyText>
        <PublishDate>10/06/2016 00:00:00</PublishDate>
        <Author>0</Author>
        <Image>1114</Image>
        <Comments>uBlogsyUmbracoExport.Models.BlogCommentuBlogsyUmbracoExport.Models.BlogCommentuBlogsyUmbracoExport.Models.BlogCommentuBlogsyUmbracoExport.Models.BlogCommentuBlogsyUmbracoExport.Models.BlogComment</Comments>
      </Blog>
      <Blog>
        <Title>This is a second blog post</Title>
        <BodyText>&lt;p&gt;This is a second blog post's content&lt;/p&gt;</BodyText>
        <PublishDate>06/06/2016 00:00:00</PublishDate>
        <Author>0</Author>
        <Image>1117</Image>
        <Comments>uBlogsyUmbracoExport.Models.BlogCommentuBlogsyUmbracoExport.Models.BlogCommentuBlogsyUmbracoExport.Models.BlogCommentuBlogsyUmbracoExport.Models.BlogCommentuBlogsyUmbracoExport.Models.BlogComment</Comments>
      </Blog>
    </BlogPosts>
    

    That's the first issue. The second is the fact that I have more than these two posts. I actually have 5 posts, yet only two are being pulled through.

  • Jonathan Ben Avraham 43 posts 216 karma points
    Jun 14, 2016 @ 14:43
    Jonathan Ben Avraham
    100

    Here was my solution:

    using uBlogsyUmbracoExport.Models;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using Umbraco.Core.Persistence;
    using Umbraco.Web;
    using Umbraco.Web.WebApi;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using umbraco.NodeFactory;
    using AutoMapper.Internal;
    using System.Xml.Linq;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    
    namespace UmbracoBlogsExportPackage.App_Code
    {
        public class ExportAllBlogsController : UmbracoAuthorizedApiController
        {
            [System.Web.Http.AcceptVerbs("GET", "POST")]
            public void ExportAll()
            {
                List<BlogPosts> BlogPostList = new List<BlogPosts>();
                BlogPostList = getPostList();
    
                string attachment = "attachment; filename= ExpBlogPosts.xml;";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.AddHeader("Content-Disposition", attachment);
                HttpContext.Current.Response.ContentType = "text/xml";
                HttpContext.Current.Response.AddHeader("Pragma", "public");
                HttpContext.Current.Response.CacheControl = "private";
    
                XDocument xdoc = new XDocument();
                var xEle = new XElement("BlogPosts",
                    from blog in BlogPostList
                    select new XElement("Blog",
                                    new XElement("Title", blog.Title),
                                    new XElement("BodyText", blog.BodyText),
                                    new XElement("PublishDate", blog.PublishDate),
                                    new XElement("Author", blog.Author),
                                    new XElement("Image", blog.Image),
                                    from comment in blog.Comments
                                    select new XElement("Comments",
                                        new XElement("Author", comment.Author),
                                        new XElement("Comment", comment.Comment)
                                        )
                                    )
                );
                xdoc.Add(xEle);
                HttpContext.Current.Response.Write(xdoc);
                HttpContext.Current.Response.End();
                HttpContext.Current.Response.Flush();
            }
    
            public List<BlogPosts> getPostList()
            {
                var contentType = UmbracoContext.Current.Application.Services.ContentTypeService
                    .GetContentType("umbNewsItem");
                var contentService = UmbracoContext.Current.Application.Services.ContentService;
                var nodes = contentService.GetContentOfContentType(contentType.Id);
    
                return nodes.Select(node => new BlogPosts()
                {
                    Title = node.GetValue("title").ToNullSafeString(),
                    BodyText = node.GetValue("bodyText").ToNullSafeString(),
                    PublishDate = node.GetValue("publishDate").ToNullSafeString(),
                    Author = node.GetValue("author").ToNullSafeString(),
                    Image = node.GetValue("image").ToNullSafeString(),
                    Comments = getCommentList(node.Id)
                }).ToList();
            }
    
            public List<BlogComment> getCommentList(int blogPostId)
            {
                var comments = UmbracoContext.Current.Application.Services.ContentService.GetById(blogPostId).Descendants().Where(n=>n.ContentType.Name == "comment");
    
                return comments.Select(node => new BlogComment()
                    {
                        Author = node.GetValue("author").ToNullSafeString(),
                        Comment = node.GetValue("comment").ToNullSafeString()
                    }).ToList();
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft