Thursday, July 21, 2011

Learning Umbraco in a week - Day VI

 

This is my sixth post of the "Learning Umbraco in a week" series. The main agenda of this post is listed below.
  • Introduction to Umbraco API
  • Working with the Umbraco Document API
  • Working with the Umbraco Node Factory
  • Introduction to Umbraco Events
  • Working with Umbraco Member API
  • Working with Umbraco User API
  • Working with Umbraco Relationship API

Introduction to Umbraco API 
The Umbraco API give you programmatic access to everything in the Umbraco CMS. There are many ways to interact with the Umbraco API. From creating user-generated content from your web front-end via the Document API. To manage complex content relationships via the Relationship API. You can even represent the API as REST-Full Web Service Endpoints for use by your custom HTML Forms or web services. 

Working with the Umbraco Document API 
The Umbraco Document API provides programmatic access to Umbraco CMS Documents. The API provides the Document data which is stored inside the database.

1. Using the Umbraco Document API 
For using the Umbraco Document API, add the cms.dll, businesslogic.dll and umbraco.dll to your project.

Then add references to the following namespaces at the top of your .cs file.

  using umbraco.BusinessLogic;
  using umbraco.cms.businesslogic.web;


2. Creating an Umbraco Document 
The following Code Snippet creates an Umbraco Document and publish it.

  //First of all, we need to get the appropriate Document Type
  DocumentType docType = DocumentType.GetByAlias("Home");

  // Here we will get the appropriate creator of the Document.
  User author = User.GetUser(0);

  //For creating a Document we also need to mention the parent node
  //As this is the root document, we will use  -1
  Document doc = Document.MakeNew("Hello World", docType , author, -1);

  //The Document has been created
  // We will now publish the Document
  doc.Publish(author);

  //We will ask Umbraco to update the Document Cache
  umbraco.library.UpdateDocumentCache(doc.Id);

3. Modifying Document Properties 
The following Code Snippet get an Umbraco Document, modifies its properties and publish it.

  //First of all, we will get the appropriate Document
  Document doc = new Document(786);

  //Get the appropriate properties and modify the values
  doc.getProperty("bodyText").Value = "<p>Hello World!</p>";

  //The Document has been modified
  // We will now publish the Document
  User author = User.GetUser(0);
  doc.Publish(author);

  //We will ask Umbraco to update the Document Cache
  umbraco.library.UpdateDocumentCache(doc.Id);

4. Enumerating Documents Children 
The following Code Snippet get an Umbraco Document and enumerates its children.

  //First of all, we will get the appropriate Document
  Document doc = new Document(786);

  //We will check the Document's HasChildren property
  //before enumerating through the Children array
  if (doc.HasChildren) {
    //Now we will enumerate through the Children array
    Document[] children = doc.Children;
    foreach (Document childDoc in children) {
      Response.Write(childDoc.Text);
    }
  }

5. Attaching Document Event Handlers 
The following Code Snippet attach to the Document beforePublish event.

  protected void Page_Load(object sender, EventArgs e) {
     Document.BeforePublish +=
        new Document.PublishEventHandler( Document_BeforePublish );
  }

  void Document_BeforePublish(
    Document sender, umbraco.cms.businesslogic.PublishEventArgs e ) {
    Response.Write(sender.Text + " is being published");

    //We will set the EventArgs.Cancel = true, i.e. for cancelling the event
    e.Cancel = true;
  }

6. Moving a Document 
The following Code Snippet moves an Umbraco Document beforePublish event.

  //First of all, we will get the appropriate Document
  Document doc = new Document(786);

  //Move the document the new Parent Node
  int newParentNode = 1234;    doc.Move(newParentNode);

  //We will ask Umbraco to update the Document Cache
  umbraco.library.RefreshContent(); 

