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