cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Qing
Helper I
Helper I

How to display the current user information by code?

Hello Everyone, I want to create a custom component which could display the current login user's business data by custom code in Power Apps component framework. But I don't know how to get the current user's ID or Email in the code. Here is my custom code. Could you give me some advice?

import { IInputs, IOutputs } from "./generated/ManifestTypes";

export class ExportExcelComponent implements ComponentFramework.StandardControl<IInputs, IOutputs> {
	// PCF framework context, "Input Properties" containing the parameters, control metadata and interface functions.
	private _context: ComponentFramework.Context<IInputs>;

	// PCF framework delegate which will be assigned to this object which would be called whenever any update happens. 
	private _notifyOutputChanged: () => void;

	// Control's container
	private controlContainer: HTMLDivElement;

	// button element created as part of this control
	private exportButton: HTMLButtonElement;

	// label element created as part of this control
	private errorLabelElement: HTMLLabelElement;
	/**
	 * Empty constructor.
	 */
	constructor() {

	}

	/**
	 * Used to initialize the control instance. Controls can kick off remote server calls and other initialization actions here.
	 * Data-set values are not initialized here, use updateView.
	 * @param context The entire property bag available to control via Context Object; It contains values as set up by the customizer mapped to property names defined in the manifest, as well as utility functions.
	 * @param notifyOutputChanged A callback method to alert the framework that the control has new outputs ready to be retrieved asynchronously.
	 * @param state A piece of data that persists in one session for a single user. Can be set at any point in a controls life cycle by calling 'setControlState' in the Mode interface.
	 * @param container If a control is marked control-type='standard', it will receive an empty div element within which it can render its content.
	 */
	public init(context: ComponentFramework.Context<IInputs>, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container: HTMLDivElement) {
		// Add control initialization code
		this._context = context;
		this._notifyOutputChanged = notifyOutputChanged;
		this.controlContainer = document.createElement("div");

		//Create an upload button to call the forms recognizer api
		this.exportButton = document.createElement("button");

		// Get the localized string from localized string 
		this.exportButton.innerHTML = context.resources.getString("PCF_FormsRecognizerControl_Recognize_ButtonLabel");
		this.exportButton.addEventListener("click", this.onExportButtonClick.bind(this));

		// Create an error label element
		this.errorLabelElement = document.createElement("label");
		this.errorLabelElement.setAttribute("id", "lblError");

		// Adding the label and button created to the container DIV.
		this.controlContainer.appendChild(this.exportButton);
		this.controlContainer.appendChild(this.errorLabelElement);
		container.appendChild(this.controlContainer);
	}

	/**
	* This button event handler will allow the user to pick the file from the device
	* Export the results searched by end-user to excel file
	* @param event click event
	*/
	private onExportButtonClick(event: Event): void {
		var currentUserMail= null;
			$.ajax({
				method: "POST",
				url: "https://formsrecognizerpcf.cognitiveservices.azure.com/formrecognizer/v1.0-preview/custom/train?mail="+currentUserMail,
				data: "",
				beforeSend: function(xhr) {
					xhr.setRequestHeader("Ocp-Apim-Subscription-Key", "dc2e05751ac1505b");
					xhr.setRequestHeader("Content-Type", "application/json");
				}
			});		
	}
	/**
	 * Called when any value in the property bag has changed. This includes field values, data-sets, global values such as container height and width, offline status, control metadata values such as label, visible, etc.
	 * @param context The entire property bag available to control via Context Object; It contains values as set up by the customizer mapped to names defined in the manifest, as well as utility functions
	 */
	public updateView(context: ComponentFramework.Context<IInputs>): void {
	// Add code to update control view
}
1 ACCEPTED SOLUTION

Accepted Solutions
a33ik
MVP

Hello,

User Id is available under context.userSettings. Here is how you can use it:

let userId = context.userSettings.userId;

If you need some additional information from user you can use context.webAPI.retrieveRecord to gte information you need.

View solution in original post

2 REPLIES 2
a33ik
MVP

Hello,

User Id is available under context.userSettings. Here is how you can use it:

let userId = context.userSettings.userId;

If you need some additional information from user you can use context.webAPI.retrieveRecord to gte information you need.

View solution in original post

In general for relatively static information like user profile, you can cache the information locally using setControlState API so that the next load is faster as State value is available to control iniit function. WebAPI can still run and refresh the value is changed.

Hemant

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (1,531)