# Example: Retrieve and create child items

This example uses a *Music Collection* module with the following hierarchical structure:

* The module is named *Music Collection*.
* The module has content type *Music Artist*. It holds information about different singers or bands. In this example, it has only one field *Name* that holds the name of a band or a singer.
* The module has content type *Album* with parent content type *Music Artist*. The *Album* content type has only one field *Title* that contain the title of an album.
* The module has content type *Song* with a parent content type *Album*. The *Song* content type has only one field *Name* that holds the name of a song.

> info **NOTE:** For demonstration purposes, this example uses only one field for every content type, but there is no limitation to how many fields a content type can have. For more details about using your content type with more fields, use the *Code reference* link on the module page.

After you create the *Music Collection* module with the Module Builder, you can create some artists, albums, and songs, using the following code snippet:

* ```C#
    using System;
    using System.Text.RegularExpressions;
    using Telerik.Sitefinity.DynamicModules;
    using Telerik.Sitefinity.DynamicModules.Model;
    using Telerik.Sitefinity.Lifecycle;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Security;
    using Telerik.Sitefinity.Utilities.TypeConverters;
    
    namespace Telerik.Sitefinity.Documentation.CodeSnippets.SFTools.TemplateBuilder.HierarchicalDM
    {
        public partial class HierarchicalDynamicModuleSnippets
        {
            public void RetrieveChildHierarchyExample()
            {
                DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
    
                // Creating an singer.
                Type musicArtistType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MusicCollection.MusicArtist");
                DynamicContent johnLennon = this.CreateNewMusicArtist("John Lennon", musicArtistType, dynamicModuleManager);
    
                // Creating an album for that singer.
                Type albumType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MusicCollection.Album");
                DynamicContent imagineAlbum = this.CreateNewAlbum("Imagine", albumType, musicArtistType, johnLennon.Id, dynamicModuleManager);
                DynamicContent wallsAndBridgesAlbum = this.CreateNewAlbum("Walls and Bridges", albumType, musicArtistType, johnLennon.Id, dynamicModuleManager);
    
                // Adding songs to the albums.
                Type songType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MusicCollection.Song");
    
                // Add two songs to 'Imagine' album.
                DynamicContent imagineSong = this.CreateNewSong("Imagine", songType, albumType, imagineAlbum.Id, dynamicModuleManager);
                DynamicContent scaredSong = this.CreateNewSong("Crippled Inside", songType, albumType, imagineAlbum.Id, dynamicModuleManager);
    
                // Add two songs to the 'Walls and Bridges' album.
                DynamicContent oldDirtRoadSong = this.CreateNewSong("Old Dirt Road", songType, albumType, wallsAndBridgesAlbum.Id, dynamicModuleManager);
                DynamicContent whatYouGotSong = this.CreateNewSong("What You Got", songType, albumType, wallsAndBridgesAlbum.Id, dynamicModuleManager);
            }
    
            /// <summary>
            /// Creates a new music artist and publishes it through Sitefinity workflow.
            /// </summary>
            /// <param name="artistName">The name of the artist.</param>
            /// <param name="musicArtistType">The content type representing the 'Music Artist' content.</param>
            /// <param name="manager">Module Builder manager.</param>
            /// <returns>The 'Master' version of the newly created 'Music Artist' content item.</returns>
            private DynamicContent CreateNewMusicArtist(string artistName, Type musicArtistType, DynamicModuleManager manager)
            {
                DynamicContent artistItem = manager.CreateDataItem(musicArtistType);
    
                // This is how values for the properties are set.
                artistItem.SetValue("Name", artistName);
                artistItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
                artistItem.SetValue("PublicationDate", DateTime.Now.ToUniversalTime());
                artistItem.SetValue("UrlName", new Lstring(Regex.Replace(artistName, UrlNameCharsToReplace, UrlNameReplaceString)));
    
                // Publishing the music artist.
                ILifecycleDataItem publishedArtistItem = manager.Lifecycle.Publish(artistItem);
                artistItem.SetWorkflowStatus(manager.Provider.ApplicationName, "Published");
    
                // You need to call SaveChanges() in order for the items to be actually persisted to database.
                manager.SaveChanges();
    
                return artistItem;
            }
    
            /// <summary>
            /// Creates a new album for the specified artist and publishes it through Sitefinity workflow.
            /// </summary>
            /// <param name="albumName">The name of the album.</param>
            /// <param name="albumType">The content type representing the 'Album' content.</param>
            /// <param name="musicArtistType">The content type representing the 'Music Artist' content.</param>
            /// <param name="artistId">The ID of the 'Master' version of the 'Music Artist' content item to which the album will be assigned.</param>
            /// <param name="manager">Module Builder manager.</param>
            /// <returns>The 'Master' version of the newly created 'Album' content item.</returns>
            private DynamicContent CreateNewAlbum(string albumName, Type albumType, Type musicArtistType, Guid artistId, DynamicModuleManager manager)
            {
                DynamicContent albumItem = manager.CreateDataItem(albumType);
    
                // This is how the parent is set for an item.
                albumItem.SetParent(artistId, musicArtistType.FullName);
    
                // This is how values for the properties are set.
                albumItem.SetValue("Title", albumName);
                albumItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
                albumItem.SetValue("PublicationDate", DateTime.Now.ToUniversalTime());
                albumItem.SetValue("UrlName", new Lstring(Regex.Replace(albumName.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString)));
    
                // Publishing the album.
                ILifecycleDataItem publishedAlbumItem = manager.Lifecycle.Publish(albumItem);
                albumItem.SetWorkflowStatus(manager.Provider.ApplicationName, "Published");
    
                // You need to call SaveChanges() in order for the items to be actually persisted to database.
                manager.SaveChanges();
    
                return albumItem;
            }
    
            /// <summary>
            /// Creates a new song for the specified album and publishes it through Sitefinity workflow.
            /// </summary>
            /// <param name="songName">The name of the song.</param>
            /// <param name="songType">The content type representing the 'Song' content.</param>
            /// <param name="albumType">The content type representing the 'Album' content.</param>
            /// <param name="albumId">The ID of the 'Master' version of the 'Album' content item to which the song will be assigned</param>
            /// <param name="manager">Module Builder manager.</param>
            /// <returns>The 'Master version of the newly created 'Song' content item.</returns>
            private DynamicContent CreateNewSong(string songName, Type songType, Type albumType, Guid albumId, DynamicModuleManager manager)
            {
                DynamicContent songItem = manager.CreateDataItem(songType);
    
                // This is how the parent is set for an item.
                songItem.SetParent(albumId, albumType.FullName);
    
                // This is how values for the properties are set.
                songItem.SetValue("Name", songName);
                songItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
                songItem.SetValue("PublicationDate", DateTime.Now.ToUniversalTime());
                songItem.SetValue("UrlName", new Lstring(Regex.Replace(songName.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString)));
    
                // Publishing the song.
                ILifecycleDataItem publishedSongItem = manager.Lifecycle.Publish(songItem);
                songItem.SetWorkflowStatus(manager.Provider.ApplicationName, "Published");
    
                // You need to call SaveChanges() in order for the items to be actually persisted to database.
                manager.SaveChanges();
    
                return songItem;
            }
    
            // Constants used to set the URLs for DynamicContent items.
            private const string UrlNameCharsToReplace = @"[^\w\-\!\$\'\(\)\=\@\d_]+";
            private const string UrlNameReplaceString = "-";
        }
    }
    
  ```

The code above executes the *Code reference* examples to create a singer, albums, and songs, to sets their relationships, and to publish them through Sitefinity CMS workflow.
