Example: Get and set parent 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 Namethat holds the name of a band or a singer.
  • The module has content type Albumwith parent content type Music Artist. The Albumcontent type has only one field Titlethat contain the title of an album.
  • The module has content type Songwith a parent content type Album. The Song content type has only one field Namethat holds the name of a song.

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, use the following code snippet to create an artist and assign an album to it:

  • C#
      using System;
      using Telerik.Sitefinity.Model;
      using Telerik.Sitefinity.DynamicModules;
      using Telerik.Sitefinity.DynamicModules.Model;
      using Telerik.Sitefinity.Utilities.TypeConverters;
      using Telerik.Sitefinity.Security;
      using Telerik.Sitefinity.Lifecycle;
      using System.Text.RegularExpressions;
      
      namespace Telerik.Sitefinity.Documentation.CodeSnippets.SFTools.TemplateBuilder.HierarchicalDM
      {
          public partial class GettingAndSettingParrentItems
          {
              public void CreateHierarchicalItems()
              {
                  DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
      
                  // Creating an singer.
                  Type musicArtistType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MusicCollection.MusicArtist");
                  DynamicContent johnLennon = CreateNewMusicArtist("John Lennon", musicArtistType, dynamicModuleManager);
      
                  // Creating an album for that singer.
                  Type albumType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MusicCollection.Albums");
                  DynamicContent albumItem = dynamicModuleManager.CreateDataItem(albumType);
      
                  // To set a parent for an item pass the ID of the 'Master' version of the parent item.
                  albumItem.SetParent(johnLennon.Id, albumType.FullName);
      
                  // This is how values for the properties are set.
                  string albumTitle = "Imagine";
                  albumItem.SetValue("Title", albumTitle);
                  albumItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
                  albumItem.SetValue("PublicationDate", DateTime.Now.ToUniversalTime());
                  albumItem.SetValue("UrlName", new Lstring(Regex.Replace(albumTitle.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString)));
      
                  // Publishing the album.
                  ILifecycleDataItem publishedAlbumItem = dynamicModuleManager.Lifecycle.Publish(albumItem);
                  albumItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");
      
                  dynamicModuleManager.SaveChanges();
              }
      
              /// <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");
      
                  manager.SaveChanges();
      
                  return artistItem;
              }
      
              // Constants for helping to set the URL for DynamicContent items.
              private const string UrlNameCharsToReplace = @"[^\w\-\!\$\'\(\)\=\@\d_]+";
              private const string UrlNameReplaceString = "-";
      
          }
      }
      
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?