Using API to set Tags/Categories and custom field values of

Posted by Community Admin on 04-Aug-2018 17:42

Using API to set Tags/Categories and custom field values of a newsItem

All Replies

Posted by Community Admin on 12-Dec-2011 00:00

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();

Posted by Community Admin on 13-Dec-2011 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 13-Dec-2011 00:00

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);
 

Here are my using statements:
       using Telerik.Sitefinity.News.Model;
       using Telerik.Sitefinity;
       using Telerik.Sitefinity.Taxonomies;
       using Telerik.Sitefinity.Taxonomies.Model;
       using Telerik.OpenAccess;
       using Telerik.Sitefinity.Modules.News;

Posted by Community Admin on 14-Dec-2011 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed