Last active
December 1, 2021 17:49
-
-
Save jonathanread/61a9e710f569753b98d7 to your computer and use it in GitHub Desktop.
Helper class for incrementing the ViewsCount property of News & Blogs in Sitefinity. This allows one increment per article or post per Session. Also disables incrementing during editing, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Control Language="C#" %> | |
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> | |
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %> | |
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %> | |
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" | |
Assembly="Telerik.Sitefinity" %> | |
<%@ Register Assembly="Telerik.Sitefinity.ContentModules" Namespace="Telerik.Sitefinity.Modules.News.Web.UI" TagPrefix="sf" %> | |
<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" TagPrefix="sf" %> | |
<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Modules.Comments.Web.UI.Frontend" TagPrefix="comments" %> | |
<%@ Import Namespace="Telerik.Sitefinity.Web.UI" %> | |
<%@ Import Namespace="Telerik.Sitefinity.Modules.Comments" %> | |
<sf:SitefinityLabel id="title" runat="server" WrapperTagName="div" HideIfNoText="true" HideIfNoTextMode="Server" /> | |
<telerik:RadListView ID="DetailsView" ItemPlaceholderID="ItemContainer" AllowPaging="False" | |
runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false"> | |
<layouttemplate> | |
<%-- <div class="sfnewsLinksWrp"> | |
<sf:MasterViewHyperLink class="sfnewsBack sfback" Text="<%$ Resources:NewsResources, AllNews %>" runat="server" /> | |
</div> --%> | |
<asp:PlaceHolder ID="ItemContainer" runat="server"/> | |
</layouttemplate> | |
<itemtemplate> | |
<%#SitefinityWebApp.Custom.ViewCountHelpers.IncrementNewsViewCount(Eval("Id").ToString())%> | |
<div class="sfnewsDetails sfdetails" data-sf-provider='<%# Eval("Provider.Name")%>' data-sf-id='<%# Eval("Id")%>' data-sf-type="Telerik.Sitefinity.News.Model.NewsItem"> | |
<h1 class="sfnewsTitle sftitle" data-sf-field="Title" data-sf-ftype="ShortText"> | |
<asp:Literal Text='<%# Eval("Title") %>' runat="server" /> | |
</h1> | |
<comments:CommentsAverageRatingControl runat="server" | |
ThreadKey='<%# ControlUtilities.GetLocalizedKey(Eval("Id"), null, CommentsBehaviorUtilities.GetLocalizedKeySuffix(Container.DataItem.GetType().FullName)) %>' | |
NavigateUrl="#commentsWidget" AllowComments='<%# Eval("AllowComments") %>' | |
DisplayMode="FullText" | |
ThreadType='<%# Container.DataItem.GetType().FullName %>'/> | |
<div class="sfnewsAuthorAndDate sfmetainfo"> | |
<asp:Literal Text="<%$ Resources:Labels, By %>" runat="server" /> | |
<sf:PersonProfileView runat="server" /> | <sf:FieldListView ID="PublicationDate" runat="server" | |
Format="{PublicationDate.ToLocal():MMM dd, yyyy}" /> | |
<comments:CommentsCountControl runat="server" | |
ThreadKey='<%# ControlUtilities.GetLocalizedKey(Eval("Id"), null, CommentsBehaviorUtilities.GetLocalizedKeySuffix(Container.DataItem.GetType().FullName)) %>' | |
NavigateUrl="#commentsWidget" AllowComments='<%# Eval("AllowComments") %>' | |
DisplayMode="ShortText" | |
ThreadType='<%# Container.DataItem.GetType().FullName %>'/> | |
</div> | |
<sf:FieldListView ID="summary" runat="server" Text="{0}" Properties="Summary" WrapperTagName="div" WrapperTagCssClass="sfnewsSummary sfsummary" EditableFieldType="ShortText"/> | |
<div class="sfnewsContent sfcontent" data-sf-field="Content" data-sf-ftype="LongText"> | |
<asp:Literal Text='<%# Eval("Content") %>' runat="server" /> | |
</div> | |
<asp:PlaceHolder ID="socialOptionsContainer" runat="server"> | |
</asp:PlaceHolder> | |
<comments:CommentsWidget runat="server" | |
ThreadKey='<%# ControlUtilities.GetLocalizedKey(Eval("Id"), null, CommentsBehaviorUtilities.GetLocalizedKeySuffix(Container.DataItem.GetType().FullName)) %>' | |
AllowComments='<%# Eval("AllowComments") %>' ThreadTitle='<%# Eval("Title") %>' ThreadType='<%# Container.DataItem.GetType().FullName %>' | |
GroupKey='<%# ControlUtilities.GetUniqueProviderKey("Telerik.Sitefinity.Modules.News.NewsManager", Eval("Provider.Name").ToString()) %>' | |
DataSource='<%# Eval("Provider.Name")%>' /> | |
</div> | |
<sf:NewsView ControlDefinitionName="NewsFrontend" runat="server" Title="Related news" UrlKeyPrefix="" ContentViewDisplayMode="Master" > | |
<RelatedDataDefinition RelatedFieldName="RelatedNews" RelatedItemType="Telerik.Sitefinity.News.Model.NewsItem" RelationTypeToDisplay="Child" RelatedItemSource="DataItemContainer"> | |
</RelatedDataDefinition> | |
<ControlDefinition ControlDefinitionName="NewsFrontend" runat="server" ProviderName="OpenAccessDataProvider"> | |
<Views> | |
<sf:ContentViewMasterDefinition ViewName="NewsFrontendList" runat="server"> | |
</sf:ContentViewMasterDefinition > | |
<sf:ContentViewDetailDefinition ViewName="NewsFrontendDetails" runat="server"> | |
</sf:ContentViewDetailDefinition > | |
</Views> | |
</ControlDefinition> | |
</sf:NewsView> | |
</itemtemplate> | |
</telerik:RadListView> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Telerik.Sitefinity.Modules.Blogs; | |
using Telerik.Sitefinity.Modules.News; | |
using Telerik.Sitefinity.Services; | |
namespace SitefinityWebApp.Custom | |
{ | |
public static class ViewCountHelpers | |
{ | |
public static string IncrementNewsViewCount(string id) | |
{ | |
if (HttpContext.Current.Session[id] == null && HttpContext.Current.Session[id] != "true") | |
{ | |
if (!SystemManager.IsDesignMode && !SystemManager.IsInlineEditingMode && !SystemManager.IsPreviewMode) | |
{ | |
NewsManager man = NewsManager.GetManager(); | |
var item = man.GetNewsItems().Where(i => i.Id == new Guid(id)).FirstOrDefault(); | |
if (item != null) | |
{ | |
man.Provider.SuppressSecurityChecks = true; | |
HttpContext.Current.Session[id] = "true"; | |
item.ViewsCount++; | |
man.SaveChanges(); | |
man.Provider.SuppressSecurityChecks = false; | |
} | |
} | |
} | |
return string.Empty; | |
} | |
public static string IncrementBlogsViewCount(string id) | |
{ | |
if (HttpContext.Current.Session[id] == null && HttpContext.Current.Session[id] != "true") | |
{ | |
if (!SystemManager.IsDesignMode && !SystemManager.IsInlineEditingMode && !SystemManager.IsPreviewMode) | |
{ | |
BlogsManager man = BlogsManager.GetManager(); | |
var item = man.GetBlogPosts().Where(i => i.Id == new Guid(id)).FirstOrDefault(); | |
if (item != null) | |
{ | |
man.Provider.SuppressSecurityChecks = true; | |
HttpContext.Current.Session[id] = "true"; | |
item.ViewsCount++; | |
man.SaveChanges(); | |
man.Provider.SuppressSecurityChecks = false; | |
} | |
} | |
} | |
return string.Empty; | |
} | |
} | |
} |
@hlong24399 here is an updated Gist using MVC. Since the modifies a content item on each view or technically 1 increment per session you will want to do any type of load testing to see how it may affect your content item load times.
https://gist.github.com/jonathanread/782554723051229a4be55c2518d91cfa
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi John, I am thanking you alot for the module!
I am just stucked at how to get the item id from MVC Views of the widget. Can you please help me?