So you want to export the export blog posts including their data like Title, Body Text, Author, PubDate etc, so you can import it to another Umbraco installation?
The point here is not to use an already built package. This is for a work project, I have to build a bespoke export package which allows the back office user to export/import the blog posts. Another challenge here might be that the package has to be compatible with both an older version of Umbraco where I would be exporting the blog and a newer version of Umbraco where I would be importing the blog.
Here is an small example of how you can get all nodes by a specific contenttype (umbNewsItem), get the properties you want and then return it like a custom list:
public List<BlogPost> GetPostList()
{
var contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("umbNewsItem");
var nodes = ApplicationContext.Current.Services.ContentService.GetContentOfContentType(contentType.Id).Select(content => new Node(content.Id));
return nodes.Select(node => new BlogPost()
{
Id = node.Id,
CustomProperty = node.GetProperty("customPropertyAlias").ToNullSafeString()
}).ToList();
}
I seem to get an compilation error when using this:
Member 'Umbraco.Core.ApplicationContext.Current.get' cannot be accessed with an instance reference; qualify it with a type name instead
So I did this:
public List<BlogPostsList> getPostList()
{
string name = "umbNewsItem";
var contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentOfContentType(string name );
var nodes = ApplicationContext.Current.Services.ContentService.GetContentOfContentType(contentType.Id).Select(content => new Node(content.Id));
return nodes.Select(node => new BlogPostNew()
{
Id = node.Id,
Title = node.GetProperty("Title").ToNullSafeString(),
Content = node.GetProperty("Content").ToNullSafeString(),
Author = node.GetProperty("Author").ToNullSafeString(),
PubDate = node.GetProperty("Publish date").ToNullSafeString()
}).ToList();
}
Because GetContentType isn't a valid method for ContentService.
I am still however getting an error but it's changed:
Did you copy the exact code I posted without any adjustments? Becaus that code works for me on Umbraco 7.4.2 and i get a list of all my blogposts. Maybe it might be a version issue? What version are you using?
Also, note that you are not supposed to create an instance of ApplicationContext, you access directly. The compilation errror you got initially indicates that you have created a instance of ApplicationContext.
CS1061: 'umbraco.interfaces.IProperty' does not contain a definition for 'ToNullSafeString' and no extension method 'ToNullSafeString' accepting a first argument of type 'umbraco.interfaces.IProperty' could be found (are you missing a using directive or an assembly reference?)
Oh, my bad. Im using the AutoMapper.Internal extension method.
Either add the using AutoMapper.Internal (make sure your solution references the AutoMapper.dll) or make sure all the properties is not null before you do ToString():
That's strange. Could you double check that you've given the correct alias for the contentType? I see in your code you wrote "umbNewsItem" and in the post header you wrote "umbNewsItems". Also check that the propertyAliases are correct. Can you debug the code in VS or something to make sure you get the correct nodes?
Hi again Jonathan. Did it work out for you or are you still having problems with this? If so, could you post your code (complete class, not just the method) and maybe i can help you find whats missing.
unfortunately not :( It just runs the script and nothing happens and the tab that open to run the script which downloads the csv file, returns a DNS error.
I now have the publish date working, however it's still not pulling the data for the title or author. I have the alias correct for title, but I'm not sure what the alias for Author is, because it doesn't specifically say.
Figured out: I can use a userSelector datatype and I was missing text in the title section because the page title was being pulled through to the front end as the blog title. So I managed to sort that out, new problem is that my CSV has multiple rows of the same data.
Exporting Blog Posts (umbNewsItem)
Is there any way I can use the ContentService to export blog posts including their data like Title, Body Text, Author, PubDate etc?
Currently I have to go a really round-about way to do this:
This is my controller, you see I have to go into the database and select each column, but the problem here is that each column is in different tables.
Id Umbraco ContentService has a way that would allow me to export this into a CSV or XLS file, that would be amazing to know.
Thanks in advance!
How about using this package for blog
https://our.umbraco.org/projects/starter-kits/smart-blog/
Manish
Hi Jonathan,
So you want to export the export blog posts including their data like Title, Body Text, Author, PubDate etc, so you can import it to another Umbraco installation?
If this the case than you can use the package called CMSImport https://our.umbraco.org/projects/developer-tools/cmsimport/
Another way is to create a package in Umbraco where you includes the document types and the content of the umbNewsItems.
Here is an older blogpost on how to create a package. http://www.uquickstart.com/umbraco-tips-and-tools/package-up-your-umbraco-website but the method is the same in the newer Umbraco installations.
Hope this helps,
-Dennis
The point here is not to use an already built package. This is for a work project, I have to build a bespoke export package which allows the back office user to export/import the blog posts. Another challenge here might be that the package has to be compatible with both an older version of Umbraco where I would be exporting the blog and a newer version of Umbraco where I would be importing the blog.
Here is an small example of how you can get all nodes by a specific contenttype (umbNewsItem), get the properties you want and then return it like a custom list:
The ContentService applies to Umbraco 6.x and newer: https://our.umbraco.org/documentation/reference/management/services/contentservice
I seem to get an compilation error when using this:
So I did this:
Because GetContentType isn't a valid method for ContentService.
I am still however getting an error but it's changed:
No, its a part of ContentTypeService, not ContentService.
ContentTypeService.GetContentType() method is a part of the ContentTypeService and you can pass in either a id (int) or an alias (string) https://our.umbraco.org/documentation/reference/Management/Services/ContentTypeService.
Looking at your code i see you are using the method ContentService.GetContentOfContentType() with a string parameter which is not supported according to https://our.umbraco.org/documentation/reference/Management/Services/ContentService, it should be an integer parameter for the id.
Did you copy the exact code I posted without any adjustments? Becaus that code works for me on Umbraco 7.4.2 and i get a list of all my blogposts. Maybe it might be a version issue? What version are you using?
Also, note that you are not supposed to create an instance of ApplicationContext, you access directly. The compilation errror you got initially indicates that you have created a instance of ApplicationContext.
Best of luck!!
I realized I made a bit of a stuff up with it, not to worry, I sorted that out:
The only issue now is that it's not finding the Node namespace and I've used the using references on the Service pages.
Update
Never mind, I fixed that too, I wasn't
using umbraco.NodeFactory;
It's throwing an issue saying that the object reference is not set to an instance of an object.
So everything is working except that it cant find the Node reference? Sounds close! :) Try add this:
Please let me know if this works out for you!
You're a star! That was an obvious miss there for me, but now I don't understand why this part is telling me that:
Object reference not set to an instance of an object.
Thank you. :)
The GetProperty() method returns a object and if that property is null, the ToString() method will throw a nullreference exception.
Try the ToNullSafeString() instead.
New error:
Thanks :)
Oh, my bad. Im using the AutoMapper.Internal extension method.
Either add the
using AutoMapper.Internal
(make sure your solution references the AutoMapper.dll) or make sure all the properties is not null before you do ToString():Now I have ensured ToNullSafeString is working, for whatever reason it's not fetching the properties...
any ideas?
That's strange. Could you double check that you've given the correct alias for the contentType? I see in your code you wrote "umbNewsItem" and in the post header you wrote "umbNewsItems". Also check that the propertyAliases are correct. Can you debug the code in VS or something to make sure you get the correct nodes?
Hi again Jonathan. Did it work out for you or are you still having problems with this? If so, could you post your code (complete class, not just the method) and maybe i can help you find whats missing.
I'm pretty sure it's umbNewsItem.
When I change it to umbNewsItems it throws me an "object reference not set to an instance of an object" exception on line.
I think it's maybe the property strings which might be wrong?
Could be, have you double checked that the property string aliases are correct?
Checked it all, still pulling through null:
here is my entire class:
I'm not sure what's going on here, now I'm getting an object reference not set to an instance of an object on this line:
It's saying sb is not an instance of an object.
Its probobly value that is null, and when you try to run Replace() on a null object, it throws an exception. Replace it with this:
Hi Jonathan.
Did this work out for you? Did the nullcheck fix the problem?
unfortunately not :( It just runs the script and nothing happens and the tab that open to run the script which downloads the csv file, returns a DNS error.
Yeah it is working if I remove Publish Date. hmm not entirely sure, also the sheet ends up looking like this:
http://pasteboard.co/N046g0E.png
Not sure why it's doing this, it's not finding the title or author data.
Sounds like a formatting error on the publish date.
Did you open this in Visual Studio so that you can step through and debug. I think that will solve a lot of your ploblem.
Sounds like you are close anyway. :)
Best of luck to you!!
Hi Dennis,
I now have the publish date working, however it's still not pulling the data for the title or author. I have the alias correct for title, but I'm not sure what the alias for Author is, because it doesn't specifically say.
Figured out: I can use a userSelector datatype and I was missing text in the title section because the page title was being pulled through to the front end as the blog title. So I managed to sort that out, new problem is that my CSV has multiple rows of the same data.
Thanks, Jonathan
Sorted!
I moved the instance of StringBuilder into the WritePostInfo method.
Thanks, Jonathan
Awsome Jonathan, im happy to hear that it worked out for you!!! :)
Best of luck to you with the rest of this package!!
Have a great day!!
is working on a reply...