CtxVirtualSite()
function CtxVirtualSite(): string;Returns the ID of the virtual site in whose context the event that triggered this script occurred. Unlike every other Ctx* function, this one always returns a meaningful value: it is never an empty string, for any event.
Why this function exists, and why it matters
Every worker instance serves exactly one virtual site, and it will not even start without one, so the virtual site is known before the first connection is accepted and cannot change for the life of the session. That makes CtxVirtualSite() the one context value you never need to guard against being empty: there is no event, not even OnNewConnection or BeforeSendSoftwareID, that fires outside a virtual site.
Like every Ctx* value, it is captured at the moment the event fires and frozen into the script's execution context for its entire lifetime. It is equivalent to Session.GetVirtualSite(), but it is available without going through the session object, which keeps it usable in exactly the same way as the rest of the event context.
This is most useful in multi-tenant deployments, where the same script is shared across virtual sites and must branch on, or simply record, which tenant it is running for.
Return value
| Scenario | Return value |
|---|---|
| Any event, at any point in the session | The virtual site ID, e.g. 0ujsszwN8NRY24YaXiTIE2VWDTS |
NOTE
This is the virtual site ID, not its display name, exactly like the value returned by Session.GetVirtualSite().
Example
{
Log("Event fired in virtual site: " + CtxVirtualSite());
if (CtxVirtualSite() === "0ujsszwN8NRY24YaXiTIE2VWDTS" && CtxUsername() !== "") {
Log("Tenant-specific handling for user: " + CtxUsername());
}
}WARNING
CtxVirtualSite() is not available prior to v8.0.6. Please upgrade to v8.0.6 or newer to use this function.
See also
EventCtx(): returns the whole event context (path, target path, VFS name, username, virtual site) as a single objectCtxUsername(): the username of the user whose session triggered the eventCtxVFSName(): the name of the VFS the event refers toCtxRelPath(): the path of the file that triggered the eventCtxRelTargetPath(): the target path of a rename or move operationEventHandler(): returns the name of the event that triggered this scriptSession.GetVirtualSite(): the same value, read from the session object
