Rotating Content with Sitefinity 4.0
Hi,
I'm trying to get rotating content with Sitefinity. Here's where I am right now:
var pageGuids = App.WorkWith().Pages().LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Frontend)
.Where(page => page.ShowInNavigation == true && page.ApprovalWorkflowState == "Published")
.Get().Select(page => page.Id).ToArray();
Random rng = new Random();
Guid randomGuid = pageGuids[rng.Next(0, pageGuids.Length)];
PageNode pn = App.WorkWith().Page(randomGuid).Get();
Hello Roland,
I am not sure what actually you are trying to do and how you want to render the page. If you want to rotate content you should use content facades. In your case you can extract only page properties data.
All the best,
Ivan Dimitrov
the Telerik team
Thanks Ivan,
Are you implying that I should update a content item content such as title and body dynamically form a list of content items?
Anyways, maybe it's the learning curve but I'm struggling to get this working. I am approaching it from a different angle as well and trying to develop a custom widget based on the NewsRotator in the SDK, which is not quite what I want.
What I want is to have say content from "Content" types with a particular tag revolve on page refresh. The specified tag will need to be customized somewhere in the custom widget interface. I could get this random content scripted out into a stored procedure but it's not "the right way to do it", plus I can't figure how to access the connection string in DataConfig.conf (this is not too important however).
Please let me know if this you think this is the right solution to pursue,
Roland
FYI. I think I found a solution that works for me. The merit goes to the other posts: www.sitefinity.com/.../generic-content-by-category.aspx however, Here's what I did:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Telerik.Sitefinity;using Telerik.Sitefinity.Fluent.Pages;using Telerik.Sitefinity.Modules.Pages;using Telerik.Sitefinity.Pages.Model;using Telerik.Sitefinity.GenericContent.Model;using Telerik.Sitefinity.Taxonomies.Model;using Telerik.Sitefinity.Taxonomies;using Telerik.Sitefinity.Modules.GenericContent;using System.Collections;using Telerik.Sitefinity.Utilities.TypeConverters;using Telerik.Sitefinity.Data;namespace SitefinityWebApp.MyControls public partial class RandomPageControl : System.Web.UI.UserControl /// <summary> /// Descriptor for a type /// </summary> /// <param name="itemType"></param> /// <param name="taxon"></param> /// <returns></returns> private TaxonomyPropertyDescriptor GetPropertyDescriptor(Type itemType, ITaxon taxon) return TaxonomyManager.GetPropertyDescriptor(itemType, taxon); /// <summary> /// Issues query to retrieve content items for a given taxon (limit to 1000) /// </summary> /// <param name="taxon"></param> /// <param name="contentProvider"></param> /// <param name="itemType"></param> /// <returns></returns> private IEnumerable GetItems(ITaxon taxon, ContentDataProviderBase contentProvider, Type itemType) TaxonomyPropertyDescriptor prop = GetPropertyDescriptor(itemType, taxon); int? totalCount = 0; var items = contentProvider.GetItemsByTaxon(taxon.Id, prop.MetaField.IsSingleTaxon, prop.Name, itemType, string.Empty, string.Empty, 0, 1000, ref totalCount); return items; /// <summary> /// Return a published list of content items for a given category /// </summary> /// <param name="category"></param> /// <returns></returns> protected List<ContentItem> GetContentItemsByCategory(string category) List<ContentItem> ciL = new List<ContentItem>(); TaxonomyManager taxManager = TaxonomyManager.GetManager(); var taxonomyManager = TaxonomyManager.GetManager(); var c = "categories"; var taxonomy1 = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == c || t.Title == c).SingleOrDefault(); var taxon = taxonomy1.Taxa.Where(t => t.Name == category || t.Title == category).SingleOrDefault(); if (taxon != null) string itemTypeName = "Telerik.Sitefinity.GenericContent.Model.ContentItem"; Type itemType = TypeResolutionService.ResolveType(itemTypeName); var manager = ManagerBase.GetMappedManager(itemType, ""); ContentDataProviderBase contentProvider = manager.Provider as ContentDataProviderBase; var v = GetItems(taxon, contentProvider, itemType); //Load into strongly typed list foreach (ContentItem n in v) ciL.Add(n); else throw new Exception("Category " + category + " was not found."); return ciL.Where(a => a.Status == ContentLifecycleStatus.Live && a.ApprovalWorkflowState == "Published").ToList(); /// <summary> /// Returns content items with a particular tag /// </summary> /// <param name="tag">tag word (name or title)</param> /// <returns></returns> protected List<ContentItem> GetContentItemsByTag(string tag) List<ContentItem> ciL = new List<ContentItem>(); TaxonomyManager taxManager = TaxonomyManager.GetManager(); // Get the tag var taxon = taxManager.GetTaxa<FlatTaxon>().Where(t => t.Name == tag || t.Title == tag).Single(); // I want content items if (taxon != null) string itemTypeName = "Telerik.Sitefinity.GenericContent.Model.ContentItem"; Type itemType = TypeResolutionService.ResolveType(itemTypeName); var manager = ManagerBase.GetMappedManager(itemType, ""); ContentDataProviderBase contentProvider = manager.Provider as ContentDataProviderBase; var v = GetItems(taxon, contentProvider, itemType); //Load into strongly typed list foreach (ContentItem n in v) ciL.Add(n); else throw new Exception("Tag " + tag + " was not found."); return ciL.Where(a => a.Status == ContentLifecycleStatus.Live && a.ApprovalWorkflowState == "Published").ToList(); /// <summary> /// Auto wired method fires when page loads /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) Random rng = new Random(); string category = "CustomerProfiles"; try var allContent = GetContentItemsByCategory(category); if (allContent.Count > 0) ContentItem ci = allContent[rng.Next(0, allContent.Count)]; pageTitle.Text = ci.Title; pageBody.InnerHtml = ci.Content.ToString(); else pageTitle.Text = "No content was categoriezed with " + category + "."; catch (Exception ex) pageTitle.Text = "Error"; pageBody.InnerText = ex.Message;