Skip to content

Instantly share code, notes, and snippets.

@jonathanread
Created December 1, 2021 18:21
Show Gist options
  • Save jonathanread/cb2ea58ecf96d43c5ffa0f6b30e8af14 to your computer and use it in GitHub Desktop.
Save jonathanread/cb2ea58ecf96d43c5ffa0f6b30e8af14 to your computer and use it in GitHub Desktop.
Get related news articles by categories.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.ContentLocations;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Services;
namespace CLane.Helpers
{
public static class ContentLocationHelper
{
/// <summary>
/// Retrives the page url of where the content item exists
/// </summary>
/// <param name="content">Content Item</param>
/// <returns>Url of page where content item is publish. This does not return the conent items default location.</returns>
public static string GetDefaultPageUrl(IDataItem content)
{
string url = string.Empty;
IContentItemLocation itemLocation = GetItemLocation(content);
if (itemLocation != null)
{
url = PagesHelper.GetPageUrlById(itemLocation.PageId);
}
return url;
}
/// <summary>
/// Gets content items default location
/// </summary>
/// <param name="content">Conent item</param>
/// <returns>Content item location object</returns>
private static IContentItemLocation GetItemLocation(IDataItem content)
{
var locationsService = SystemManager.GetContentLocationService();
var itemLocation = locationsService.GetItemDefaultLocation(content.GetType(),content.Provider.ToString(), content.Id);
return itemLocation;
}
/// <summary>
/// Get content items full url path
/// </summary>
/// <param name="content">Content item</param>
/// <returns>Full url path to conent item</returns>
public static string GetContentItemFullUrl(IDataItem content)
{
string url = string.Empty;
IContentItemLocation itemLocation = GetItemLocation(content);
if (itemLocation != null)
{
url = itemLocation.ItemAbsoluteUrl;
}
return url;
}
}
}
@{ List<NewsItem> relatedNews = NewsHelper.GetRelatedNewsByTagsCategories((Model.Item.DataItem as NewsItem), Model.Item.Fields.Tags, Model.Item.Fields.Category);}
@if (relatedNews != null && relatedNews.Count() > 0)
{
<h3 class="pt-3 mb-4">You Might Also Like</h3>
<div class="row mt-3 news2Col border-bottom border-gray-600 pb-4 mb-4">
@foreach (var item in relatedNews)
{
var navigateUrl = CLane.Helpers.ContentLocationHelper.GetContentItemFullUrl(item);
<article class="col-md-6 newsItemCol card card-no-border">
<div class="newsGraphic card-img-top">
<a href="@navigateUrl"><i class="far fa-newspaper fa-12x"></i></a>
</div>
<div class="d-flex mt-2 mb-3">
<div class="text-muted">
<i class="far fa-calendar-alt"></i>
<time pubdate="pubdate">
@item.PublicationDate.ToString("MMM d, yyyy")
</time>
</div>
<div class="ml-auto">
@Html.CommentsCount(navigateUrl, item)
</div>
</div>
<h4>
<a class="link-dark" href="@navigateUrl">@item.Title</a>
</h4>
<div class="Col2Summary mt-3">
@Html.HtmlSanitize(item.Summary)
</div>
</article>
}
</div>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.OpenAccess;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
namespace CLane.Helpers
{
public static class NewsHelper
{
/// <summary>
/// Gets other news items that have the same categories/tags
/// </summary>
/// <param name="currentNewsItem">Current news item</param>
/// <param name="tagIds">TrackedList of Guids tags</param>
/// <param name="categoryIds">TrackedList of Guids categories</param>
/// <param name="take">How many results to return</param>
/// <returns>List of news items</returns>
public static List<NewsItem> GetRelatedNewsByTagsCategories(this IDataItem currentNewsItem, object tagIds, object categoryIds, int take = 2)
{
newsManager = NewsManager.GetManager();
if(tagIds != null || categoryIds != null)
{
var listTagIds = tagIds as TrackedList<Guid>;
var listCategoryIds = categoryIds as TrackedList<Guid>;
return newsManager.GetNewsItems()
.AsEnumerable()
.OrderBy(n => Guid.NewGuid())//Order randomly
.Where(n => n.Status == ContentLifecycleStatus.Live && n.Visible && n.Id != currentNewsItem.Id)
.ToList()//Had to ToList as Intersect would not work on the server side
.Where(n => n.GetValue<IList<Guid>>("Tags").Intersect(listTagIds).Any() || //Filter News by Category or Tag
n.GetValue<IList<Guid>>("Category").Intersect(listCategoryIds).Any())
.Take(take)
.OrderByDescending(n => n.LastModified)
.ToList();
}
return null;
}
private static NewsManager newsManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment