- Creating a widget consist in adding a json config inside app (current experience) config.
- Widget config must exist in a layout, thus layout must exist inside a the layout of the wished page.
- This can't be done from
settings.tsx, must be done from your widget code.settings.tsxcode doesn't see the same app config as widget code.
Created
October 17, 2025 12:55
-
-
Save MarcAlx/ad478a4beaccb531c18d77266a67ffb9 to your computer and use it in GitHub Desktop.
ArcGIS Experience Builder Developer Edition : Instantiate/Create widget from custom widget code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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