Creating an assets folder

Vote:
 

I'm trying to create a data folder under the Media tab to store some xml files.  I'm able to create my xml files on the root of the assets folder without any issues.

I've found example code that just says 2 lines, not really helpful.

https://world.episerver.com/documentation/developer-guides/CMS/Content/assets-and-media/Content-assets-and-folders/

How do I create a folder called XMLFiles?  

I assume once it's created I'd use this to get the assets root:

_contentRepository.GetDefault<GenericMedia>(SiteDefinition.Current.SiteAssetsRoot);

Then look for a content somehow named XMLFiles?

#201620
Feb 25, 2019 10:00
Vote:
 

You should be able to create a content of type ContentFolder under the SiteAssetsRoot. Something like

var folder = _contentRepository.GetDefault<ContentFolder>(SiteDefinition.Current.SiteAssetsRoot);

folder.Name = "XMlFiles";

_contentRepository.Save(...);

#201623
Feb 25, 2019 11:35
Vote:
 

Hi Quan Mai,

What should be the access level while saving that? Or, am I doing something wrong?

I tried to implement it in Alloy Site in Initialization Module. (Please see below screenshot)

Thanks,

Praful

#201660
Feb 26, 2019 14:08
Vote:
 

You are requiring the user to run the code to have Administer access right. It might make senses to run this code with AccessLevel.NoAccess

#201664
Feb 26, 2019 14:16
Vote:
 

Oh,

Great, I got this worked. I see folder is created.

Thanks.

#201666
Feb 26, 2019 14:20
Vote:
 

Just a note: because you are running this in an initialization module, you should check for the folder for existence before creating it 

#201667
Feb 26, 2019 14:23
Vote:
 

Yeah, that totally make sense. Thanks for reminding.

#201668
Feb 26, 2019 14:25
Vote:
 

Brilliant thank you.  I knew it would be something obvious once someone helped out.  

How would I go about putting the folder in the "For This Site" rather than "For All Sites".  Doesn't seem to make a difference if I use SiteDefinition.Current.GlobalAssetsRoot or SiteDefinition.Current.SiteAssetsRoot.

#201673
Feb 26, 2019 15:47
Vote:
 

I think the reason is that you only have one site configured. If you have multiple sites configured then For This site and For All sites will be distinct 

#201675
Feb 26, 2019 16:01
Vote:
 

When running via the scheduled job it always goes to the "For All Sites".  I have noticed when then using the SiteAssetsRoot on the website code area it actually points "For This Site" and the GlobalAssetsRoot  points to "For All Sites".  Is this because under the Admin area it doesn't know which site you want?

#201699
Feb 27, 2019 16:52
Vote:
 

Not exactly, it is the context of scheduled job is always an empty SiteDefinition IIRC. That is why it fallback to the global asset root

#201700
Feb 27, 2019 17:20
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.