Power Appsで実行している環境を知りたい場合の方法はこれまでいくつか考えられてきました。
例えばデバイスがGPSの信号をとれるかどうか や、フローを実行してPowerAppsからのリクエストを見る方法などです。
GPSのほうはPCでもGPSの信号を取得できるとモバイルアプリとの区別ができないのでちょっと難あり。フローを実行するほうは起動時に処理を入れたい場合に組み込みにくいという課題があります。
一方で、Javascript使えば簡単にUserAgent stringとれるので、Power Appsのコードを書く拡張である Power Apps Component Frameworkで作ってみました。
以下がPCのブラウザーでのPCFの出力結果とiOSアプリでの結果です。
たしかにUserAgentとれていますね。モバイルデバイスの場合には"PowerApps"という文字が入るので、これで判定すればよいかと思います。
作り方
いつも参考にさせていただいているPCFの作り方HowToはこちらです。
まっさらな状態から変更するのはindex.ts
とControlManifest.Input.xml
とManifestTypes.d.ts
の3つです。
ControlManifest.Input.xml
<?xml version="1.0" encoding="utf-8" ?> <manifest> <control namespace="SampleNamespace" constructor="TSLinearInputComponent" version="1.0.0" display-name-key="TSLinearInputComponent" description-key="TSLinearInputComponent description" control-type="standard"> <!-- outProperty に出力結果を格納する--> <property name="outProperty" display-name-key="outProperty_Display_Key" description-key="outProperty_Desc_Key" of-type="SingleLine.Text" usage="output"/> <resources> <code path="index.ts" order="1"/> </control> </manifest>
ManifestTypes.d.ts
export interface IOutputs { outProperty?: string; }
index.ts
import {IInputs, IOutputs} from "./generated/ManifestTypes"; export class TSLinearInputComponent implements ComponentFramework.StandardControl<IInputs, IOutputs> { /** * Empty constructor. */ constructor() { } private _notifyOutputChanged: () => void; private labelElement: HTMLLabelElement; private _container: HTMLDivElement; public init(context: ComponentFramework.Context<IInputs>, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container:HTMLDivElement): void { this._container = document.createElement("div"); this.labelElement = document.createElement("label"); this.labelElement.innerText=navigator.userAgent; this._container.appendChild(this.labelElement); container.appendChild(this._container); //画面上にUserAgent Stringを念のため表示 this._notifyOutputChanged=notifyOutputChanged; } public updateView(context: ComponentFramework.Context<IInputs>): void { // Add code to update control view this._notifyOutputChanged(); //必要かどうか微妙。Updateしないからいらないかも } public getOutputs(): IOutputs { return {"outProperty":navigator.userAgent}; //ここで出力としてoutPropertyを返している } public destroy(): void { // Add code to cleanup control if necessary } }
まっさらな状態と比べると、追加したのは10行くらいです。これをあとはビルドして環境に追加すれば先ほどのようなUserAgentを表示するカスタムコントロールが使えるようになります。
おわり。
Refs
Solved: pcf assign output params for canvas app operations - Power Platform Community