Changing Auto Publish Media on Upload Programmatically in Episerver

NTS: Ever needed to know if the AutoPublishMedia is set? Here is how and why you should.

icon of user profile

Published 11th June 2019
Episerver 11

Last day I was looking for an API for checking “Automatically publish media on upload”. In the UI it is located in Admin/Configuration/System Settings/Editing …

However I couldn’t find it, not even when i googled it…

Where is it??

Well it is saved in tblSiteConfig:

I had to do some reflection in dotPeek searching for “AutoPublishMediaOnUpload” to find the API in EPiServer.Cms.Shell.SiteConfigurationRepositoryExtensions

API for AutoPublishMediaOnUpload

By using EPiServer.Cms.Shell.SiteConfigurationRepositoryExtensions you get some extention methods to ISiteConfigurationRepository

Example use:







_siteConfigurationRepository.GetValue((string)null, "AutoPublishMediaOnUpload");

_siteConfigurationRepository.SetValue((string)null, "AutoPublishMediaOnUpload", "true");

Why do I need to check AutoPublishMediaOnUpload?

When you’re doing some saving programmatically on media and you don’t know how the editors wants it, normally (probably) it is published on upload, but what if the admins changes this behavior? this is how you future proof it:

_contentRepository.Save(fotowareMedia as MediaData, GetSaveAction(), AccessLevel.NoAccess);

private SaveAction GetSaveAction()
   return _siteConfigurationRepository.GetAutoPublishMediaOnUpload() ? SaveAction.Publish : SaveAction.Save;

EXTRA: Saving a custom property on site level


_siteConfigurationRepository.SetValue((string)null, "MyCustomFlag", "Something");

Beware! SiteConfigurationRepository is unsupported INTERNAL API! Not covered by semantic versioning; might change without notice.

Results in

Data being saved in tblSiteConfig

Retrieve value

_siteConfigurationRepository.GetValue((string)null, "MyCustomFlag");

Further reading:

About the author

Luc Gosso
– Independent Senior Web Developer
working with Azure and Episerver

Twitter: @LucGosso