Example: Get and set parent items

NEW TO SITEFINITY?

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.

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:

  • 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?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?