|
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 = "-"; |
|
} |
|
} |