Getting app settings on java

Hey guys,

Quick question: is it possible to get data I’ve set on my app’s site.xml from java?
My users will set some settings for the app, and my application’s java controllers are receiving data directly, and they can’t be processed by enonic first, and I really need those settings. Can that be done?


1 Like

Yes, you can get the values from the site.xml config from Java.
Fetch the Site using ContentService, and then you can get the SiteConfigs, with a config for each app in the site.

Site site = (Site) contentService.getByPath( ContentPath.from( "/my-site" ) );
SiteConfigs siteConfigs = site.getSiteConfigs();
ApplicationKey thisApp = ApplicationKey.from( getClass() );
SiteConfig siteConfig = siteConfigs.get( thisApp );
String configValue = siteConfig.getConfig().getString( "configProperty" );

Full example class:


Thanks for the response. Just one more question:

In order to get the site, I need to know its name, right? But I won’t have this information. Would it be possible to get all sites from the contentService?

Or a way to get the config for the current app only, without having to meddle with the Sites.

Like, this is the part of the code that interests me:
ApplicationKey thisApp = ApplicationKey.from( getClass() );
SiteConfig siteConfig = siteConfigs.get( thisApp );

But I can’t fill siteConfigs without knowing a name to use in the ContentPath.from().

Hope I could explain my problem, thanks!

You can get all existing sites with a query, and filter by content type:

ContentQuery contentQuery = ContentQuery.create().addContentTypeName( ).build();
contentService.find( contentQuery );

You cannot get the site.xml config without a site, because the site.xml is just a schema without values, until it's applied to an existing site.

That should work, thanks a bunch!

If your code is executed in the context of a site. You should be able to retrieve it through the context?

1 Like

That sounds more straightforward, do you have an example of how could I achieve such a thing?

First thing is that you must be running in a site context, so how is this java code executed?

Do you think I would be able to do something like this?

ApplicationKey thisApp = ApplicationKey.from(getClass());

I saw that I was using the PortalRequestAccessor to get the raw requests before, so I’m pretty sure everything is inside a site context.

1 Like

It depends on the context in which the java-code is invoked. How and when is your java code invoked?

1 Like