More in this section

Forums / Developing with Sitefinity / Using API to set Tags/Categories and custom field values of a newsItem

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

4 posts, 0 answered
  1. Alex
    Alex avatar
    39 posts
    Registered:
    19 Jul 2010
    12 Dec 2011
    Link to this post
    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();
  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    28 Aug 2017
    13 Dec 2011
    Link to this post
    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
  3. Alex
    Alex avatar
    39 posts
    Registered:
    19 Jul 2010
    13 Dec 2011
    Link to this post
    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;
  4. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    28 Aug 2017
    14 Dec 2011
    Link to this post
    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
4 posts, 0 answered