Skip to main content

Posts

Showing posts from October, 2024

Add contextual code to an item in Sitecore

One of the most useful customization of Sitecore is the ability to add contextual code to an item in Sitecore. For example let say we have a page in Sitecore that works well in the English language. A content author wants to create the same page in french. Unfortunately there isn't an out of the box way to copy the English page content (including all child items) into french.  This is where adding contextual code will help.  To do this you would have to install Sitecore powershell extensions. There are lots of documentations  online about this tool.  To add contextual code to an item in Sitecore, you would need to add a new module to the powershell script library located at /sitecore/system/Modules/PowerShell/Script Library/ Under this module you can add any powershell scripts you want under the context menu node.  Make sure to enable the module and you should see these script under any item. You can even modify the rule section to d...

GlassMapper

 How GlassMapper works  Glass Mapper is an object relational mapping (ORM) tool in Sitecore. It maps Sitecore items to strongly-typed classes. This makes it simple to access Sitecore data within C# code. Without an ORM, Sitecore developers would need to write and manage a lot of mapping code.  Below are example files that you can use. You have an interface class file, a class file that implements the interface and lastly you can use this in your cshtml file.  That's it. Happy Sitecoreing! IMySitecoreItemName class file namespace Your.NameSpace { [SitecoreType(TemplateId = Templates.MyTemplate.IdString, AutoMap = true)] public interface IMySitecoreItemName : IMySitecoreModel { [SitecoreField(FieldId = Templates.MySitecoreItem.Fields.MyField1)] string StringFieldName { get; set; } [SitecoreField(FieldId = Templates.MySitecoreItem.Fields.MyField2)] bool BoolFieldName { get; set; } } } Use it on your .cs file ...

Create Object XML while Debugging in VS

 There are times when you put breakpoints in Visual studio and read object values within Visual Studio. This is all good if you are doing some debugging. But if you want to save an object like a Json object in Visual studio there isn't a straightforward way to do that. I found this piece of code that I copied and pasted in the Immediate Window in Visual Studio and was able to save the object as an XML file. (new System.Xml.Serialization.XmlSerializer(YourObject.GetType())).Serialize(new System.IO.StreamWriter(@"c:\tmp\YourObject.xml"), YourObject) Happy Sitecoreing!