Orchard CMS Quick Tip–Getting at the Orchard.Layouts JSON for your template defaults

If you have started using the Orchard.Layouts module which has seen some pretty significant updates in the last few releases you might be wondering how you get at the JSON data that you’ve been finding in content part default layout data setting. There are two ways but both are a bit tricky.

UPDATE

It’s been pointed out to me by several people that this technique doesn’t work any more. I don’t know an alternative way to solve this, sorry.

What do I mean exactly?

When you have a LayoutPart attached to one of your content items you will see a section like this in its Content Definition page:

orchard-cms-getting-json-template

This data pre-populates the page when you create a new content item.

If you want to customise your own content types with LayoutParts that come with defaults its possible but their currently there isn’t anyway to access this through the admin panel directly. Instead you need to take one of the two courses listed below.

XML export

The first option is to export the data using the import/export module that comes with Orchard CMS. You can so it this way but then you will get the JSON data in xml encoded format so it will need extra processing.

Access the clipboard data

(Special thanks to Daniel Stolt, one of the core Orchard.Layouts developers, for this idea)

In Chrome you can select an element, hit Ctrl+C and then that JSON data is actually placed on the system clipboard, along with the plain-text of the element. It's how copy-paste works.

The trouble is that you probably don’t have an application installed that will paste clipboard data with the type “Chromium Web Custom MIME Data Format” so if you try to paste it back again all you will get is the plain text representation.

To get at this data you can use LINQPad to extract this hidden JSON data:

orchard-cms-getting-json-linqpad

Just follow these simple steps:

  1. Download LINQPad 4
  2. Paste this query into the query window:

    var s = (System.IO.MemoryStream)System.Windows.Forms.Clipboard.GetData("Chromium Web Custom MIME Data Format");
    System.Text.Encoding.Unicode.GetString(s.ToArray()).Dump();
    
  3. Set the Language to C# Statement(s) for multiline support. Note the (s) on the end.

  4. Query > Query Properties > Additional References > Add > Search Forms > Add System.Windows.Forms.dll reference > OK
  5. Run the query
  6. Bonus tip: File > Save to save the snippet for next time. You can load it back from the My Queries tab in the bottom corner.

Troubleshooting - NullReferenceException: Object reference not set to an instance of an object.

You copied something else into the clipboard while setting this up, copy the original back from Orchard.

No comments :