Skip to content

Instantly share code, notes, and snippets.

@jonathanread
Created December 8, 2015 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanread/6cb54c6bd8c6a9df2512 to your computer and use it in GitHub Desktop.
Save jonathanread/6cb54c6bd8c6a9df2512 to your computer and use it in GitHub Desktop.
This searches and returns pages with the string from "SearchPhrase"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Modules.GenericContent.Web.UI;
namespace SitefinityWebApp.Custom
{
public partial class GetSomePages : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
if (SearchPhrase.Text.IsNullOrEmpty())
{
Response.Write("Wrong");
}
else
{
var pages = App.WorkWith()
.Pages()
.LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Frontend)
.Where(pn => pn.NodeType == NodeType.Standard)
.ThatArePublished()
.Get();
List<MyNode> nodes = new List<MyNode>();
foreach (var pn in pages)
{
var pd = pn.GetPageData();
var controls = pd.Controls.Where(c => c.ObjectType.StartsWith(typeof(ContentBlock).FullName)).DefaultIfEmpty();
bool isOnPage = false;
foreach (var c in controls)
{
if (c != null && !isOnPage)
{
Guid contentBlockId = c.Id;
ControlProperty htmlProperty = c.Properties.Where(prop => prop.Control.Id == contentBlockId && prop.Name == "Html").FirstOrDefault();
if (htmlProperty != null)
{
if (htmlProperty.Value.Contains(SearchPhrase.Text))
{
isOnPage = true;
continue;
}
}
}
}
if (isOnPage)
{
nodes.Add(new MyNode()
{
PageNode = pn,
MatchPhrase = SearchPhrase.Text
});
}
}
foreach (var no in nodes.Distinct())
{
Response.Write("Title: " + no.PageNode.Title + " Phrase: " + no.MatchPhrase + " || " + no.PageNode.GetFullUrl() +"<br/>");
}
Response.Write(nodes.Count());
}
}
}
}
public class MyNode
{
public PageNode PageNode { get; set; }
public string MatchPhrase { get; set; }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetSomePages.aspx.cs" Inherits="SitefinityWebApp.Custom.GetSomePages" %>
<!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="SearchPhrase"></asp:TextBox>
<asp:Button runat="server" ID="Button" OnClick="Button_Click" Text="Find The Pages" />
</div>
</form>
</body>
</html>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace SitefinityWebApp.Custom {
public partial class GetSomePages {
/// <summary>
/// form1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
/// <summary>
/// SearchPhrase control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox SearchPhrase;
/// <summary>
/// Button control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button Button;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment