Custom Item Generator

  0 rating
CMS;Developers
3/22/2013 2:04:25 AM
3/22/2013 2:04:25 AM

About

 The custom item generator is a tool that is used to create classes which will wrap access to a template's fields. For a more in depth overview please check out the following blog post.


If you have any issues with the Custom Item Generator module, please report them to the Shared Source forum on SDN or email ​customitem@velir.com. 

Documentation

Documentation
  • Documentation > Getting Started with the Custom Item Generator
    Getting Started with the Custom Item Generator

     Velir is going to be posting extra information about the custom item generator on our blog, these posts can be found here.

    Velir Blog Custom Item Posts


    Initial Configuration

    The first configuration change that will need to be made is to add the following command to your ~/App_Config/Commands.config file.


    <command name="devtools:generatecustomitem" type="CustomItemGenerator.SitecoreApp.CustomItemGeneratorCommand,CustomItemGenerator"/>


    Then you will need to adjust the custom item configuration file found here


    ~/App_Config/Include/CustomItem.config


    There are two settings you have the be concerned with to start


    Base.Namespace - This will be the base of the auto generated namespace for a custom item class. The rest of the namespace (by default) will be generated from the template folder structure.


    Base.FileOutputPath - This will be the base folder that the custom item class files will be written to. The rest of the file path (by default) will be generated from the template folder structure.

    To start that should be it. There are other configuration settings that can be modified, but you will not need to adjust those for the default setup.


    Basic Usage

    In order to create a custom item for a template (or a folder of templates), all you have to do is right click on the template/folder and choose Tools-->Generate Custom Items. This will pop up a simple dialog that allows you to override the default settings if you want, it also asks you what partial class files you want to include in the generation. In most cases you will just want to create the base.cs file. The other file options are there as a convenience, but be careful because as of right now if you select the .instance.cs option say, it will overwrite an existing .instance.cs file. Probably the preferable way to use these partial class files is to just create them in Visual Studio when you need them.


    The instance/interface/static files are partial classes that allow you to separate custom code into a file structure that makes sense. You should never put custom code in the .base.cs class, It should be assumed that this file can always be generated again without affecting any custom code. The files should be pretty self explanatory

        

        * instance.cs - Any code that relates to a specific instance of a specific custom item
        * interface.cs – Any interface implementations for the specified custom
        * static.cs – Any static methods that generally relate to the custom item.


    So say for example I was implementing an interface on a publication custom item that requires a method called TestMethod(),my process would be something like this


    1) Create PublicationItem.interface.cs in Visual Studio if it does not exist already.

    2) Implement the interface method in the partial class file PublicationItem.interface.cs .


    Then say that the Publication template changes, we can re-run the custom item generator and get a new .base.cs class without affecting the custom interface code we added.


    Simple Example

    Here are some basic usage examples, say for example we have a basic piece of content that has a body rich text field

    BasicItem basicContent = Sitecore.Context.Item;

    For each field you have the following options

        

        * Field – this will allow you to get at the underlying field object. This can be useful if you need to get the underlying name of the field.
        * Raw – This is the raw value of the field. This is usually used if you need to check the underlying value against something, but do not want to run it through the field renderer before you do the check.
        * Rendered – This is what you use when you want to display a field value on the front end. This will run the field through the field renderer which is needed for a bunch of reasons when displaying content on the front end. For example the page editor will not work unless you put the fields through the field renderer.

    We also added some convienence calls for each of the field types, which are

        * CheckboxField (field.Checked) – A simple boolean to say if the checkbox is checked or not.
        * DateField (field.DateTime) – This returns the DateTime stored in the date field.
        * FieldField,ImageField (field.MediaItem and field.MediaUrl) – return the Item referenced in the field as well as an easy way to return the URL to the referenced media item.
        * IntegerField (field.Integer) – Returns the field content cast as an integer.
        * TextField (field.Text) – This returns the rendered text from the field, the equivalent of field.Rendered.


    Some basic examples


    I want to display the body text followed by a date stored in the item

    litOutput.Text = basicContent.Body.Text + “ : “ + basicContent.Date.DateTime.ToString(“MM dd YYYY”);


    I want to check if the body contains some text

    if(basicContent.Body.Raw.Contains(“some text”)) {

        Do Something

    }


    In this case I want to test using the .Raw value and not the .Rendered value. This is because if I am in Page Editor mode then the .Rendered value may include javascript, which we do not want to include in our check.


    I want to link to a file stored in a file field

    if(basicContent.LinkedFile.MediaItem == null) return;

    linkFile.NavigateUrl = basicContent.LinkedFile.MediaUrl;



    0
Back
Release notes
The module was added 21-10-2010
Read more Back
Code examples

Solution screenshots(0)

Upload

Reviews (0)

Sort by: Date Most votes
  • Profile Avatar
    [fullName]

    Level: 0

    x0 x0 x0

    [date]

    [title]

    [text]

    Was this helpful?

    0

Comments (0)

Sort by: Date  Most votes

Leave a Comment

Comment must be field in
Post comment
loader

Write a review

Title can't be empty
Review can't be empty
Post review

Download

Title Description Download Action

Add File