Skip to content

Instantly share code, notes, and snippets.

@ps-team
ps-team / SendAnEmail.cs
Created October 27, 2017 09:51
Send an email to an email address using the EmailHost set against the publisher. You could create a text box called #MyFormFieldName with this to allow a user to enter details. @using System.Net.Mail; **This method is not necessarily best practice, we should discuss other options for posting emails.**
@using Contensis.Framework.Web;
@using System.Net.Mail;
@if (IsPost) {
string EmailFrom = "[email protected]";
string EmailTo = "[email protected]";
MailMessage email = new MailMessage();
email.IsBodyHtml = true;
string emailBody = "";
@ps-team
ps-team / Skip and Take
Created October 27, 2017 09:51
Group items and wrap in a div in Razor using Skip and Take
@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search;
@{
var relatedContent = CurrentNode.RelatedNodes();
}
@if (relatedContent.Count >= 1){
for (var i = 0; i < relatedContent.Count; i += 3)
{
@ps-team
ps-team / news list with category filter
Created October 27, 2017 09:51
News list with category filtering Requires meta data for categories to be added to the page so editors can control categories shown.
@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
@{
// Get category value from url to filter results with
var catFilter = Request.QueryString["TaxonomyKey"];
// Get news articles
var newsArticlesQuery = Query.Where("SC_T_ID").IsEqualTo("-2875009").And("Property_TaxonomyCategories").Contains(catFilter).OrderBy("Property_DatePublished").Descending;
var newsArticles = new NodeFinder().Find(newsArticlesQuery, selectCount:8);
@ps-team
ps-team / Distinct list of categories.cshtml
Created October 27, 2017 09:50
Generate a **distinct** list of 'categories' assigned to a set of nodes. This code loops through a set of nodes, creates an array from the categories on each node, then we get only the distinct ones. This array can then be looped through again to build a list of your categories.
@using Contensis.Framework.Web.Search;
@{
var query = Query.Where("Property_Path").StartsWith("/connect/blog/").And("Property_AuthorID").IsEqualTo(authorId.ToString()).OrderBy("Property_Title");
var nodes = new NodeFinder().Find(query, selectCount: 20);
string fulltagslist = "";
}
@foreach(var node in nodes)
{
@ps-team
ps-team / GetPLaceholderDataInRazor.cshtml
Created October 27, 2017 09:49
jMoo's amazing placeholder data in razor magic unicorn wizardry - Example of getting placeholder data in razor
@using System.Data;
@using Contensis.Framework.Web;
@using Contensis.Framework.Web.Search;
@using Contensis.Core.Utilities.DataAccess;
@{
var relatedContent = CurrentNode.RelatedNodes();
var dal = new SqlDataAccess(Contensis.Framework.Web.AppContext.Current.Server.ConnectionString);
}
@ps-team
ps-team / TemplateLookup.cshtml
Created October 27, 2017 09:49
A 'maintenance' razorview - displays an alphabetical list of all used template types in a site with a page count for each template
@using Contensis.Framework.Web
@{
@*
- this is more of a 'maintenance' razorview
- displays an alphabetical list of all used template types in a site with a page count for each template
*@
<h2>List of all used templates with page count</h2>
@ps-team
ps-team / Validating Meta Data variable assignments.cshtml
Created October 27, 2017 09:48
Smarter variable assignment of Meta Data to prevent Razorviews from potentially falling over
@using Contensis.Framework.Web
@{
@*
- Sometimes meta data does not always become instantly available when building/previewing templates/pages,
- or if the preview database is cleared and is in the process of rebuilding itself.
-
- Depending on how you access the meta data, this can cause a razorview to fall over:
- ERROR: 'Cannot perform runtime binding on a null reference'
@ps-team
ps-team / RemoveShippedjQueryVersion.vbs
Created October 27, 2017 09:42
Oh, man. This is horrible. But sometimes needed in older versions where you can't remove jQuery due to a control rendering it out. Honestly, don't use this.
'Jquery reference code
Dim js = From file In Javascript.Files
Where file.Key = "jquery"
if js.count > 0 then
Javascript.Files.Remove(js.First())
end if
Javascript.Files.Insert(0, New CMS_API.Utilities.Javascript.UrlJavascriptFileReference("/SiteElements/Scripts/jquery.js"))
@ps-team
ps-team / Localisation part 1 - get all nodes in current language.cshtml
Created October 27, 2017 09:40
Localisation - using localisation in Contensis razorviews - In razor we can access the current pages' language folder and then use that to look-up all nodes in the CMS that are in that language. So for example, we can use this to build a custom search results razorview, which is language specific. Or we can build up a more specific search query …
@using System.Collections.ObjectModel
@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
@{
@*
- assuming the site structure has been setup so that all content for each language
- is contained in a language folder that sits under the website root, we target the
- current pages' language folder and create a dynamic path variable which we tie into
@ps-team
ps-team / GetDaySuffix.cs
Created October 27, 2017 09:38
Gets suffix for current day (st, nd, rd or th)
@{
int lastUpdatedOnDay = CurrentNode.Data.DateModified.Day;
string lastUpdatedOn = lastUpdatedOnDay + GetDaySuffix(lastUpdatedOnDay) + CurrentNode.Data.DateModified.ToString(" MMMM yyyy");
}
<span>Last updated on @lastUpdatedOn</span>
@functions {
//Gets suffix for current day (st, nd, rd or th)
string GetDaySuffix(int day)