I'm writing a tool that will import the data (some text and images) from external systems into EPiServer as regular page types.
My page type has serveral ContentReference properties that are marked as required. How can I create EPiServer pages from the code and have a nice Assets folder structure?
My idea was to create the ContentAssetFolder first:
var assetsFolder = contentRepository.GetDefault<ContentAssetFolder>(SiteDefinition.Current.SiteAssetsRoot);
Then to upload all the images to that folder.
Then to create a new page with all required fileds and to attach ContentAssetFolder to this page.
If I do it like that, my content asset folders will have the following structure:
- folder 1
- folder 2
- folder 3
And I would like them to follow my current page structure:
- folder 1
- folder 2
- - folder 3
http://s2.postimg.org/8nwqlru8l/image.png vs http://s29.postimg.org/o3ei48aef/image.png
How can I accomplish that?
And do I have to create a ContentAssetFolder at all? In edit mode, when I insert an image inside rich-text editor field, it appears as a resource inside 'For This Page' folder, but EPiServer didn't create a new folder inside 'For This Site' folder. Can I accomplish the same?
I managed to solve this using ContentAssetHelper:
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contentAssetHelper = ServiceLocator.Current.GetInstance<ContentAssetHelper>();
var page = ... get page using content repository
var assetsFolder = contentAssetHelper.GetOrCreateAssetFolder(page.ContentLink);
var blobFactory = ServiceLocator.Current.GetInstance<BlobFactory>();
var imagefile = contentRepository.GetDefault<ImageFile>(assetsFolder.ContentLink);
imagefile.Name = "some name";
blob = blobFactory.CreateBlob(imagefile.BinaryDataContainer, fileExtension);
blob.Write(file.InputStream); // file.InputStream = Stream of the file that we want to import
imagefile.BinaryData = blob;
fileContentReference = contentRepository.Save(imagefile, SaveAction.Publish, AccessLevel.NoAccess);
page.SomeImageProperty = fileContentReference;
contentRepository.Save(page, SaveAction.Publish, AccessLevel.Read);