Importing Blog Posts - Unsupported Language ""
I am migrating an existing blog to Sitefinity using the Fluent API and when I run the following code I get an exception - "Additional information: Unsupported language "". If you want to support this language, please configure your application accordingly."
01.
using
System;
02.
using
System.Linq;
03.
using
Telerik.Sitefinity;
04.
using
System.Xml.Linq;
05.
using
System.Text.RegularExpressions;
06.
using
System.Collections.Generic;
07.
using
Telerik.Sitefinity.Workflow;
08.
using
Telerik.Sitefinity.Blogs.Model;
09.
using
System.Globalization;
10.
using
System.Threading;
11.
12.
namespace
SitefinityWebApp
13.
14.
public
partial
class
BlogImport : System.Web.UI.Page
15.
16.
protected
void
Page_Load(
object
sender, EventArgs e)
17.
18.
var blogRSS = XDocument.Load(
" OUR RSS FEED "
);
19.
20.
BlogPost blogPost =
null
;
21.
22.
var currentUICulture = CultureInfo.CurrentUICulture;
23.
Thread.CurrentThread.CurrentUICulture =
new
CultureInfo(currentUICulture.Name);
24.
25.
var rssItems = from d
in
blogRSS.Descendants(
"item"
)
26.
select d;
27.
28.
using
(var api = App.WorkWith())
29.
30.
var blog = (from b
in
api.Blogs().Get()
31.
where b.Title ==
"Blog"
32.
select b).FirstOrDefault();
33.
34.
foreach
(var item
in
rssItems.ToList())
35.
36.
blogPost =
new
BlogPost();
37.
blogPost.Title.SetString(Thread.CurrentThread.CurrentUICulture, item.Element(
"title"
).Value);
38.
CreateBlogPost(blog.Id, blogPost);
39.
40.
41.
42.
43.
private
void
CreateBlogPost(Guid parentBlogId, BlogPost blogPost)
44.
45.
var count = 0;
46.
Guid masterBlogPostId = Guid.NewGuid();
47.
48.
App.WorkWith().Blogs().Where(b => b.Id == parentBlogId).Count(
out
count);
49.
50.
if
(count > 0)
51.
52.
App.WorkWith().Blog(parentBlogId).CreateBlogPost()
53.
.Do(p =>
54.
55.
masterBlogPostId = blogPost.Id;
56.
p.Title = blogPost.Title;
57.
p.DateCreated = DateTime.UtcNow;
58.
p.PublicationDate = DateTime.UtcNow;
59.
p.LastModified = DateTime.UtcNow;
60.
p.Content = blogPost.Content;
61.
p.UrlName = Regex.Replace(blogPost.Title.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
62.
).SaveChanges();
63.
64.
var bag =
new
Dictionary<
string
,
string
>();
65.
bag.Add(
"ContentType"
,
typeof
(BlogPost).FullName);
66.
WorkflowManager.MessageWorkflow(masterBlogPostId,
typeof
(BlogPost),
null
,
"Publish"
,
false
, bag);
67.
68.
69.
70.
Hello Neil,
You can create a blog post using the native or fluent API. You can read more about the two approaches in this documentation article.
So according to the documentation here is how you have to change your code in order to get it working:
using
System;
using
System.Linq;
using
Telerik.Sitefinity;
using
System.Xml.Linq;
using
System.Text.RegularExpressions;
using
System.Collections.Generic;
using
Telerik.Sitefinity.Workflow;
using
Telerik.Sitefinity.Blogs.Model;
using
System.Globalization;
using
System.Threading;
namespace
SitefinityWebApp
public
partial
class
BlogImport : System.Web.UI.Page
protected
void
Page_Load(
object
sender, EventArgs e)
var blogRSS = XDocument.Load(
" OUR RSS FEED "
);
BlogPost blogPost =
null
;
var currentUICulture = CultureInfo.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture =
new
CultureInfo(currentUICulture.Name);
var rssItems = from d
in
blogRSS.Descendants(
"item"
)
select d;
using
(var api = App.WorkWith())
var blog = (from b
in
api.Blogs().Get()
where b.Title ==
"Blog"
select b).FirstOrDefault();
foreach
(var item
in
rssItems.ToList())
var blogPostTitle = item.Element(
"title"
).Value;
CreateBlogPost(blog.Id, blogPostTitle);
private
void
CreateBlogPost(Guid parentBlogId,
string
blogPostTitle)
var count = 0;
Guid masterBlogPostId = Guid.NewGuid();
App.WorkWith().Blogs().Where(b => b.Id == parentBlogId).Count(
out
count);
if
(count > 0)
App.WorkWith().Blog(parentBlogId).CreateBlogPost()
.Do(p =>
masterBlogPostId = p.Id;
p.Title = blogPostTitle;
p.DateCreated = DateTime.UtcNow;
p.PublicationDate = DateTime.UtcNow;
p.LastModified = DateTime.UtcNow;
p.Content = blogPostContent;
// add some content
p.UrlName = Regex.Replace(blogPostTitle.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
).SaveChanges();
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(BlogPost).FullName);
WorkflowManager.MessageWorkflow(masterBlogPostId,
typeof
(BlogPost),
null
,
"Publish"
,
false
, bag);
Great, thanks!
On related note I added a DateTime publishedDate parameter to the CreateBlogPost method and send it in using var publishedDate = DateTime.Parse(item.Element("pubDate").Value); in the fluent API call I am setting p.DateCreated, p.PublicationDate and p.LastModified to this publishedDate parameter but it is being persisted to the database as the current date time
Hello Neil,
Sitefinity sets the DateCreated/LastModified automatically to the current date once the item is created and this cannot be further modified. Sitefinity sets it automatically to current date once the item is created independent of the value we have set in code and then updates the date every time the item is modified. This date cannot be modified to a date in the past.
This is the behavior by design in order to allow the admin users to track when an item has been created and when this item has been last modified in Sitefinity.
What I can suggest as a workaround is to create a custom field for the Blog post items and specify it to be of type Date and Time. Then when creating the blog post through the API, you can set the value of this custom field to be the DateCreated date of the original blog post.
Regards,
Nadezhda Petrova
Telerik