London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!
AI OnAI Off
London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!
Hi,
In your example, it looks like you're creating the block as a child of the articlePage where you should be creating it in that page's asset folder. The asset folder doesn't exist by default but you can get or create it as a single action like this:
var contentAssetHelper = ServiceLocator.Current.GetInstance<ContentAssetHelper>();
var assetFolderLink = contentAssetHelper.GetOrCreateAssetFolder(articlePage.ContentLink).ContentLink;
You can then use assetFolderLink as the parent of your new block in your GetDefault() call.
Hi,
i'm trying to create content in a ContentArea of a page. I want to store this block in "for this page", as if the user would create it there manually.
But i get an exception "...is not allowed to be created under ArticlePage ..." at _contentRepository.Save. So how to do it correctly? This is my code:
private ContentReference CreateArticleContent(MappedArticleEntry articleEntry, ArticlePage articlePage) { if (string.IsNullOrWhiteSpace(articleEntry.Content)) { this.LogError($"Parsed article-content is null or empty. {nameof(MappedArticleEntry.NewsId)} <{articleEntry.NewsId}>"); return null; } CultureInfo culture = articleEntry.Language.TryParseCultureInfo(out CultureInfo ci) ? ci : ArticlePageImportActionCreate.DefaultCulture; try { IContent block = (IContent)_contentRepository.GetDefault<ContentTemplateBlock.ContentTemplateBlock>(articlePage.GetContentLink(), culture); block.Name = $"{_ARTICLE_CONTENT_BLOCK_NAME_PREFIX}{articlePage.GetContentLink()?.ID.ToString() ?? articleEntry.NewsId}"; block.HtmlContent = new XhtmlString(articleEntry.Content); ContentReference newArticleContentReference = _contentRepository.Save(block, SaveAction.Publish, AccessLevel.NoAccess); this.LogInformation($"Created article-content-block for <{articleEntry.NewsId}> in culture <{culture}> with Length <{articleEntry.Content.Length}>. Generated block-id <{newArticleContentReference.ID}>"); return newArticleContentReference; } catch (Exception e) { this.LogError(e, $"Error creating article-content-block for <{articleEntry.NewsId}> in culture <{culture}> with Length <{articleEntry.Content.Length}>."); return null; } }