Commonly used sample codes for Power Apps Code Component Framework (PCF)
Here is a commonly used code snippets used in PCF Controls.
PAC Commands commonly used
PCF Commands
==========================================================
pac pcf init --namespace "$NAMESPACE" --name "$CONTROL_NAME" --template "$TEMPLATE"
npm install
npm run build
pac solution init --publisher-name "$PUBLISHER_NAME" --publisher-prefix "$PUBLISHER_PREFIX"
pac solution add-reference --path "$PCF_PROJECT_PATH"
pac solution build or msbuild /t:build /restore in dev command promot
pac auth create --url "$ENVIRONMENT_URL"
pac auth list
pac auth select --index <index of the active profile>
pac pcf push --publisher-prefix <your publisher prefix>
Extra
--------------------
pac pcf init -n <control name> -ns <namespace> -t field -npm
pac pcf init -n <control name> -ns <namespace> -t field -fw react -npm
pac pcf init -n <control name> -ns <namespace> -t dataset -npm
pac pcf init -n <control name> -ns <namespace> -t dataset -fw react -npm
npm run build
npm start watch
pac solution init --publisher-name developer --publisher-prefix dev
pac solution add-reference --path <path to base control folder>
msbuild /t:build /restore
msbuild /p:configuration=release
pac auth create -u <URL to Dynamics or Power Apps organization>
pac auth list
pac auth select -i <index>
pac pcf push --publisher-prefix <publisher prefix>
Initializing a PCF Control
export class MyPCFControl implements ComponentFramework.StandardControl<IInputs, IOutputs> {
private container: HTMLDivElement;
// Called when the control is initialized
public init(
context: ComponentFramework.Context<IInputs>,
notifyOutputChanged: () => void,
state: ComponentFramework.Dictionary,
container: HTMLDivElement
): void {
this.container = container;
this.renderControl(context);
}
}
Rendering HTML Control
private renderControl(context: ComponentFramework.Context<IInputs>): void {
const label = document.createElement("label");
label.textContent = "Enter your value:";
const input = document.createElement("input");
input.type = "text";
input.value = context.parameters.myInputProperty.raw || "";
this.container.appendChild(label);
this.container.appendChild(input);
}
Reading Input Values
input.addEventListener("input", (event: Event) => {
const value = (event.target as HTMLInputElement).value;
notifyOutputChanged();
});
Handling Daverse Parameters
public updateView(context: ComponentFramework.Context<IInputs>): void {
const currentValue = context.parameters.myInputProperty.raw || "";
// Update the control based on Dataverse parameter values
}
Notify Dataverse about Data Changes
private notifyOutputChanged: () => void;
// Notify when data changes
public getOutputs(): IOutputs {
return {
myInputProperty: this.currentValue
};
}
Handing Resize Event
public resizeCanvas(width: number, height: number): void {
// Adjust the control's size dynamically
this.container.style.width = `${width}px`;
this.container.style.height = `${height}px`;
}
Applying CSS Dynamically
const style = document.createElement("style");
style.textContent = `
input {
width: 100%;
padding: 8px;
margin-top: 5px;
}
`;
this.container.appendChild(style);
Working with Dataverse Web API
const webApi = context.webAPI;
// Retrieve records
webApi.retrieveMultipleRecords("account", "?$select=name,accountnumber").then(
(response) => {
console.log(response.entities);
}
);
Cleaning Up Resources
public destroy(): void {
while (this.container.firstChild) {
this.container.removeChild(this.container.firstChild);
}
}
Hope it helps.