K# And Kentico EMS – Part 1 Karol Jarkovsky Consulting Services Manager Kentico Software http://devnet.kentico.com/Blogs/Karol-Jarkovsky.aspx karolj@kentico.com Agenda 1. K# • K# Facts & Syntax • K# Security & Performance 2. K# Demo I • Rendering MegaNav using macros 3. K# and On-line Marketing • OnlineMarketingContext 4. K# Demo II • Sending complex score notification e-mails Q&A K# Basics K# Basics Complete guide available at http://devnet.kentico.com/docs/devguide/macro_expressions_overview.htm More in depth information available at http://devnet.kentico.com/Blogs/Martin-Hejtmanek/December-2011/From-developers-to-developers---K-.aspx K# Facts & Syntax • K# is Kentico compilation-free scripting language, • K# is available for any context macros, {% CurrentUser.UserName %} • You can still take advantage of K# and work with other types of macros, Macro type Context macro equivalent {@<cookie_name>@} {%Cookie.<cookie_name>%} {#<custom_macro_expression>#} {%Custom.<custom_macro_expression>%} {&<path>&} {%Path.<path>%} {?<query_string_key>?} {%QueryString.<key>%} K# Facts & Syntax • K# creates hierarchical structure from all the objects available in the current context, • • Testing tool available at ~/CMSAdminControls/UI/Macros/ObjectBrowser.aspx, To retrieve context information you can access one of the following collections: • CMSContext – Complete information on the current request related objects – user, document, client browser, page template, CSS, website, settings, etc., {% CMSContext.CurrentSite.SiteName %} • CommunityContext – Information on current group and other community related objects, {% CommunityContext.CurrentGroup.GroupCreatedWhen %} • EcommerceContext – E-commerce related data valid for the current request, {% EcommerceContext.CurrentCustomer.CustomerLastName %} • ForumContext – Forums related information, {% ForumContext.CurrentForum.ForumName %} K# Facts & Syntax • MediaLibraryContext – Media library data collection, {% MediaLibraryContext.CurrentMediaLibrary.LibraryLastModified %} • Use K# and context macros to access any global or site specific object through the following selectors: • GlobalObjects – Provides access to all global objects available, {% GlobalObjects.Users.Administrator.UserID %} • SiteObjects – The same as above but includes only current site objects., {% SiteObjects.Roles.CMSBasicUsers.RoleID %} K# Facts & Syntax • Several ways how you can actually access value in the data context item, For example to access current website home page data you can use {% CMSContext.Current.RootDocument.Children.Home %} {% CMSContext.Current.Documents["/Home"] %} To access document object containing coupled data (data stored outside CMS_Tree and CMS_Document table) you need to access document through the WithAllData selector {% CMSContext.Current.RootDocument.Children.WithAllData.Home %} {% CMSContext.Current.Documents.WithAllData["/Home"] %} or in case you’re looking for information on CMS Basic Users role {% SiteObjects.Roles.CMSBasicUsers %} {% SiteObjects["Roles"]["CMSBasicUsers"] %} {% SiteObjects[“cms.role”]["CMSBasicUsers"] %} • K# supports variable declarations and allows you to use variable declared earlier towards the end of the whole text where it was declared, K# Facts & Syntax • K# makes nested macros much simpler and transparent, Previous version required you to write something like {(1)% CurrentUser.LastName|(default){(2)%CurrentUser.UserName%(2)}%(1)} You can achieve the same results using K# and the following expression {% name = CMSContext.CurrentUser.LastName; (( name != "")? name : CMSContext.CurrentUser.UserName; ) %} You can even combine two different types of macros within single expression {% OnlineMarketingContext.CurrentContact.ContactLastName + " visited this page at " + Custom.CurrentDateTime %} • K# you can use integrates with Kentico UI and provides several tools that makes writing macro expressions easier, • • • Automatic completition, Macro selection control, Macro condition editor. K# Security Security • When the macro is evaluated permissions are checked for the user that specified the macro – permission of the user content is displayed for aren’t checked at all, • When the macro is defined the ‘#’ appears at the end of the expression to indicate that macro author user identity stamp was just added to the definition, • When the macro is evaluated system checks permission for the user whose identity stamp is coupled with the expression, • Actual value of the stamp hidden in the UI, but stored in the back-end looks something like: {% <macro expression>|(user)administrator|(hash)a3309399c0fd38344b57c 18b87528b4e15f36b059c59b8cd152e6fd01371da72 %} • Even though you cannot see identity stamp value in the UI it’s still part of the macro and therefore stored as field value in the DB – make sure field length is sufficient for both macro as well as hash, • To avoid signing macro expression insert ‘@’ at the end (instead of ‘#’). Macro is then evaluated as for public user. K# Performance Performance • Data context content by default valid through the time of request processing – available and re-usable across the whole system, • Data aren’t retrieved from DB unless required = when navigating through the context tree or during the evaluation when specific collection/object/property is accessed – data context and macro engine both leverage a lazy-read, • If you access many objects from the data context in your macro expressions you can cache data context and re-use data you already requested during previous requests, CMSCachedContext(Int32 cacheMinutes, String cacheItemName) Data context is cached for specified amount of minutes and stays in the cache unless it’s flushed or cache period expires – no cache dependencies, • If you need to make sure cached context is invalidated when certain change occurs in the system you can cache result of macro expression instead of whole context, Cache(Object expression, Int32 cacheMinutes, Boolean condition, String cacheItemName, String cacheItemNameParts, CMSCacheDependency cacheDependency) K# Demo I K# Demo I Rendering MegaNavigation using macros Questions & Answers Thank you!