Fetching site config in services

Enonic version: 6.5.1

According to the docs, the site config isn’t meant to be requested in services. Why is that? And how can I access the site config from a service?

It doesn’t specifically mention services, but it should work there. I think it has to be used inside the handleGet or handlePost method so it has the site context.

At least in my case, portal.getSiteConfig() returns null in my service. Here’s the code.

Any idea as to why I run into this problem?

I’m gueesing that you’re not getting any result because a service does not know (or cannot assume) what site context that a request originates from, or even if it’s from any Enonic XP site at all. The server-side JavaScript reference documentation says “[getSiteConfig] is meant to be called from a page, layout or part controller” without any mention of services. But I guess there are probably ways to create a workaround? I don’t know, maybe create a content with a URL that your service can access with the httpclient lib, where this content provides the info that you need?

I just checked and I definitely used getSiteConfig() in a service exports.post and it works. It uses the context of the site that is calling the service. Is it possible here that concatenating the JSON.stringify with other string in the log.info() could be messing something up?

log.info(JSON.stringify(siteConfig, null, 4));

Ah, so the context is the calling app, not the current one? How do I get the site config of the app the service resides in? What happens when the service is called from an external user (in my case it’s a frontend AJAX call)?

The siteConfig var is null, so the code crashes further down due to a null pointer.

You add your contact form app to a Superhero site then the portal.serviceUrl() for the Ajax will include “superhero” so you should get the Superhero site config. It doesn’t matter where the request comes from, it goes through the “Site”. The configs for all apps added to the site should be returned.


I think the getSiteConfig should include both Superhero config and contact form config. Will do some testing tomorrow.

1 Like

I am running into the same problem. I set up my service url using the portal.serviceUrl() method, but in my post handler, the siteConfig returned is null.
I see my app name and app namespace is included in the url, yet i cannot get the correct site config in the handler.

Did any of you find a solution to this?

I was using “fetch” to post data from the frontend, and since the content I was testing on was not yet published I had to supply the correct cookies on my ajax request. This was solved using:
credentials: ‘same-origin’
In my request configuration object passed to fetch.

const request = {
method: ‘post’,
credentials: ‘same-origin’,
body: JSON.stringify(data)

return fetch(endpoint, request)

top tip - use a package on top of the raw Fetch-API to deal with headers, json-requests etc.

I put one together some time ago, which I am pretty happy with:

1 Like

A bit confused on what happened here. Are you saying you were testing the service on the master branch, using a path that was not yet published? Or were you using the preview mode?

Just to be clear on this one. The app context of server-side code i.e. a service will always be app the controller/script exists in. However the site context may change based on which site the app is actually used.