How to Map Class Package to a Namespace Programmatically

Answers

Something like this, but in %SYS namespace. Documentation

%SYS>s props("Database")="SAMPLES",sc=##Class(Config.MapPackages).Create("USER","PackageA",.props)

Thanks, Dmitry!

Though the parameter for a 'namespace-to-map' with a name "Database" looks weird.

Nevermind, everything works fine!

Just to be clear: your line of code shows how to create the mapping of PackageA in Database SAMPLES to Namespace USER.

Note: if you want to add a large number of mappings, you should first save all new mappings and then activate the new config file afterwards.

This is applicable to all Config modifications.

cpf files.

All classes from Config package affect one of cpf file (active one by default). But you also need to save and activate modified config file. Dmitry solution does that all by default. Which is fine if you want to add one mapping, but if you want to modify several Config settings it's better to modify them and activate the changes separately. For example here's mapping several packages to %ALL.

#include %syConfig
set namespace = "%ALL"
set namespace = $zcvt(namespace, "U")
for package = "Package1","Package2","Package3" {
    kill p
    set p("Database")=pFrom
    set sc = ##Class(Config.MapPackages).Create(namespace, package,.p,,$$$CPFSave)
}
set sc = ##Class(Config.CPF).Write()
set sc = ##Class(Config.Map).MoveToActive(namespace)
set sc = ##Class(Config.Namespaces).Load(namespace)

Comments

This is similar documentation for mapping Routines.

And one note: if you map routines, put the routine name without extension.

E.g. if you want to map MyMacro.inc from USER to %ALL run the following in %SYS namespace:

%SYS>s props("Database")="USER",sc=##Class(Config.MapRoutines).Create("%ALL","MyMacro",.props)