Skip to content

Instantly share code, notes, and snippets.

@MarcAlx
Created October 17, 2025 12:55
Show Gist options
  • Select an option

  • Save MarcAlx/ad478a4beaccb531c18d77266a67ffb9 to your computer and use it in GitHub Desktop.

Select an option

Save MarcAlx/ad478a4beaccb531c18d77266a67ffb9 to your computer and use it in GitHub Desktop.
ArcGIS Experience Builder Developer Edition : Instantiate/Create widget from custom widget code

Notes

  1. Creating a widget consist in adding a json config inside app (current experience) config.
  2. Widget config must exist in a layout, thus layout must exist inside a the layout of the wished page.
  3. This can't be done from settings.tsx, must be done from your widget code. settings.tsx code doesn't see the same app config as widget code.
import { AppWidgetManager, widgetService } from "jimu-for-builder";
import { getAppStore, appActions, uuidv1, Immutable } from "jimu-core";
const awManager = AppWidgetManager.getInstance();
const widgetId = uuidv1();
//widget conf
const wConf = {
uri: 'widgets/common/text/',
id: widgetId,
label: 'Mon texte programmatique',
version: '1.10.0',
manifest: null,
config: {
text: '<p>Sample</p>'
}
};
//ask AppWidgetManger to hydrate proper widget json
awManager.handleNewWidgetJson(wConf).then((hwConf:any)=>{
const state = getAppStore().getState();
const appConfig = state.appConfig;
//create widget in appconfig
const arr = widgetService.createWidget(appConfig, hwConf,[], true);//return an array with [New AppConfig, New widget id in New AppConfig]
const updatedAppConfig = arr[0];
const layoutItemId = "23000";//must be integer string
const layoutItem = {
widgetId: arr[1],
bbox: { left: 100, top: 100, width: 200, height: 50 },
id: layoutItemId,
type: "WIDGET"
};
//locate page and layout where layout will be added
const firstPageId = updatedAppConfig.pages?.[Object.keys(updatedAppConfig.pages)[0]].id;
const firstPage = updatedAppConfig.pages[firstPageId];
const layoutId = firstPage.layout?.['L0'] || Object.values(firstPage.layout)[0];
//add layout to appconfig
let newAppConfig = updatedAppConfig.setIn(['layouts', layoutId, 'content', layoutItemId], Immutable(layoutItem))
//indicate where layout should display
.setIn(['layouts', layoutId, 'order', 2 ], layoutItemId);
//publish new appconfig
getAppStore().dispatch(appActions.appConfigChanged(newAppConfig))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment