+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / News API adding Category and Tags

News API adding Category and Tags

5 posts, 1 answered
  1. David Pearson
    David Pearson avatar
    54 posts
    Registered:
    17 Jul 2012
    12 Oct 2010
    Link to this post
    Sitefinity 3.7 question.

    I am coding a import program to import rss news feed into the News Module.  I am able to insert the title, summary, and content using the News API.  The Rss feeds comes with a Category for each news article.

    I found some info on Category API, and how to insert a Category, and figure out how to tell if the category exist before inserting. 

    How do you assoicate a news article to a category.
        
        newsContent.SetMetaData("Category","some cat");

    Will this automatically associate the "some cat" with the Category dropdown in the news module?
    Same question for tags. 

    Thanks,
    David
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 Oct 2010
    Link to this post
    Hi David,

    Category is a metakey, so you are correctly using SetMetaData method of the IContent object. The CategoriesField control should be automatically populated with the value set for a category.
    You can use the methods below to save a tag to a given content item. The difference here is that you can save multiple tags to a given content item

    ContentManager methods

    SaveTags(Guid contentId, List<string> tags)  - Saves tags for a particular content. The first parameter is the primary key of the content for which tags are being saved. The second parameter is List of tag names to be saved
    SaveTags(Guid contentId, List<string> tags, string owner) - Create/Updates Tags and assotiates them using TaggedContent, Optionally, owner is used to , know "My tags" list


    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  3. David Pearson
    David Pearson avatar
    54 posts
    Registered:
    17 Jul 2012
    12 Oct 2010
    Link to this post
    The CategoriesField control does not automatically populated with 'cat'.   The dropdown is not showing 'cat'. 

    NewsManager newsManager = new NewsManager("News");
           IContent newsContent = newsManager.Content.CreateContent("text/html");
           newsContent.Content = "My first News Item";
           newsContent.SetMetaData("Summary", "This is a news summary");
           newsContent.SetMetaData("Title","News 1");
           newsContent.SetMetaData("Category", "cat");
           newsManager.Content.SaveContent(newsContent);

    I am using sitefinity 3.7.2057.2:1.  Maybe this is something that is corrected in SP3 or 4, if my code is correct above.  It is storing the 'cat' from looking at the preview view before entering the edit page.  The dropdown is not showing cat.

    Thanks,
    David
  4. David Pearson
    David Pearson avatar
    54 posts
    Registered:
    17 Jul 2012
    12 Oct 2010
    Link to this post
    I just figure it out.  I still need to add the category to the sf_Category table.  For anybody else here is my solutions.

    Contentmanager manager = new ContentManager("News");
    ICategory cat = manager.CreateCategory();
    cat.CategoryName = "cat";  // my test value to insert into the dropdown of news
    manager.SaveCategory(cat);

    You can not do this in the interface:
    cat.Application = "News";

    This must be done in the Contentmanager object.

    fun fun.........
    David.  :)
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 Oct 2010
    Link to this post
    Hello David,

    The code below sets the category and populates CategoryFields control properly. Sample video is also attached.

    var newsManager = new NewsManager("News");
     var cnt = newsManager.Content.CreateContent("text/html");
     cnt.SetMetaData("Title", "MyNewNewsItem");
     cnt.Content = "some new content";
     cnt.SetMetaData("Category", "a");
     cnt.SetMetaData("Publication_Date", DateTime.Now);
     cnt.SetMetaData("Expiration_Date", DateTime.Now.AddDays(10));
     newsManager.Content.SaveContent(cnt);


    ContentManger is the base class, so basically when you save a content item you call SaveContent from ContentManager class.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
5 posts, 1 answered