Skip to content

Instantly share code, notes, and snippets.

@jonathanread
Created November 21, 2017 15:00
Show Gist options
  • Save jonathanread/b7590f2cdbbf5717d695f2f545fb33ed to your computer and use it in GitHub Desktop.
Save jonathanread/b7590f2cdbbf5717d695f2f545fb33ed to your computer and use it in GitHub Desktop.
Search Sitefinity Documents by Tag Name
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetDocsByTag.aspx.cs" Inherits="SitefinityWebApp.Sitefinity.GetDocsByTag" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="TagNameTextBox"></asp:TextBox>
<asp:Button runat="server" ID="FindDocWithTagNameBtn" Text="Search For Docs By Tag Name" />
<asp:Repeater runat="server" ID="DocsRepeater">
<ItemTemplate>
<div><%# Eval("Title") %></div>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
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.Model;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.OpenAccess;
namespace SitefinityWebApp.Sitefinity
{
public partial class GetDocsByTag : System.Web.UI.Page
{
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
LibrariesManager librariesManager = LibrariesManager.GetManager();
public IQueryable<Document> Docs;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string tagName = TagNameTextBox.Text;
Docs = FindDocsByTagName(tagName);
DocsRepeater.DataSource = Docs;
DocsRepeater.DataBind();
}
}
private IQueryable<Document> FindDocsByTagName(string tagName)
{
if (!tagName.IsNullOrWhitespace())
{
var taxon = GetTagByName(tagName);
if (taxon != null)
{
return GetDocumentsByTag(taxon);
}
else
{
//return error
return null;
}
}
else
{
//return error
return null;
}
}
private Taxon GetTagByName(string tagName)
{
Taxon taxon = null;
if (!tagName.IsNullOrWhitespace())
{
taxon = taxonomyManager.GetTaxa<FlatTaxon>().Where(t => t.Name == tagName).FirstOrDefault();
}
return taxon;
}
private IQueryable<Document> GetDocumentsByTag(Taxon taxon)
{
return librariesManager.GetDocuments()
.Where(d => d.Status == ContentLifecycleStatus.Live &&
d.Visible &&
d.GetValue<TrackedList<Guid>>("Tags").Contains(taxon.Id));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment