Using API to set Tags/Categories and custom field values of a newsItem
I'm using the following code to add a NewsItem, but would like to set tags/Categories as well as 2 custom field values at the same time. What do I need to add to this code in order to do this? Thanks!
fluent.NewsItem().CreateNew()
.Do(ni =>
ni.Id = newItemGuid;
ni.Title = this.txtTitle.Text;
ni.Content = this.RadEditorStoryHTML.Content;
ni.Summary = this.txtSummary.Text;
if (txtPublishDate.SelectedDate.HasValue)
ni.PublicationDate = txtPublishDate.SelectedDate.Value;
if (txtUnPublishDate.SelectedDate.HasValue)
ni.ExpirationDate = txtUnPublishDate.SelectedDate.Value;
ni.DateCreated = DateTime.Now;
ni.AllowComments = false;
ni.Visible = true;
)
.Publish()
.SaveChanges();
Hello Alex,
You can use our extension methods GetValue() and SetValue() to get/set your metafileds. Please make sure you're supplying the correct type of data in the fields, though (for example, both Tags and Categories accept a value of type <IList<Guid>>, since they contain a List of the category/tags IDs assigned to the item). I believe you might find my latest reply to this forum post useful. If you need some further assistance, please let us know.
All the best,
Boyan Barnev
the Telerik team
So I was able to get the Categories, but the following code errors because it says there are no GetValue or SetValue methods.
//Set Categories
NewsManager nManager = NewsManager.GetManager();
var newsItems = nManager.GetNewsItem(newItemGuid);
if (!chkCategories.SelectedValue.IsNullOrEmpty())
var catlist = newsItem.GetValue<
TrackedList
<Guid>>("Category");
foreach (ListItem item in chkCategories.Items)
if (item.Selected)
catlist.Add(item.Value);
newsItem.SetValue("Category", catlist);
Hi Alex,
Since GetValue and SetValue are extension methods, VS Intellisense will not automatically detect their namespace. Please add a using statement for Telerik.Sitefinity.Model in your class, this should fix the issues.
Best wishes,
Boyan Barnev
the Telerik team