Publishing Content Programmatically
I am trying to create and publish content items programmatically and have based most of the code on your blog post at http://www.sitefinity.com/blogs/slavoingilizov/posts/11-09-28/how_to_publish_content_items_in_the_past.aspx. However, when it runs the code it does not "Publish" the item. It shows as a "draft" instead of "published in the admin for News items. Any ideas?
Here is a snippet of my code: (Publication date will always have a date in this scenario and I have verified that a date is being passed in)
NewsManager nManager = NewsManager.GetManager();
var newsItem = nManager.CreateNewsItem();
newsItem.Title = this.txtTitle.Text;
newsItem.Content = this.RadEditorStoryHTML.Content;
newsItem.Summary = this.txtSummary.Text;
newsItem.AllowComments = false;
newsItem.Visible = true;
string cloudFileLocation = "ims_media/";
if (txtPublishDate.SelectedDate.HasValue)
newsItem.PublicationDate = txtPublishDate.SelectedDate.Value;
if (txtUnPublishDate.SelectedDate.HasValue)
newsItem.ExpirationDate = txtUnPublishDate.SelectedDate.Value;
//Set Categories
var catlist = newsItem.GetValue<
TrackedList
<Guid>>("Category");
string selectedCategory = Request.Form["C001$radioCategories"];
if (!selectedCategory.IsNullOrEmpty())
ListItem item = radioCategories.Items.FindByValue(selectedCategory);
catlist.Add(Guid.Parse(selectedCategory));
cloudFileLocation += item.Text.ToLower().Replace(" ", "_") + "/";
newsItem.SetValue("Category", catlist);
nManager.RecompileItemUrls(newsItem);
nManager.Lifecycle.PublishWithSpecificDate(newsItem, newsItem.PublicationDate);
nManager.SaveChanges();
Hello Alex,
We have replied to you in the support thread you had open for the same issue, you can check our response there. For your convenience, please find below a copy of our reply:
"Actually if you want to Schedule the item for publishing/unpublishing you might need to alter the code a little bit to involve WorkflowManager and use the MessageWorkflow() method with parameter "Schedule". Please find below a slightly modified sample demonstrating how you can achieve this (I have omitted several parts of the code for the sake of testing, but they should not interfere with the functionality so you can add them back). The important changes have been commented for your convenience:
public
const
string
UrlNameCharsToReplace = @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
;
public
const
string
UrlNameReplaceString =
"-"
;
protected
void
Page_Load(
object
sender, EventArgs e)
NewsManager nManager = NewsManager.GetManager();
var newsItem = nManager.CreateNewsItem();
newsItem.Title =
"News Article 4"
;
newsItem.Content =
"Some test content, created though code"
;
newsItem.Summary =
"Summary"
;
newsItem.AllowComments =
false
;
newsItem.Visible =
true
;
//You need to set the UrlName and replace any special characters, the manager will take care of recompiling the item URL
newsItem.UrlName = Regex.Replace(newsItem.Title.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);
newsItem.PublicationDate = DateTime.UtcNow;
newsItem.ExpirationDate = DateTime.UtcNow.AddDays(+2);
//The item needs to be checked out so we can pass it to WorkflowManager for scheduling
nManager.CheckOut(newsItem);
nManager.SaveChanges();
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(NewsItem).FullName);
WorkflowManager.MessageWorkflow(newsItem.Id,
typeof
(NewsItem),
null
,
"Schedule"
,
true
, bag);
Hi ,
i have error when i try to set the News item Catefories.
Error :
Telerik.sitefinity.News.Model.NewsItem does not contaion a definition for 'GetValue' and no extension method ....etc please check the attached file/
//Set Categories
var catlist = newsItem.GetValue<TrackedList<Guid>>("Category");
Hi,
It's a using problem.
Add this one : using Telerik.Sitefinity.Model;
thank you nicolas but it does not work :(
Hi,
With this using you probably doesn't have the same error. On previous one, GetValue() wasn't recognized, it needs a specific using.
You are correct it is working fine now thank you :)
How to unpublish the blog post item directly?