Working with the Umbraco Node Factory 
The Umbraco Node Factory is a strongly type presentation of the in Memory XML Cache optimized for reading (it's read-only). Which means it's very fast and only includes published content. The API provides the Node data which is published (and stored inside the Umbraco.config).

1. Using the Umbraco Node Factory 
For using the Umbraco Node Factory, add the interfaces.dll, cms.dll, businesslogic.dll and umbraco.dll to your project.

Then add references to the following namespaces at the top of your .cs file.

  using using umbraco.NodeFactory

2. Retrieving an Umbraco Node 
The following Code Snippet retrieves an Umbraco Node.

  Node node= new Node(786);
  //Use Node.GetCurrent() method to get the Current Node
  string bodyText= node.GetProperty("bodyText").Value;
  string nodeName = node.Name;
  DateTime nodeCreationDate = node.CreateDate;
  string nodeUrl = node.NiceUrl; 

Introduction to Umbraco Events 
For registering events in Umbraco, ApplicationBase class is used, as it is called automatically when Umbraco loads.

1. Using the Umbraco Events 
For using the Umbraco Events, add the cms.dll, businesslogic.dll and umbraco.dll to your project.

Then add references to the following namespaces at the top of your .cs file.

  using umbraco.BusinessLogic; 
  using umbraco.cms.businesslogic;
  using umbraco.cms.businesslogic.web;

2. Registering Events in Umbraco 
The following Code Snippet extends the ApplicationBase class and registers the event in the default constructor.

  public class AppBase : umbraco.BusinessLogic.ApplicationBase {
    public AppBase() {
      Document.BeforePublish += 
        new Document.PublishEventHandler(Document_BeforePublish);
    }

    void Document_BeforePublish(Document sender, PublishEventArgs e) {
      //TODO
    }
  }


3. Overview of All Events 
Events can be attached with the following Umbraco objects.
  • Document
  • Access
  • DocumentType
  • Domain
  • StyleSheet
  • StyleSheetProperty
  • Member
  • MemberGroup
  • MemberType
  • CMSNode
  • Dictionary
  • Media
  • MediaType
  • DataTypeDefinition
  • Language
  • Macro
  • CreatedPackage
  • InstalledPackage
  • Template
  • ContentControl
  • BaseTree
  • User
  • Content 

Working with the Umbraco Member API 
A Member in Umbraco is an external person, who has a profile on the website. Members are distinct from Umbraco Users, meaning that a Member do not have access to the Umbraco back-end. The Member API provides access to the Members data which is stored inside the database. 

1. Using the Umbraco Member API 
For using the Umbraco Member API, add the umbraco.dll to your project. 

Then add references to the following namespaces at the top of your .cs file. 

  using umbraco.cms.businesslogic.member; 

2. Creating a Member 
The following Code Snippet creates an Umbraco Member and sets a property.

  //First of all we will have to select the appropriate Member Type
  MemberType memberType = MemberType.GetByAlias("standard");

  //Create a member
  Member member = 
    Member.MakeNew("Awan", memberType, new umbraco.BusinessLogic.User(0));

  //Set a property value on the member
  member.getProperty("city").Value = "London";

  //We will ask Umbraco to update the Member Cache
  member.XmlGenerate(new System.Xml.XmlDocument());

  //Save the Member
  member.Save();

3. Adding Member to a Group 
The following Code Snippet add an Umbraco Member to a Group and then removes it.

  //First of all we will have to select the appropriate Member
   Member member = Member.GetMemberFromEmail("muneeb@email.com");

  //Get the appropriate Member Group
  MemberGroup memberGroup = MemberGroup.GetByName("admin");

  //Add Member to the Group
  member.AddGroup(memberGroup.Id);

  //Remove Member from the Group
  member.RemoveGroup(memberGroup.Id); 

Working with the Umbraco User API 
Umbraco provides access of Umbraco Back-End via Umbraco User API for writers, translators, editors and administrators. 

1. Using the Umbraco User API 
For using the Umbraco User API, add the umbraco.dll to your project. 

Then add references to the following namespaces at the top of your .cs file. 

  using umbraco.BusinessLogic;

2. Creating a User and allow Access to Umbraco Section
The following Code Snippet creates an Umbraco User and sets a property. 

  //First of all we will get the appropriate User Type
  UserType userType = UserType.GetUserType(123); 

  //Create a new User
  User.MakeNew("Awan", "awan", "secrectPass", userType);

  //Get the User via Login
  User user = new User("awan");

  //Add User Email
  user.Email = "muneeb@email.com";

  //Allow access to Umbraco Section
  user.addApplication("settings");

  //Save the User 
  user.Save();

Working with the Umbraco Relationship API 
The Umbraco Relationship API enables associations or links between Typed NodeIds be defined. For example, you may wish to make a connection between Members and Email (Document) etc.

1. Using the Umbraco Relationship API 
For using the Umbraco Relationship API, add the umbraco.dll to your project. Then we need to defien an Umbraco Relation Type
  • A row first needs to be added into the UmbracoRelationType Table in the Database (Detail available below).
  • The Relation Types Package can used to define a type of link.

It's then possible to progmatically add / remove and quickly select relationships between typed Ids. We can now add references to the following namespaces at the top of your .cs file. 

  using cms.businesslogic.relations

2. Object GUIDs for Creating Relation Types 
To setup a RelationType in Umbraco, we will need the exact GUID of the appropriate Types, e.g, Document to Member, Member to Media, Media to Media.

A list of all the built Object Types and their GUIDs is below.

ObjectType Description
C66BA18E-EAF3-4CFF-8A22-41B16D66A972            Document
B796F64C-1F99-4FFB-B886-4BF4BC011A9C Media
6FBDE604-4178-42CE-A10B-8A2600A2F07D Template
39EB0F98-B348-42A1-8662-E7EB18487560 Member
A2CB7800-F571-4787-9638-BC48539A0EFB DocumentType
366E63B9-880F-4E13-A61C-98069B029728 MemberGroup

For a complete his list of Object Types please visit, Hendy Rachers Blog.

That's all for this post, in the next post (Learning Umbraco in a week - Day VII) we will have a look at LINQ to Umbraco.

Learning Umbraco in a week - Day V

 

This is my fifth post of the "Learning Umbraco in a week" series. The main agenda of this post is listed below.
  • What are .Net Macros in Umbraco?
  • How to use .Net User Controls in Umbraco?
  • How to Parametrize a Macro? 
  • Communication between Umbraco and User Control
  • How to Debug a Macro in Umbraco?
  • How to Cache Macro result in Umbraco? 

What are .Net Macros in Umbraco? 
.Net Macros are Umbraco's means of inserting .Net Controls into templates or content, to start. They are also the carrier for custom .NET controls. 

How to use .Net User Controls in Umbraco? 
Using .Net Controls in Umbraco is a two phase process, the phase are listed below. 

Phase 1 - Copying Files
    • First you need to copy your control. Whether it’s a User Control or a Custom Control you’ll need to copy the assembly (dll) into the /bin folder of your Umbraco installation.
    • If it’s a User Control you’ll need to copy the .ascx file as well.
    • Your installation already contains a / User Controls folder which is recommend to be used, but you’re free to place them anywhere in your application.



    Phase 2 - Registering a .Net User Control as Macro
    • Go to Umbraco Default Page (Admin Page) > Sections > Developer
    • Right click on the Macros folder, and select Create.
    • You will receive a dialog where you can specify the name of the Macro to be created.
    • Specify the name of the new Macro, check Create Macro. Click Create.
    • Now select the macro from the list "Browse usercontrols on server…".
    • You have now created the Macro for rendering the content.



    How to Parametrize a Macro? 
    It is possible to pass values from to a Macro in Umbraco. Values can be passed through the following ways.
    1. Current Page node
    2. Request collection
    3. Cookies 

    To pass the Current Page's property with the alias "bodyText", the following syntax is used.

    <umbraco:macro alias = "RenderProperties" pagevalue = "[#bodyText]" runat = "server"/>

    Syntax for passing parameters through different ways is as follows.
      • To insert page value: [#propertyAlias]
      • To insert recursive page value: [$propertyAlias]
      • To insert cookie value: [%cookieValueKey]
      • To insert value from request collection: [@requestValueKey]
      • For passing multiple values <umbraco:macro value = "[#propertyAlias], [#propertyAlias2], my static string" /> 
       
      Communication between Umbraco and User Control
      Communication between Umbraco and User Control is done by creating Public Properties in your control and match them with Macro elements. Umbraco will even create the UI when inserting the Macro.
       
      It’s important to note that the Alias of your Macro element matches the Name of your Public Property. Moreover, be aware of case sensitivity.


      How to Debug a Macro in Umbraco? 
      NET controls are wrapped in Umbraco Macros and suddenly is out of the hands of Visual Studio doesn't mean you cannot use your favorite debugging practices, i.e. you can always utilize the Microsoft Visual Studio "Attach to Process" feature and debug .Net Macro.

      For example, if Umbraco is hosted on Microsoft IIS then you will have to attach to "w3wp.exe", and if Umbraco is hosted on Microsoft WebMatrix then you will have to attach to "WebMatrix.exe".




      How to Cache Macro result in Umbraco? 
      If you are using a User Control or XSLT, and have the Macro set to cache, it will cache the results of the control for the determined time.


      That's all for this post, in the next post (Learning Umbraco in a week - Day VI) we will have a look at the Umbraco API.

      Learning Umbraco in a week - Day III

       

      This is my fifth post of the "Learning Umbraco in a week" series. The main agenda of this post is listed below.
      • Introduction to XSLT
      • Introduction to XPath
      • Basic Structure of XSLT
      • How to link XSL Stylesheet to an XML Document?
      • How to apply XSL Transform?

      Introduction to XSLT 
      XSLT is a document that is applied programmatically to an XML Document to manipulate the data. However, the structure of XSLT resembles that of XML, and contains specialized tags to perform specific actions to the data. 

      Introduction to XPath 
      XPath is a language for navigating in XML documents. Moreover, XPath is used by XSLT to find information in an XML document. 

      Basic Structure of XSLT 
      The structure of XSLT resembles that of XML, and contains specialized tags to perform specific actions to the data. 

      1. Top Declarations 
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
              <xsl:stylesheet     version="1.0"     
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:msxml="urn:schemas-microsoft-com:xslt"
                  xmlns:umbraco.library="urn:umbraco.library"
                  exclude-result-prefixes="msxml umbraco.library">
              <xsl:output method="xml" omit-xml-declaration="yes" />
       

      2. The XML declaration
          <?xml version="1.0" encoding="UTF-8"?>

      3. The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          or
          <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      4. Parameter Declaration
          This is optional but a very important declaration, as it is the default link to the content published in the site.

          <xsl:param name="currentParam" />

      5. Template Declaration
          This is the portion that is responsible for the processing and the output.

          <xsl:template match="/">

          …
          </xsl:template>

      6. Closing the Style Sheet
          We close out the stylesheet, since this is XML, everything must be properly structured.

          </xsl:stylesheet>

          or
          </ xsl:transform >
       


      How to link XSL Stylesheet to an XML Document? 
      To link XSL Stylesheet to an XML Document, add the XSL style sheet reference to the XML document, e.g. "sample.xml".

          <?xml-stylesheet type="text/xsl" href=“sample.xsl"?>

      How to apply XSL Transform? 
      The most easiest way to apply XSL Transforms over an XML Document or Node is by using the Template Element. 

      A sample XML Document or Node used in the examples.

          <?xml version="1.0" encoding="ISO-8859-1"?>

          <catalog>
              <cd>
                  <title>Empire Burlesque</title>
                  <artist>Bob Dylan</artist>
                  <country>USA</country>
                  <company>Columbia</company>
                  <price>10.90</price>
                  <year>1985</year>
              </cd>
              . . .
          </catalog> 

      A list of XSL Elements commonly used in Transformations are listed below. 

      1. The <xsl:template> Element 
      A template contains rules to apply when a specified node is matched. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

      For example

          <xsl:template match="/">
              <h2>My CD Collection</h2>

              <table border="1">
                  <tr bgcolor="#9acd32">
                      <th>Title</th>
                      <th>Artist</th>
                  </tr>  
                  …
              </table>

          </xsl:template>

      2. The <xsl:value-of> Element 
      The <xsl:value-of> element is used to extract the value of a selected node. The select attribute contains an XPath expression. An XPath expression works like navigating a file system; a forward slash (/) selects subdirectories.

      For example

          <xsl:template match="/">

              <h2>My CD Collection</h2>
              <table border="1">
                  <tr bgcolor="#9acd32">
                      <th>Title</th>
                      <th>Artist</th>
                  </tr>
                  <tr>
                      <td>
                          <xsl:value-of select="catalog/cd/title"/>
                      </td>
                      <td>
                          <xsl:value-of select="catalog/cd/artist"/>
                      </td>
                  </tr>
              </table>
          </xsl:template>

      3. The <xsl:for-each> Element 
      The <xsl:for-each> element allows you to do looping in XSLT. Filtering the output by adding a criterion to the select attribute in the <xsl:for-each> element.

          <xsl:for-each select="catalog/cd[artist='Bob Dylan']">


      Legal filter operators in XSLT are listed below.
      • =  (equal)
      • != (not equal)
      • &lt; less than
      • &gt; greater than

      For example

          <xsl:template match="/">

              <h2>My CD Collection</h2>
              <table border="1">
                  <tr bgcolor="#9acd32">
                      <th>Title</th>
                      <th>Artist</th>
                  </tr>
                  <xsl:for-each select="catalog/cd">
                      <tr>  
                          <td><xsl:value-of select="title"/></td>
                          <td><xsl:value-of select="artist"/></td>
                      </tr>
                  </xsl:for-each>
              </table>
          </xsl:template>

      4. The <xsl:sort> Element 
      The <xsl:sort> element is used to sort the output. To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element. The select attribute indicates what XML element to sort on.

      For example

          <xsl:template match="/">

              <h2>My CD Collection</h2>
              <table border="1">
                  <tr bgcolor="#9acd32">
                      <th>Title</th>
                      <th>Artist</th>
                  </tr>
                  <xsl:for-each select="catalog/cd">
                      <xsl:sort select="artist"/>
                          <tr>
                              <td><xsl:value-of select="title"/></td>
                              <td><xsl:value-of select="artist"/></td>
                          </tr>
                      </xsl:sort>
                  </xsl:for-each>
              </table>
          </xsl:template>

      5. The <xsl:if> Element 
      The <xsl:if> element is used to put a conditional test against the content of the XML.

      <xsl:if test="expression">
          ...some output if the expression is true...
      </xsl:if>

      The value of the required test attribute contains the expression to be evaluated.

      For example

          <xsl:template match="/">

              <h2>My CD Collection</h2>
              <table border="1">
                  <tr bgcolor="#9acd32">
                      <th>Title</th>
                      <th>Artist</th>
                  </tr>
                  <xsl:for-each select="catalog/cd">
                      <xsl:if test="price &gt; 10">
                          <tr>
                              <td><xsl:value-of select="title"/></td>
                              <td><xsl:value-of select="artist"/></td>
                          </tr>
                      </xsl:if>     
                  </xsl:for-each>   
              </table>   
          </xsl:template>

      6. The <xsl:choose> Element 
      The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.

      <xsl:choose>

          <xsl:when test="expression">
              ... some output ...
          </xsl:when>
          <xsl:otherwise>
              ... some output ....
          </xsl:otherwise>
      </xsl:choose>

      The value of the required test attribute contains the expression to be evaluated.

      For example

          <xsl:template match="/">

              <h2>My CD Collection</h2>
              <table border="1">
                  …
                  <xsl:for-each select="catalog/cd">
                      <tr>
                          <td><xsl:value-of select="title"/></td>
                          <xsl:choose>
                              <xsl:when test="price &gt; 10">
                                  <td bgcolor="#ff00ff"><xsl:value-of select="artist"/></td>
                              </xsl:when>
                              <xsl:otherwise>
                                  <td><xsl:value-of select="artist"/></td>
                              </xsl:otherwise>
                          </xsl:choose>
                      </tr>
                  </xsl:for-each>
              </table>
          </xsl:template>

      7. The <xsl:apply-templates> Element 
      The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.

      For xample

          <xsl:template match="/">

             <html>
                  <body>
                      <h2>My CD Collection</h2>
                       <xsl:apply-templates/>
                  </body>
              </html>
          </xsl:template>

          <xsl:template match="cd">
              <p>
                  <xsl:apply-templates select="title"/>
                  <xsl:apply-templates select="artist"/>
              </p>
          </xsl:template>

          <xsl:template match="title">

              Title: <span style="color:#ff0000"><xsl:value-of select="."/></span>
              <br />
          </xsl:template>
          <xsl:template match="artist">
              Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span>   
              <br />
          </xsl:template> 

      That's all for this post, in the next post (Learning Umbraco in a week - Day IV) we will have a look at the XSLT Macros in Umbraco.

      Learning Umbraco in a week - Day IV

       

      This is my fourth post of the "Learning Umbraco in a week" series. The main agenda of this post is listed below.

      • What are Umbraco Macros?
      • How to use Macros as building blocks in Umbraco?
      • How Umbraco uses XSLT?
      • Creating XSLT Macro
      • Understanding the "currentPage" parameter
      • Understanding the XML Document Schema (v4.7)
      • XPATH Axes in Umbraco
      • A look at XSLT Macro Snippets

      What are Umbraco Macros?
      Macros are used to get and display data from Umbraco. For example, site maps, navigation, and news lists. This can be done using XSLT. Umbraco includes several pre-built XSLT Macros, and you can also create your own.

      Umbraco Macros can also be used to wrap .NET user controls or custom controls, to include those in your Web Page. 

      How to use Macros as building blocks in Umbraco?
      In Umbraco, Macros are small building blocks of functionality that can be dropped into an editor. Each Macro encompasses a piece of functionality, and provides a simple interface to be able to modify the macro to you requirements. Importantly, Macros can be placed in transforms, templates, and rich-text, and the Umbraco GUI provides a button for this in the relevant section.




      How Umbraco uses XSLT?
      Umbraco utilizes XSLT to dynamically render content such as navigational structures, lists, and nearly anything you can dream of. This is accomplished through the use of Macros.




      Creating XSLT Macro
      Creating a Macro in Umbraco is very easy.




      For creating an XSLT Macro in Umbraco, follow the steps below.
      1. Go to Umbraco Default Page (Admin Page) > Sections > Developer > XSLT Files.
      2. Right click on the XSLT Files node, and select create.
      3. You will receive a dialog where you can specify the name, select an XSLT template, and specify if you want to automatically create the Macro for this template.
      4. Specify the name of the new XSLT file, check Create Macro, and select a template if you desire. Click Create.
      5. You have now created the XSLT file and the Macro for rendering the Content.



        Understanding the "currentPage" parameter
        The currentPage parameter is the complete XML document of the published site, and is how we reference the data stored in each document.

        It is important to note that, by default, the context of the XML document is set to the requested Web Page. The XML document, as mentioned before, contains all the content of the published documents. This data is stored in the XML document structured in the same manner as your tree is laid out in Umbraco. So, documents are nested to create the hierarchy that we can easily use. 

        An Umbraco Node (The "currentPage" parameter) consists of several common pieces of data, and they are listed below.
        1. id
        2. version
        3. parentID
        4. level
        5. writerID
        6. creatorID
        7. nodeType
        8. template sortOrder
        9. createDate
        10. updateDate
        11. nodeName
        12. urlName
        13. writerName
        14. creatorName
        15. nodeTypeAlias path
        All these are store as Attributes of the Document (Also called "Node").

        Moreover, the Properties that are added to the Document in Umbraco, and what the user edits are referenced as "Data". Data consists of a couple common pieces of data, which are Attributes on the data elements.  These are listed below.
        1. alias
        2. versionID
        Understanding the XML Document Schema (v4.7)
        In Umbraco, each Document Type has it's own Node using the Document Type alias - <Home> as opposed to the previous convention <node documentTypeAlias="home">.

        Each Property has it's own Node underneath the Document Type node <umbracoNaviHide> as opposed to the previous convention <data alias="umbracoNaviHide">.

        It is important to note that, the difference between a Document Type Node and a Property Node is that the Document Type Node has the blank attribute isDoc <home isDoc"">


        XPATH Axes in Umbraco
        XPATH works on the premise of Axes, which is how the data relates to the Current Node in Umbraco as well.



        • Self Axis - While it is rarely used, the self axis actually returns the node in reference.
                                     $currentPage/self::node
        • Child Axis - The child axis select the nodes immediately below the node in reference. While the verbose method is rarely used, it is here for reference.
                                     $currentPage/child::node
                                     $currentPage/node
        • Parent Axis - The parent axis allows us to see the node immediately above the node in reference.
                                     $currentPage/parent::node 
                                     $currentPage/../
        • Descendant Axis - The descendant axis retrieves all nodes below the node in reference no matter the depth.
                                     $currentPage/descendant::node 
                                     $currentPage//node
        • Descendant-or-Self Axis - The descendant-or-self axis returns all nodes below the current node, but also returns the node in reference to the command..
                                     $currentPage/ descendant-or- self::node
        • Ancestor Axis - The ancestor axis selects all nodes that are ancestors, or the parent, and the parent's parent, and so on, to the node in reference..
                                     $currentPage/ancestor::node
        • Ancestor-or-Self Axis - The ancestor-or-self axis selects all nodes that are ancestors, or the parent, and the parent's parent, and so on, including the node in reference.
                                     $currentPage/ancestor-or-self::node
        • Preceding Axis - The preceding axis selects all nodes no matter the depth, that are located on parent-level and who are also located before (preceding) its parent of the node in reference.
                                     $currentPage/preceding::node
        • Preceding-Sibling Axis - The preceding-sibling axis selects all nodes that are located on the same level who are also located before (preceding) the node in reference..
                                     $currentPage/ preceding-sibling ::node
        • Following Axis - The following axis selects all nodes no matter the depth, that are located on parent-level and who are also located after (following) its parent of the node in reference.
                                     $currentPage/following::node
        • Following-Sibling Axis - The following-sibling axis selects all nodes that are located on the same level who are also located before (following) the node in reference.
                                     $currentPage/following-sibling::node


        A look at XSLT Macro Snippets
        For a practical understanding of XSLT Macros, a list of XSLT Macro snippets is provided below.

        1. For each child Node of a specific Document Type Alias 

        <xsl:for-each select="$currentPage/NewsItem"> </xsl:for-each>

        However for whatever reason if you want to have a document type alias and a document type property with the same alias it would select both.

        <xsl:for-each select="$currentPage/NewsItem [@isDoc]"> </xsl:for-each>

        2. For each child Node excluding a specific Document Type Alias 
        <xsl:for-each select = "$currentPage/*[not(self::NewsItem)]" > </xsl:for-each>

        3. For each child Node 

        <xsl:for-each select="$currentPage/* [@isDoc]"> </xsl:for-each>

        This example selects all xml nodes underneath the currentPage item using the * selector.

        However without the [@isDoc] section it would select all nodes including those which are just document type properties.

        4. For each child Node via a selectable source 

        This example shows you how to list childnodes from the nodeID of the contentpicker.

        <xsl:param name="Source" select = "$currentPage/mySource" />
        <xsl:for-each select = "umbraco.library:GetXmlNodeById($Source)/DocType">
        </xsl:for-each>

        5. For Each node of a specific Document Type Alias 

        <xsl:for-each select="$currentPage/ancestor-or-self::Home//MyDocumentType [@isDoc]">
        </xsl:for-each>

        This example walks upto the top level node/dcoument type with the alias of Home. So this is a fairly expensive XPath call.

        Then it looks through all the child nodes no matter how deep to find the node (document type) with the alias of MyDocumentType.

        6. Using a Macro parameter to get a Document Type Property 

        <xsl:value-of select="$currentPage/* [name() = $myMacroParameter and not(@isDoc)]" />

        This example selects the child node where it's name matches the value in myMacroParameter and checks to see if that it does not have the isDoc attribute.

        7. Using GetMedia Umbraco Method to load Images 

        This code loops through the images in a media folder as specified by a media picker.

        <xsl:for-each select="umbraco.library:GetMedia($images, true())/Image">
            <xsl:variable name="picFile" select="umbracoFile"/>
            <xsl:variable name="picW" select="umbracoWidth"/>
            <xsl:variable name="picH" select="umbracoHeight"/>
            <img >
                <xsl:attribute name="src">
                    <xsl:value-of select = "$picFile"/>
                </xsl:attribute>
            </img>
        </xsl:for-each> 

        That's all for this post, in the next post (Learning Umbraco in a week - Day V) we will have a look at .Net Macros in Umbraco.