Tuesday, February 16, 2010

Creating Edit Forms for Custom Types in Alfresco Share

Most organizations that will use Alfresco will want to create custom types.
Associated with each custom type is a data model or set of metadata that is unique to that type of document.
To be able to edit document type metadata in Alfresco Share, you need to define an edit form.

Here's a brief example for how to set up and define the metadata for a custom document type and then how to define a Share edit form for that custom type.

Some good resources with getting started with Alfresco Share forms include:

Alfresco Wiki Form Developer Guide page.
Alfresco Wiki Form Examples page.  [Good starting place]
Ed Loves Java on creating Alfresco Share 3.2 forms.
Loftux on Alfresco Forms

First we define a minimum custom Alfresco type. There are four files needed to add this new type to the content model.

tomcat\alfresco\WEB-INF\classes\extension\model\mmModel.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Definition of new Model -->
<model name="mm:minmodel" xmlns="http://www.alfresco.org/model/dictionary/1.0">

 <!-- Optional meta-data about the model -->
 <description>MinProperty Model</description>
 <author>Formtek</author>
 <version>1.0</version>

 <!-- Imports are required to allow references to definitions in other models -->
 <imports>
  <!-- Import Alfresco Dictionary Definitions -->
  <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d" />
  <!-- Import Alfresco Content Domain Model Definitions -->
  <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm" />
 </imports>

 <!-- Introduction of new namespaces defined by this model -->
 <namespaces>
  <namespace uri="http://www.formtek.com/model/content/1.0" prefix="mm" />
 </namespaces>

 <constraints>
  <constraint name="mm:colorList" type="LIST">
            <parameter name="allowedValues">
                <list>
                    <value>Red</value>
                    <value>Orange</value>
                    <value>Yellow</value>
                    <value>Green</value>
                    <value>Blue</value>
                    <value>Indigo</value>
                    <value>Violet</value>
                </list>
            </parameter>
        </constraint>
 </constraints>

 <types>
  <!-- Enterprise-wide generic document type -->
  <type name="mm:doc">
   <title>Minimum Custom Document</title>
   <parent>cm:content</parent>
   <properties>
    <property name="mm:freeText">
     <type>d:text</type>
     <mandatory>false</mandatory>
    </property>
   </properties>
   <associations>
    <association name="mm:relatedDocuments">
     <title>Related Documents</title>
     <source>
      <mandatory>false</mandatory>
      <many>true</many>
     </source>
     <target>
      <class>mm:doc</class>
      <mandatory>false</mandatory>
      <many>true</many>
     </target>
    </association>
   </associations>
  </type>
 </types>

 <aspects>
  <aspect name="mm:colorAble">
   <title>Rainbow Color</title>
   <properties>
    <property name="mm:color">
     <type>d:text</type>
     <mandatory>true</mandatory>
     <default>Green</default>
     <constraints>
      <constraint ref="mm:colorList" />
     </constraints>
    </property>
   </properties>
  </aspect>
 </aspects>
</model>


tomcat\webapps\alfresco\WEB-INF\classes\alfresco\min-model-context.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
    <!-- Registration of new models -->
    <bean id="minModel.dictionaryBootstrap" parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap">
        <property name="models">
            <list>
                <value>alfresco/extension/model/mmModel.xml</value>
            </list>
        </property>
        
        <property name="labels">
            <list>
                <value>alfresco/extension/model/mmModelResourceBundle</value>
            </list>
        </property>
    </bean>

</beans>



