Hey Lars,
You can use ContentAssetHelper class to create 'For this page' folder like this:
var contentAssetHelper = ServiceLocator.Current.GetInstance<ContentAssetHelper>();
// get an existing content asset folder or create a new one
var assetsFolder = contentAssetHelper.GetOrCreateAssetFolder(myPage.ContentLink);
So first you must create the myPage and then save/publish it, and after that you can call contentAssetHelper.GetOrCreateAssetFolder. ContentAssetHelper will automatically attach that folder for you.
Can I use this contentAssetHelper also to create a content folder programmetically in the Media Panel and give it a specif name?
I found an article about this for EPiServer E-Commerce but this is not working for CMS unfortunality:
http://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-Commerce/75/Content/Assets-and-media/Legacy-Asset-Management/Legacy-asset-management/
Johan Viklund has just wrote this neat function;
/// <summary> /// Returns a <c>ContentFolder</c> folder /// </summary> /// <param name="parentFolder">The folder container.</param> /// <param name="folderName">Identifier for folder.</param> /// <param name="languageId">Identifier for language branch.</param> /// <returns>Stored <c>ContentFolder</c> folder; otherwise created folder.</returns> private ContentFolder GetOrCreateFolder(ContentReference parentFolder, string folderName, string languageId) { var languageSelector = new LanguageSelector(languageId); var storedFolder = _contentRepository.GetChildren<ContentFolder>(parentFolder, languageSelector) .FirstOrDefault(f => string.Compare(f.Name, folderName, StringComparison.OrdinalIgnoreCase) == 0); if (storedFolder != null) { return storedFolder; } var parent = _contentRepository.Get<ContentFolder>(parentFolder, languageSelector); var folder = _contentRepository.GetDefault<ContentFolder>(parent.ContentLink, languageSelector); folder.Name = folderName; var folderReference = _contentRepository.Save(folder, EPiServer.DataAccess.SaveAction.Publish, AccessLevel.NoAccess); return _contentRepository.Get<ContentFolder>(folderReference); }
Thanks for sharing, Miriam! Here's a little tip to make it even better: Use GetBySegment to find any existing content, instead of iterating the children using GetChildren. This will greatly improve performance if there are lots of children. Something like this:
private ContentFolder GetOrCreateFolder(ContentReference parentFolder, string folderName, string languageId) { var languageSelector = new LanguageSelector(languageId); var storedFolder = _contentRepository.GetBySegment(parentFolder, folderName, languageSelector) as ContentFolder; if (storedFolder != null) { return storedFolder; } storedFolder =_contentRepository.GetDefault<ContentFolder>(parentFolder, languageSelector); storedFolder.Name = folderName; _contentRepository.Save(storedFolder, EPiServer.DataAccess.SaveAction.Publish, AccessLevel.NoAccess); return storedFolder; }
Per, as per Episerver 9.0.3, the call of IContentRepository.GetDefaul<T> should be as follows:
storedFolder =_contentRepository.GetDefault<ContentFolder>(parentFolder, languageSelector.Language);
We have a page type that when created should have some blocks already populated in its ContentArea. These blocks should be programmatically created and published in the "For this page" assets folder upon page creation. We have to check whether the page already has this folder or not. If it doesn't have the folder it must be programmatically created as well. The howto is explained here: http://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-CMS/75/Content/Assets-and-media/Content-assets-and-folders/. We have used a slightly different implementation:
The problem is that this programmatically created assets folder differs from the default "For this page" assets folder in two key ways:
- The icon is a standard blue folder, just like any other global folder (unfortunate as it doesn't give the editor the well known visual clue that this is the local assets folder for the page)
- The folder has the same options available as a global folder, hence it can be deleted (unlike the default "For this page" assets folder).
Is there any way I can make this programmatically created folder look and behave similar to the default "For this page" folder?