Sunday 9 July 2023

To display the photo of the current user in an SPFx web part using the Microsoft Graph API

 you can follow these steps:

  1. Install necessary dependencies:

    • Install the @microsoft/sp-http package by running the following command in your project's root directory:
      bash
      npm install @microsoft/sp-http
  2. Import the required modules and interfaces in your web part's TypeScript file:

    typescript
    import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http'; import { MSGraphClient } from '@microsoft/sp-http';
  3. Retrieve the photo URL from Microsoft Graph and display it in your web part:

    typescript
    // Get the photo URL using Microsoft Graph this.context.msGraphClientFactory .getClient() .then((client: MSGraphClient): void => { client .api('/me/photo/$value') .responseType('blob') .get((error, photoResponse: any, rawResponse?: any) => { if (photoResponse) { // Convert the photo response to a blob URL const blobUrl = URL.createObjectURL(photoResponse); // Display the photo in your web part const photoElement = document.createElement('img'); photoElement.src = blobUrl; this.domElement.appendChild(photoElement); } else { // Handle error console.error(error); } }); });

Remember to replace this.context with your web part's context object, which provides access to the SharePoint HTTP client and the Microsoft Graph client.

Make sure you have the necessary permissions and consent to access the user's photo.

Module '"@microsoft/sp-property-pane"' has no exported member 'PropertyPaneDatePicker'.

  The @microsoft/sp-property-pane package does not provide a PropertyPaneDatePicker control. If you're looking to add a date picker in the property pane of an SPFx web part, you can use the PropertyPaneCustomField control and integrate a third-party date picker component.

Here's an example of how you can use the PropertyPaneCustomField to add a date picker using the office-ui-fabric-react library:

  1. Install the required packages:
bash
npm install office-ui-fabric-react
  1. Import the necessary modules in your web part's TypeScript file:
typescript
import * as React from 'react'; import * as ReactDom from 'react-dom'; import { PropertyPaneCustomField } from '@microsoft/sp-property-pane'; import { DatePicker } from 'office-ui-fabric-react/lib/DatePicker'; import { IDatePickerProps } from 'office-ui-fabric-react/lib/DatePicker';
  1. Create a custom property pane field component for the date picker:
typescript
export class DatePickerField implements IPropertyPaneField<any> { private elem: HTMLElement; private properties: any; private targetProperty: string; private onPropertyChange: (propertyPath: string, newValue: any) => void; private customProperties: any; constructor(elem: HTMLElement, properties: any, onPropertyChange: (propertyPath: string, newValue: any) => void, targetProperty: string, customProperties?: any) { this.elem = elem; this.properties = properties; this.onPropertyChange = onPropertyChange; this.targetProperty = targetProperty; this.customProperties = customProperties; } public render(): void { const element: React.ReactElement<IDatePickerProps> = React.createElement(DatePicker, { label: this.customProperties.label, value: this.properties[this.targetProperty], onSelectDate: this.onDateChanged, }); ReactDom.render(element, this.elem); } private onDateChanged = (date: Date): void => { this.properties[this.targetProperty] = date; this.onPropertyChange(this.targetProperty, this.properties[this.targetProperty]); this.render(); } // Other required methods from IPropertyPaneField }
  1. In your web part's property pane configuration, use the PropertyPaneCustomField to add the date picker:
typescript
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration { return { pages: [ { header: { description: 'Custom Fields' }, groups: [ { groupName: 'Settings', groupFields: [ PropertyPaneCustomField({ targetProperty: 'selectedDate', onRender: (elem: HTMLElement): void => { if (this.context.propertyPane.isRenderedByWebPart()) { const datePickerField: DatePickerField = new DatePickerField(elem, this.properties, this.onPropertyPaneFieldChanged, 'selectedDate', { label: 'Select a date' }); datePickerField.render(); } } }) ] } ] } ] }; }
  1. Make sure to update your web part's interface to include the selectedDate property:
typescript
export interface IYourWebPartProps { selectedDate: Date; }

With these steps, you have added a custom date picker field to your web part's property pane using the PropertyPaneCustomField control and the office-ui-fabric-react library.