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

Forums / Developing with Sitefinity / Create news item with related images from album using API

Create news item with related images from album using API

4 posts, 0 answered
  1. Anju
    Anju avatar
    19 posts
    Registered:
    22 Jul 2014
    06 May 2015
    Link to this post

    Hi All, 

     I was trying to create a news item with related images from the album using native API. I could create a news item and also create a new album and child folders with images using native ​API. Now i am trying to associate the news item created using API and the images created using API. How can i associate the news item and images. I could see an option for related images is available when we create a news item from sitefinity CMS. But how can i add the related images for the news item from code using API. 

     Is that possible and how could I do that ?
    Thanks

    Anju

     

  2. Kaloyan
    Kaloyan avatar
    272 posts
    Registered:
    02 Dec 2016
    11 May 2015
    Link to this post
    Hi Anju,

    You can check the following documentation article where you can find detailed information how to make relations using Sitefinity API:

    Related data API

    Regards,
    Kaloyan
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Anju
    Anju avatar
    19 posts
    Registered:
    22 Jul 2014
    12 May 2015 in reply to Kaloyan
    Link to this post

    Hi Kaloyan,

    Thanks for your response but i tried using related api

    I tried using the following code : 

            public void RelateNewsMedia(Telerik.Sitefinity.Libraries.Model.Image image, Guid newsId)
            {
                NewsManager newsManager = NewsManager.GetManager();
                NewsItem newsItem = newsManager.GetNewsItems().Where(item => item.Id == newsId).FirstOrDefault();

                // create a relation to image. Relation will be available only for the current item state (Master)
                newsItem.CreateRelation(image, "relatedImages");
                newsManager.SaveChanges();

                // on publish, all item's relations are copied from Master to Live state.
                newsManager.Lifecycle.Publish(newsItem);
                newsManager.SaveChanges();

                // get the count of all related data items
                var relatedImagesCount = newsItem.GetRelatedItemsCountByField("relatedImages");

                // get all related images of this news item
                var relatedImages = newsItem.GetRelatedItems("relatedImages");

                newsManager.SaveChanges();
            }

    The code is not throwing any errors but we couldn't find any related media for the news items.

  4. Kaloyan
    Kaloyan avatar
    272 posts
    Registered:
    02 Dec 2016
    15 May 2015
    Link to this post
    Hi Anju,

    Here is a sample that works perfect on my end:

    NewsManager newsManager = NewsManager.GetManager();
                NewsItem newsItem = newsManager.GetNewsItems().Where(item => item.Title == "First").FirstOrDefault();
     
                LibrariesManager libMan = LibrariesManager.GetManager();
                var image = libMan.GetImages().Where(i => i.Title == "sitefinity" && i.Status == ContentLifecycleStatus.Master).FirstOrDefault();
     
                newsItem.CreateRelation(image, "RelatedImage");
                newsManager.SaveChanges();

    Regards,
    Kaloyan
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
4 posts, 0 answered