Skip to content

Instantly share code, notes, and snippets.

@ps-team
ps-team / NodeLookup.cshtml
Created October 27, 2017 08:09
Example showing the best way to get node by legacy Content and Folder Ids, it is recommended to put this in App_Code and use this one class throughout as this functionality uses CMS_API and this will be deprecated soon.
@using Contensis.Framework.Web;
@using CMS_API.Navigation;
@functions {
class NodeLookup {
public ContentNode GetWebPage(int webPageId) {
var navigationProvider = NavigationProviderFactory.GetNavigationProvider(NavigationProviderType.Xml);
var navigationItem = navigationProvider.GetNavigationWebpage(webPageId);
@ps-team
ps-team / RelatedContent.cshtml
Created October 27, 2017 08:11
Loading in related content from Contensis and rendering it on a page. Useful for "shared" content.
@{
// This lets us load in content added through relationships and use it in the page
// as though it is part of the CurrentNode.
var requiredModules = CurrentNode.RelatedNodes("RequiredModules"); //name of relationship in Contensis
}
@if (!String.IsNullOrEmpty(requiredModules.ToString()))
{
if (requiredModules.Count >= 1)
@ps-team
ps-team / FullTextQuery.cshtml
Created October 27, 2017 08:11
Example of doing a weighted search with the WebApi
@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
@using System.Collections.ObjectModel
@{
IQuery pageQuery = Query.WhereFullTextContains("Contensis", In.Column("Property_Title", 100), In.Column("SC_Content", 10));
ReadOnlyCollection<ContentNode> pages = new NodeFinder().Find(pageQuery, selectCount:3);
}
<ul>
@ps-team
ps-team / simplenav.cshtml
Created October 27, 2017 08:11
A simple navigation that outputs the contents of the current folder and the homepage of any sub folders one level below.
@using Contensis.Framework.Web
<ul class="secondaryNav">
<li class="secondaryNav__title"><a href="@CurrentNode.Parent.Parent.Path">@CurrentNode.Parent.Title</a></li>
@foreach (ContentNode navItem in CurrentNode.Parent.Descendants().Where(wP => wP.IsWebPage && wP.IncludeInMenu && wP.Depth <= (CurrentNode.Depth +1)).OrderBy(wP => wP.MenuOrder))
{
if (navItem.Depth > CurrentNode.Depth && navItem.IsHomePage == true)
{
@ps-team
ps-team / NodeQuery-SearchParameteres.xml
Created October 27, 2017 09:33
These example show passing search parameters either form the query string or a from posted data to a node query
<control name="News Listing Using Node Query" showInMenu="true" category="Dev Training" viewingGroup="1">
<properties>
<nodeQuery name="NewsArticlesQuery" orderby="Property_DatePublished desc">
<where property="Type" operator="IsEqualTo" value="News" />
<and property="Title" operator="Contains" value="@Request.Querystring.Keywords" />
<and property="Data.TaxonomyCategories" operator="Contains" value="@Request.Querystring.category" />
</nodeQuery>
</properties>
</control>
@ps-team
ps-team / BasicNodeQuery.cs
Created October 27, 2017 09:33
Example of doing a basic Node Query and how to render the data it returns
<control name="News Listing Using Node Query" showInMenu="true" category="Dev Training" viewingGroup="1">
<properties>
<nodeQuery name="NewsArticlesQuery" orderby="Property_DatePublished desc">
<where property="Type" operator="IsEqualTo" value="News" />
</nodeQuery>
</properties>
</control>
@using Contensis.Framework.Web;
@using System.Collections.ObjectModel;
@ps-team
ps-team / Email validation.cshtml
Created October 27, 2017 09:34
A re-usable C# function for backend email validation. No need to use long REGEX which, depending on the expression, may not capture all possibilities. The .Net MailAddress class does all this for us, so by sending it a string with an invalid email format, it will error which we can capture and use to dictate what to do next.
@using System.Net.Mail
@{
string email = "[email protected]";
if(IsValidEmail(email))
{
// do something like submit the form and store the data, send an email or w/e
}
else
@ps-team
ps-team / CanonicalTagging.cshtml
Created October 27, 2017 09:35
Canonical tags in Razor. I made some changes to point URL's with query strings back at the URL without the QS as this was showing up as duplicated content in GA.
@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
@using System.Collections
@using System.Collections.ObjectModel
@using System.Text.RegularExpressions;
@{
if (CurrentNode.Data.Property_SynchronisationSourceMasterContentID >= 0)
{
int masterPageContentID = CurrentNode.Data.Property_SynchronisationSourceMasterContentID;
Node masterPage = null;
@ps-team
ps-team / List all sibling pages excluding folder homepage.cshtml
Created October 27, 2017 09:35
Simple Razorview to list all sibling pages in a folder and exclude the folder homepage using meta data to generate the link title.
@using System.Collections.ObjectModel
@using Contensis.Framework.Web
@{
// links in containing folder
ReadOnlyCollection<ContentNode> nodes = CurrentNode.Siblings(false);
// render - links if they exist
if(nodes.Count > 0)
@ps-team
ps-team / ImageAltText.cs
Created October 27, 2017 09:35
How to get the alt text of an Image node using the WebApi
@using Contensis.Framework.Web
@{
NodeFactory nf = new NodeFactory();
ContentNode n = (ContentNode)nf.LoadById(####);
<img src="@n.Path" alt="@n.MenuName" />
}