cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
srasmussen
Frequent Visitor

Ability to add public methods or events to a PCF control

I am trying to build a PCF control that replaces the OOB Multi-select optionset control.  One of the things that I can't figure out is how to add public methods to my control such that a form script is able to interact with my PCF control.  For example, I need to be able to add the following methods (that the OOB control has):

addOption(1234)
removeOption(1234)
clearOptions()
getOptions()

It seems like it should be possible based on the response to a similar question here: https://powerusers.microsoft.com/t5/Power-Apps-Pro-Dev-ISV/Form-designer-setting-additional-methods-... 

 

However, I've created similar methods on my class that implements the `ComponentFramework.StandardControl` but it doesn't appear to work.  Is this possible?  If so, where should I be placing my methods?

9 REPLIES 9
skoofy5
Advocate IV
Advocate IV

I just a did test with a dropdown control we have on top of an custom option set control and the 'removeOption' method seems to work. Is it triggering the updateView of your control and is your control being re-rendered?

https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/...

srasmussen
Frequent Visitor

@skoofy5 - Thanks for the reply!  When I try the following in a form "onLoad" script with my custom PCF I get an error: "TypeError: control.removeOption is not a function"

let control = this._formContext.getControl("mrs_legs"); // My custom PCF control
control.removeOption(123); // Throws the exception: TypeError: control.removeOption is not a function

 

If I change the control back to the default then I don't receive that error.

 

My control is very basic at this point but here it is:

import * as React from "react";
import * as ReactDOM from 'react-dom';
import AdvancedMultiSelectControl, { IAdvancedMultiSelectProps } from "./AdvancedMultiSelectControl";
import { IInputs, IOutputs } from "./generated/ManifestTypes";

interface IAdvancedMultiSelectState extends ComponentFramework.Dictionary {

}

export class AdvancedMultiSelect implements ComponentFramework.StandardControl<IInputs, IOutputs> {
    _container: HTMLDivElement;
    _initialVisible: boolean;
    _initialDisabled: boolean;
    _optionSetMetadata: ComponentFramework.PropertyHelper.FieldPropertyMetadata.OptionSetMetadata;

    constructor() {

    }

    public init(
        context: ComponentFramework.Context<IInputs>,
        notifyOutputChanged: () => void,
        state: IAdvancedMultiSelectState,
        container: HTMLDivElement): void {

        this._container = container;
        this._initialVisible = context.mode.isVisible;
        this._initialDisabled = context.mode.isControlDisabled;
        this._optionSetMetadata = context.parameters.controlProps.attributes!;
    }

    public updateView(context: ComponentFramework.Context<IInputs>): void {
        ReactDOM.render(React.createElement(AdvancedMultiSelectControl,
            {
                values: [],
            } as IAdvancedMultiSelectProps),
            this._container);
    }


    public getOutputs(): IOutputs {
        return {};
    }

    public destroy = (): void => {}

    public clearOptions = () => {
        alert("Clear options");
    }

    public getOptions = () => {
        alert("Get options called");
    }

    public removeOption = (option: number) => {
        alert("Remove option");
    }

    public addOption = (option: number) => {
        alert("addOption");
    }
}

 

Are you able to share the code from your control?  I must just be not understanding something correctly.  Thanks!

DianaBirkelbach
Super User
Super User

Just to get a little more precise @srasmussen .

I don't think that the PCFs have the possibility to define own methods.

On the roadmap is only the definition of custom events, similar to Canvas Components. That was announced for Wave 2, 2021 and hopefuly will be soon available: https://docs.microsoft.com/en-us/power-platform-release-plan/2021wave2/power-apps/code-component-imp....

 

The same as @skoofy5 checked, I could verify too that my ColorfulOptionset PCF does have the standard methods (like .getOptions()), exactly like the other standard methods like .setDisabled() and so on. But these are not methods defined inside the PCF, but methods defined for the base type of your PCF. 

These standard methods can be called from form scripting, and the result are the disabled state, or the options provided through the parameters,
For instance .removeOption(...) will call the updateView of your PCF , where context.parameters.optionsInput.attributes.Options doesn't have that option anymore.

Hope this helps!

 

Kind regards,
Diana
----------
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Interestingly from that same article, @DianaBirkelbach 


"For model-driven apps, code components can be created for the simple lookup and Multi-select option set (Choices) type, and properties of these new types can also be added. These are top community requests. Additional framework APIs on the canvas apps will be added, which approaches model-driven app parity."

 

Does that mean support for multiselect isn't ready yet?

DianaBirkelbach
Super User
Super User

Hi @skoofy5 
I think it depends how you read the statement. I understand it just as "we can define these properties and use them".
It is confusing, indeed. Maybe I'm wrong.

In my opinion the lookup and multioptionsets are avaibale only for model-driven apps for now.

Even in model-driven apps, considering lookups there are still a lot of properties we need to get from the platform (like in this blog where all these properties for lookup are unsupported: https://dianabirkelbach.wordpress.com/2021/06/19/lookup-pcf-lets-dive-deeper/). Right now we have only the way to get or chnage the value for the lookup; I suppose there is still a lot to come. So no, it's not ready.

 

But I don't understand the statement above talking about custom methods for PCFs. As I said, could be just my opinion.

 

 

 

Kind regards,
Diana
----------
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

@DianaBirkelbach - That makes sense about the custom functions. Unfortunately, I'm still not able to take advantage of the OOB methods for a MuliSelectOptionSet (ie "addOption" etc).  Perhaps my manifest isn't setup properly or something?  I'm fairly new to PCF controls so that very well could be the issue. I've pushed my project to a Github repo here: https://github.com/StevenRasmussen/PCF.AdvancedMultiChoice 

 

If you have any further thoughts or can point to a repo with a working example it would be much appreciated.

DianaBirkelbach
Super User
Super User

The manifest is looking good, @srasmussen .

If the manifest would be the problem, you wouldn't be able to load the component on the form.

Maybe is really a bug with the MultiOptionSet ? I don't have one to try it out right now.
But you could try to register the sdk example for MultiOptionSet PCF: https://docs.microsoft.com/en-us/powerapps/developer/component-framework/sample-controls/multi-selec... and check there if the methods are available.

Kind regards,
Diana
----------
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

@DianaBirkelbach - Thanks!  For some reason I missed that documentation.  I'll give it a try and post results here.

@DianaBirkelbach - Just deployed and tested the example PCF control and it unfortunately has the same problem 😞  So it appears that this is a genuine bug with the MultiSelectOptionSet control.  Do you have any ideas on how to report this to MS?  Do they monitor these forums at all?

Helpful resources

Announcements
Power Platform Call June 2022 768x460.png

Power Platform Community Call

Join us for the next call on August 17, 2022 at 8am PDT.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Users online (1,877)