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 15, 2016 @ 09:55
    Jonathan Ben Avraham
    0

    Importing blog posts and comments into nested content nodes

    I'm creating a package for Umbraco to export and import blogs, I have most of the code working however the nesting on import isn't really working how I want it to.

    this is the code I have

    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.Configuration;
    using System.Xml.Linq;
    using System.Xml;
    
    namespace UmbracoBlogsExportPackage.App_Code
    {
        public class ImportAllBlogsController : UmbracoAuthorizedApiController
        {
            public string basePath;
    
    
            public void LocatePath()
            {
                this.basePath = System.Web.Hosting.HostingEnvironment.MapPath(@"/upload");
            }
    
            [System.Web.Http.HttpPost]
            public void ImportAll()
            {
                // Get HttpContext
                var myContext = Request.TryGetHttpContext();
                if (myContext.Success)
                {
                    // Fetch File from Angular Interface
                    HttpPostedFileBase myFile = myContext.Result.Request.Files["file"];
    
                    //Check if file is valid
                    if (myFile == null)
                    {
                        throw new HttpException("invalid file");
                    }
                    else
                    {
                        // parse the document and sort contents into list
                        XDocument xdoc1 = XDocument.Load(myFile.InputStream);
                        BlogPosts objBlogPosts = new BlogPosts();
                        BlogComment objBlogComments = new BlogComment();
                        List<BlogPosts> lstblogs
                            = (from _blog in xdoc1.Element("BlogPosts").Elements("Blog")
                               select new BlogPosts
                               {
                                   Title = _blog.Element("Title").Value,
                                   BodyText = _blog.Element("BodyText").Value,
                                   PublishDate = _blog.Element("PublishDate").Value,
                                   Author = _blog.Element("Author").Value
                               }).ToList();
    
                         List<BlogComment> lstComments
                            = (from _comm in xdoc1.Element("BlogPosts").Elements("Blog").Elements("Comments")
                               select new BlogComment
                               {
                                   Author = _comm.Element("Author").Value,
                                   Comment = _comm.Element("Comment").Value
                               }).ToList();
    
                        //use ContentService API to create a document for the item 
    
                        var contentService = UmbracoContext.Current.Application.Services.ContentService;
                        var blogNode = Umbraco.TypedContent(1136);
    
    
    
                        foreach (var post in lstblogs)
                        {
                            // Create a document with a title, parentNodeId, node, and userI
                            var newBlog = contentService.CreateContent(post.Title, 1137, "umbNewsItem", 0);
    
                            if (newBlog != null)
                            {
                                // Set values for the document
                                newBlog.SetValue("title", post.Title.ToNullSafeString());
                                newBlog.SetValue("bodyText", post.BodyText.ToNullSafeString());
                                newBlog.SetValue("publishDate", post.PublishDate.ToNullSafeString());
                                newBlog.SetValue("author", post.Author.ToNullSafeString());
                                newBlog.SetValue("image", post.Image);
    
                            }
    
                            // Check if content is null, if so throw exception
                            if (newBlog == null)
                            {
                                throw new Exception("newBlog is null.");
                            }
                            // Check if contentService is null, if so throw exception
                            if (contentService == null)
                            {
                                throw new Exception("contentService is null.");
    
                            }
                            // Check if contentService is specified
                            if(contentService != null)
                            {
                                // Save and publish content
                                contentService.SaveAndPublish(newBlog, 0, true);
                            }
    
                            var parentId = newBlog.Id;
                            var newCommentSection = contentService.CreateContent("Comments", parentId, "comments", 0);
                            contentService.SaveAndPublish(newCommentSection, 0, true);
    
                            foreach (var comm in lstComments)
                            {
                                var commentsId = newCommentSection.Id;
                                var newComment = contentService.CreateContent("Comment", commentsId, "comment", 0);
    
                                if (newComment != null)
                                {
                                    newComment.SetValue("comment", comm.Comment.ToNullSafeString());
                                    newComment.SetValue("author", comm.Author.ToNullSafeString());
                                }
    
                                contentService.SaveAndPublish(newComment, 0, true);
                            }
    
                         }
    
    
                     }   
                 }
             }
        }
    }  
    

    What it's doing is creating the post with the comments, however when there's more than one post, it's adding all the comments to each post. What I want is to add the comments relevant to the individual post on each post.

    here is my xml example:

    <BlogPosts>
    <Blog>
        <Title>This is a new amazing blog title</Title>
        <BodyText>&lt;p&gt;This is more new Random Content&lt;/p&gt;</BodyText>
        <PublishDate>29/09/2036 15:49:47</PublishDate>
        <Author>0</Author>
        <Comments>
        <Author>Genevive La Crosse</Author>
            <Comment>&lt;p&gt;This is a comment&lt;/p&gt;</Comment>
        </Comments>
        <Comments>
        <Author>Gilliandro Du Mere</Author>
            <Comment>&lt;p&gt;This is a comment&lt;/p&gt;</Comment>
        </Comments>
      </Blog>
    <Blog>
        <Title>This is another new amazing blog title</Title>
        <BodyText>&lt;p&gt;This is more new Random Content&lt;/p&gt;</BodyText>
        <PublishDate>29/09/2036 15:49:47</PublishDate>
        <Author>0</Author>
        <Comments>
        <Author>Gerome Littlewood</Author>
            <Comment>&lt;p&gt;This is a comment&lt;/p&gt;</Comment>
        </Comments>
        <Comments>
        <Author>Tatiana Du Mere</Author>
            <Comment>&lt;p&gt;This is a comment&lt;/p&gt;</Comment>
        </Comments>
      </Blog>
    </BlogPosts>
    

    thanks in advance!

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 18, 2016 @ 21:07
    Alex Skrypnyk
    0

    Hi Jonathan,

    Did you solve your issue?

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft