Create news item with related images from album using API

Posted by Community Admin on 04-Aug-2018 07:00

Create news item with related images from album using API

All Replies

Posted by Community Admin on 06-May-2015 00:00

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

 

Posted by Community Admin on 11-May-2015 00:00

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
 

Posted by Community Admin on 12-May-2015 00:00

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.

Posted by Community Admin on 15-May-2015 00:00

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
 

This thread is closed