A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.
JavaScript claude-code-pluginclaude-code-skillscode-appsgithub-copilot-pluginpower-appspower-pagespower-platform
#!/usr/bin/env node
// Adds a connector-bound GenPage and its connection references to a solution so it
// travels cross-environment. Verified live 2026-07-10 on a Dataverse test environment:
// - The appmodule (type 80, AddRequiredComponents=true) pulls the sitemap (62) and
// appmodulecomponent (10097) but does NOT pull the GenPage — so the GenPage's
// uxagentproject row MUST be added explicitly; adding it pulls its
// uxagentprojectfile children (incl. config.json with connectorBindings).
// - connectionreference is its own component and is added explicitly so
// the bindings resolve in the target env (at import the deployer supplies each
// ConnectionId through scripts/solution-settings.js: `template` writes the settings file and
// `import --settings` applies it; `pac solution create-settings` and `pac solution import
// --settings-file` are only its fallback).
// Both are custom tables whose ObjectTypeCode varies by environment, so their
// component types are discovered from EntityDefinitions before any mutation
// (lib/page-solution.js holds the read, and how a page is added, for every path that adds one).
//
// The app id is optional: with none, only the pages (and connection references) are added. That is how a page made into
// an app that is not the run's own — or one whose add failed after it was made — joins a solution: a page is a component of
// its own, and an app is not one of its required components (the app's navigation names the page, not the reverse), so adding
// the app too would put an app the maker did not ask for into the solution.
//
// Usage:
// node add-page-to-solution.js <envUrl> <solutionUniqueName> [<appId>]
// [--page-ids <uxagentprojectId1,uxagentprojectId2>]
// [--connection-refs <logicalName1,logicalName2>]
//
// Output: { "ok": true, "added": [...] }
const {
dataverseRequest,
ensureOk,
parseArgs,
validateFlags,
emitResult,
} = require('./lib/dataverse-auth');
const { odataLit } = require('./lib/odata');
const { FORM_GUID_RE } = require('./lib/app-spec');
const { makeWritesBlocking } = require('./lib/output-redaction.js');
const { UXAGENTPROJECT_LOGICAL_NAME, PAGE_ADD_REQUIRED_COMPONENTS, componentTypePath, componentTypeOf } = require('./lib/page-solution.js');
const APPMODULE_COMPONENT_TYPE = 80;
const CONNECTION_REFERENCE_LOGICAL_NAME = 'connectionreference';
async function addComponent(envUrl, solutionUniqueName, componentId, componentType, addRequired) {
const body = {
ComponentId: componentId,
ComponentType: componentType,
SolutionUniqueName: solutionUniqueName,
AddRequiredComponents: addRequired,
};
const res = await dataverseRequest(envUrl, 'POST', 'AddSolutionComponent', body);
ensureOk(res, `Add component ${componentId} (type ${componentType}) to ${solutionUniqueName}`);
}
async function resolveEntityComponentType(envUrl, logicalName) {
const res = await dataverseRequest(envUrl, 'GET', componentTypePath(logicalName));
ensureOk(res, `Resolve component type for ${logicalName}`);
return componentTypeOf(res.data, logicalName);
}
async function resolveConnectionReferences(envUrl, refs) {
const resolved = [];
for (const logicalName of refs) {
const query =
`connectionreferences?$filter=connectionreferencelogicalname eq '${odataLit(logicalName)}'` +
'&$select=connectionreferenceid&$top=1';
const lookup = await dataverseRequest(envUrl, 'GET', query);
ensureOk(lookup, `Lookup connection reference ${logicalName}`);
const id = lookup.data?.value?.[0]?.connectionreferenceid;
if (!id) throw new Error(`Connection reference '${logicalName}' not found in env`);
// The id becomes the ComponentId of a write, so it is held to the same GUID rule as the ids on the
// command line — and, like them, before anything is written: a bad reply refused here leaves the
// solution untouched, where one refused at its own AddSolutionComponent would follow the app and pages.
// A damaged or unexpected reply can carry a number, an object or a braced/padded string, so the type
// is checked too, not only the pattern.
if (typeof id !== 'string' || !FORM_GUID_RE.test(id)) {
throw new Error(`Connection reference '${logicalName}' has a connectionreferenceid that is not a GUID (${JSON.stringify(id).slice(0, 80)}).`);
}
resolved.push({ logicalName, id });
}
return resolved;
}
async function main() {
const argv = process.argv.slice(2);
const { positional, flags } = parseArgs(argv);
const USAGE = 'Usage: node add-page-to-solution.js <envUrl> <solutionUniqueName> [<appId>] [--page-ids <id1,id2>] [--connection-refs <logicalName1,logicalName2>]';
// Both flags are comma-joined id lists. A bare one would become boolean `true`, and `true.split`
// is a TypeError mid-way through packaging rather than a usage error before it.
const flagError = validateFlags(argv, {
known: ['page-ids', 'connection-refs'],
needValue: ['page-ids', 'connection-refs'],
});
if (flagError) {
makeWritesBlocking(process.stdout, process.stderr);
process.stderr.write(`✗ ${flagError}\n${USAGE}\n`);
process.exit(1);
}
// The app is optional, but something has to be named to add: an app, pages, or connection references.
if (positional.length < 2 || (positional.length < 3 && !flags['page-ids'] && !flags['connection-refs'])) {
process.stderr.write(USAGE + '\n');
process.exit(1);
}
const [envUrl, solutionUniqueName, appId] = positional;
const added = [];
// Parsed BEFORE the first mutation so lookup failures refuse the whole run rather
// than leaving a half-packaged solution (app + pages added, refs missing). A repeated
// entry (in any letter case) is dropped: adding the same component twice is a wasted write.
const unique = (list) => {
const seen = new Set();
return list.filter((v) => {
const key = v.toLowerCase();
if (seen.has(key)) return false;
seen.add(key);
return true;
});
};
const refs = unique((flags['connection-refs'] || '')
.split(',')
.map((s) => s.trim())
.filter(Boolean));
const pageIds = unique((flags['page-ids'] || '')
.split(',')
.map((s) => s.trim())
.filter(Boolean));
// Every id must be a GUID before anything is read or written. A malformed page id used to reach
// Dataverse only at its own AddSolutionComponent — after the app had been added — so the run failed
// with the solution holding the app but not its page.
const malformed = [
...(appId === undefined || FORM_GUID_RE.test(appId) ? [] : [`app id '${appId}'`]),
...pageIds.filter((id) => !FORM_GUID_RE.test(id)).map((id) => `page id '${id}'`),
];
if (malformed.length) {
emitResult(false, new Error(`Not a GUID: ${malformed.join(', ')}. Nothing was added to ${solutionUniqueName}.`));
return;
}
// A list that holds only separators (`--page-ids ,`) names nothing, and with no app that is a run with nothing to do: said, not reported as done.
if (appId === undefined && !pageIds.length && !refs.length) {
emitResult(false, new Error(`Nothing to add: name an app, --page-ids or --connection-refs. Nothing was added to ${solutionUniqueName}.`));
return;
}
try {
// ObjectTypeCode for custom tables is allocated per environment. Resolve both
// types before the first AddSolutionComponent so a metadata failure leaves the
// target solution untouched instead of half-packaged.
const pageComponentType = pageIds.length
? await resolveEntityComponentType(envUrl, UXAGENTPROJECT_LOGICAL_NAME)
: null;
const connectionReferenceComponentType = refs.length
? await resolveEntityComponentType(envUrl, CONNECTION_REFERENCE_LOGICAL_NAME)
: null;
const resolvedConnectionRefs = refs.length
? await resolveConnectionReferences(envUrl, refs)
: [];
// The appmodule (type 80) with AddRequiredComponents=true pulls the sitemap and
// appmodulecomponent, but NOT the GenPage — the page is added explicitly below. When an app is
// named it goes first, so a run that stops part-way never leaves pages in the solution without
// the app whose navigation opens them.
if (appId !== undefined) {
await addComponent(envUrl, solutionUniqueName, appId, APPMODULE_COMPONENT_TYPE, true);
added.push({ type: 'appmodule', id: appId });
}
// Add each GenPage (uxagentproject) explicitly. AddRequiredComponents
// pulls its uxagentprojectfile rows — including config.json with the
// connectorBindings that must travel with the page.
for (const pageId of pageIds) {
await addComponent(envUrl, solutionUniqueName, pageId, pageComponentType, PAGE_ADD_REQUIRED_COMPONENTS);
added.push({ type: 'uxagentproject', id: pageId });
}
for (const { logicalName, id } of resolvedConnectionRefs) {
await addComponent(envUrl, solutionUniqueName, id, connectionReferenceComponentType, false);
added.push({ type: 'connectionreference', logicalName, id });
}
emitResult(true, { ok: true, added });
} catch (e) {
emitResult(false, e);
}
}
// Only run when invoked directly as a CLI; requiring the module (e.g. from tests)
// must not execute main().
if (require.main === module) {
main();
}
// Exported for unit tests. AppModule is a stable system component type; the two custom-table
// component types are deliberately resolved per environment by resolveEntityComponentType.
module.exports = {
APPMODULE_COMPONENT_TYPE,
UXAGENTPROJECT_LOGICAL_NAME,
CONNECTION_REFERENCE_LOGICAL_NAME,
resolveEntityComponentType,
resolveConnectionReferences,
};