tomcat\webapps\alfresco\WEB-INF\classes\alfresco\extension\web-client-config-custom.xml
<alfresco-config>

 <!-- show related documents association on doc property sheet -->
 <config evaluator="node-type" condition="mm:doc">
  <property-sheet>
   <show-association name="mm:relatedDocuments"/>
   <show-property name="mm:freeText"/>
  </property-sheet>
 </config>

 <config evaluator="aspect-name" condition="mm:colorAble">
        <property-sheet>
            <show-property name="mm:color" />
        </property-sheet>
    </config>

 <config evaluator="string-compare" condition="Content Wizards">
  <content-types>
   <type name="mm:doc" />
  </content-types>
 </config>

 <config evaluator="string-compare" condition="Action Wizards">
  <subtypes>
   <type name="mm:doc" />
  </subtypes>  

  <!-- The list of content and/or folder types shown in the specialise-type action -->
  <specialise-types>
   <type name="mm:doc" />
  </specialise-types>  
  
  <aspects>
   <aspect name="mm:colorAble" />
  </aspects>
 </config> 
 
 <config evaluator="string-compare" condition="Advanced Search">
  <advanced-search>
   <content-types>
    <type name="mm:doc" />  
   </content-types>
   <custom-properties>  
    <meta-data aspect="mm:colorAble" property="mm:color"/>
   </custom-properties>
  </advanced-search>
 </config>
</alfresco-config>


tomcat\webapps\alfresco\WEB-INF\classes\alfresco\extension\model\mmModelResourceBundle.properties
mm_minmodel.description=Alfresco Example Minimum Domain Model

mm_minmodel.type.mm_doc.title=Min Extended Content
mm_minmodel.type.mm_doc.description=Minimum Extended Content Document
mm_minmodel.property.mm_freeText.title=Additional Details
mm_minmodel.property.mm_freeText.description=Place to enter more details about this document
mm_minmodel.aspect.mm_colorAble.title=Rainbow Color
mm_minmodel.aspect.mm_colorAble.description=Color of the rainbow this document belongs to

The example files above define a simple "Minimum Content Model" for Alfresco. All of these files are kept within the Alfresco repository -- within the alfresco.war file set.

The next file is on the Share side and is used to define a form within Share for editing metadata specific to the custom class just defined.

tomcat\webapps\share\WEB-INF\classes\alfresco\web-extension\web-framework-config-custom.xml

<alfresco-config>

 <config evaluator="node-type" condition="mm:doc">
    <forms>
       <form>
          <field-visibility>
             <!-- inherited from cm:content -->
             <show id="cm:name" />
             <show id="cm:title" force="true" />
             <show id="cm:description" force="true" />
             <show id="mimetype" />
             <show id="cm:author" force="true" />
             <show id="size" for-mode="view" />
             <show id="cm:creator" for-mode="view" />
             <show id="cm:created" for-mode="view" />
             <show id="cm:modifier" for-mode="view" />
             <show id="cm:modified" for-mode="view" />
           
             <!--  specific for mm:doc -->
             <show id="mm:relatedDocuments" />
             <show id="mm:freeText" />

             <!--  aspect mm:colorAble -->             
             <show id="mm:color" />
           
          </field-visibility>
          <appearance>
             <field id="mm:relatedDocuments" label="Related Docs"/>
             <field id="mm:freeText" label="More Information"/>
             <field id="mm:color" label="Color">
                <control template="controls/selectone.ftl">
                   <control-param name="options">Red,Orange,Yellow,Green,Blue,Indigo,Violet</control-param>
                </control>
             </field>
          </appearance>
       </form>
    </forms>
 </config>
</alfresco-config>


With those files in place, you can checkin a document of type mm:doc from the Alfresco Explorer client and then make it available from Alfresco Share. Edit the metadata for the document, and select "Full Metadata Edit Page...".

You should see something like this:


The three new custom fields are now exposed on the Share edit form:  Related Docs, More Information, and Color.

3 comments:

  1. I am able to configure my custom types and they appear in the drop down list, but I do see one minor problem. Once the document type is changed using Change Type, it cannot be changed again. Subsequent selection of Change Type does not list the custom types, the drop down list is empty. Is this how it is supposed to work?

    ReplyDelete
  2. AFAIK That is just because you can only specialise to subtypes of your type, so once you specialise from cm:content to mm:doc you can't specialise anymore, unless you define subtypes for mm:doc

    ReplyDelete