SharePoint Framework - React-based Carousel

Overview

SharePoint portals have many widgets or (technically) web parts displaying the content to the users. They are either out-of-the-box web parts configured to show the content or custom developed web parts. Carousel is one such commonly used web part. The Carousel Web Part often scrolls the images in an infinite loop and allows the users to scroll through. Often, Carousels are used to rotate the news or announcements on a home page of SharePoint portal. The modern SharePoint does not offer any carousel web part out of the box; however, we can use available npm packages for implementing the carousel functionality.
 
In this article, we will explore npm packages to help represent carousel in SPFx webpart. We will use ReactJS in this example.
 
SharePoint Framework Version and NPM Packages Installed
 
For this article, I am using SharePoint Framework Version 1.7.1.
  1. npm view @microsoft/generator-sharepoint  
SharePoint Framework - React based Carousel
 
To see the installed npm packages, use the below command.
  1. npm list-g --depth 0  
Create SPFx Solution
 
Open the command prompt. Create a directory for SPFx solution.
  1. md spfx-react-carousel  
Navigate to the above-created directory.
  1. cd spfx-react-carousel  
Run Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
Yeoman Generator will present you with the wizard by asking questions about the solution to be created.
 
SharePoint Framework - React based Carousel
 
Solution Name: Hit Enter to have a default name (spfx-react-carousel in this case) or type-in any other name for your solution.
Selected choice: Hit Enter
 
Target for component: Here, we can select the target environment where we are planning to deploy the client webpart, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
 
Place of files: We may choose to use the current folder or create a subfolder for our solution.
Selected choice: Use the current folder
 
Deployment option: We may choose to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without running any feature deployment or adding apps in sites.
Selected choice: N (install on each site explicitly)
 
Type of client-side component to create: We can choose to create client-side webpart or an extension.
Selected choice: Web Part
 
Web part name: Hit Enter to select the default name or type in any other name.
Selected choice: ReactCarousel
 
Web part description: Hit Enter to select the default description or type in any other value.
Selected choice: React-based Carousel
 
Framework to use: Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice: React
 
Yeoman generator will perform the scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock-down the version of project dependencies by running the below command.
  1. npm shrinkwrap  
In the command prompt, type the below command to open the solution in a code editor of your choice.
  1. code .  

npm Packages

We will use the npm package called as react-responsive-carousel (https://www.npmjs.com/package/react-responsive-carousel). Use the below command to install the carousel.
  1. npm install react-responsive-carousel --save  
The --save option enables NPM to include the packages to dependencies section of the package.json file.
 
Code the webpart
  1. Open the ReactCarousel.tsx file under “\src\webparts\reactCarousel\components\” folder.
  2. Import the Carousel control.
    1. import { Carousel } from 'react-responsive-carousel';    
  3. Include the styles for Carousel.
    1. import "react-responsive-carousel/lib/styles/carousel.min.css";    
Define State
  1. Create a new file IReactCarouselState.ts under “\src\webparts\reactCarousel\components\” folder.
    1. export interface IReactCarouselState {    
    2.     imageURLs: string[];    
    3. }  
  2. Update our component “\src\webparts\reactCarousel\components\ ReactCarousel.tsx” to use the state.
    1. export default class ReactCarousel extends React.Component<IReactCarouselProps, IReactCarouselState> {    
    2.   public constructor(props: IReactCarouselProps, state: IReactCarouselState) {    
    3.     super(props);    
    4.     
    5.     this.state = {    
    6.       imageURLs: []    
    7.     };    
    8.   }    
    9. }    
Implement Service
 
Let us implement the service to fetch the image URLs to display in the carousel.
  1. Create a “services” folder under the “src” folder.
  2. Add a file IDataService.ts under “services” folder.
    1. export interface IDataService {    
    2.     getImages: (listName?: string) => Promise<any>;    
    3. }    
  3. Add a file ImageService.ts under “services” folder.
    1. import { ServiceScope, ServiceKey } from "@microsoft/sp-core-library";    
    2. import { IDataService } from './IDataService';    
    3. import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';    
    4. import { PageContext } from '@microsoft/sp-page-context';    
    5. import { ICarouselImage } from './ICarouselImage';    
    6.     
    7. export class ImageService implements IDataService {    
    8.     public static readonly serviceKey: ServiceKey<IDataService> = ServiceKey.create<IDataService>('carousel:data-service', ImageService);    
    9.     private _spHttpClient: SPHttpClient;    
    10.     private _pageContext: PageContext;    
    11.     private _currentWebUrl: string;    
    12.     
    13.     constructor(serviceScope: ServiceScope) {    
    14.         serviceScope.whenFinished(() => {    
    15.             // Configure the required dependencies      
    16.             this._spHttpClient = serviceScope.consume(SPHttpClient.serviceKey);    
    17.             this._pageContext = serviceScope.consume(PageContext.serviceKey);    
    18.             this._currentWebUrl = this._pageContext.web.absoluteUrl;    
    19.         });    
    20.     }    
    21.     
    22.     public getImages(listName?: string): Promise<string[]> {    
    23.         var images: string[] = [];    
    24.         return new Promise<string[]>((resolve: (itemId: string[]) => void, reject: (error: any) => void): void => {    
    25.             this.readImages(listName)    
    26.                 .then((carouselItems: ICarouselImage[]): void => {    
    27.                     var i: number = 0;    
    28.                     for (i = 0; i < carouselItems.length; i++) {    
    29.                         images.push(this._currentWebUrl + carouselItems[i].FileRef);    
    30.                     }    
    31.                     resolve(images);    
    32.                 });    
    33.         });    
    34.     }    
    35.     
    36.     private readImages(listName: string): Promise<ICarouselImage[]> {    
    37.         return new Promise<ICarouselImage[]>((resolve: (itemId: ICarouselImage[]) => void, reject: (error: any) => void): void => {    
    38.             this._spHttpClient.get(`${this._currentWebUrl}/_api/web/lists/getbytitle('${listName}')/items?$select=FileRef/FileRef&$filter=FSObjType eq 0`,    
    39.                 SPHttpClient.configurations.v1,    
    40.                 {    
    41.                     headers: {    
    42.                         'Accept''application/json;odata=nometadata',    
    43.                         'odata-version'''    
    44.                     }    
    45.                 })    
    46.                 .then((response: SPHttpClientResponse): Promise<{ value: ICarouselImage[] }> => {    
    47.                     return response.json();    
    48.                 })    
    49.                 .then((response: { value: ICarouselImage[] }): void => {    
    50.                     resolve(response.value);    
    51.                 }, (error: any): void => {    
    52.                     reject(error);    
    53.                 });    
    54.         });    
    55.     }    
    56. }   
Update Web Part class to consume Service
  1. Open the ReactCarousel.tsx file under “\src\webparts\reactCarousel\components\” folder.
  2. Update the class to consume the implemented service.
    1. import * as React from 'react';    
    2. import styles from './ReactCarousel.module.scss';    
    3. import { IReactCarouselProps } from './IReactCarouselProps';    
    4. import { escape } from '@microsoft/sp-lodash-subset';    
    5. import { Carousel } from 'react-responsive-carousel';    
    6. import "react-responsive-carousel/lib/styles/carousel.min.css";    
    7. import { IReactCarouselState } from './IReactCarouselState';    
    8.     
    9. import { ServiceScope } from '@microsoft/sp-core-library';    
    10. import { ImageService } from '../../../services/ImageService';    
    11. import { IDataService } from '../../../services/IDataService';    
    12.     
    13. export default class ReactCarousel extends React.Component<IReactCarouselProps, IReactCarouselState> {    
    14.   private dataCenterServiceInstance: IDataService;    
    15.     
    16.   public constructor(props: IReactCarouselProps, state: IReactCarouselState) {    
    17.     super(props);    
    18.     
    19.     this.state = {    
    20.       imageURLs: []    
    21.     };    
    22.     
    23.     let serviceScope: ServiceScope = this.props.serviceScope;    
    24.     this.dataCenterServiceInstance = serviceScope.consume(ImageService.serviceKey);    
    25.     
    26.     this.dataCenterServiceInstance.getImages('Site Collection Images').then((carouselItems: any) => {    
    27.       this.setState({    
    28.         imageURLs: carouselItems    
    29.       });    
    30.     });    
    31.   }    
    32.     
    33.   public render(): React.ReactElement<IReactCarouselProps> {    
    34.     return (    
    35.       <div className={styles.reactCarousel}>    
    36.         <div className={styles.container}>    
    37.           <div className={styles.row}>    
    38.             <div className={styles.column}>    
    39.               <span className={styles.title}>Welcome to SharePoint!</span>    
    40.               <p className={styles.subTitle}>React based Carousel</p>    
    41.               <p className={styles.description}>{escape(this.props.description)}</p>    
    42.     
    43.               <Carousel showThumbs={false} >    
    44.                 {this.state.imageURLs.map((imageList) => {    
    45.                   return (<div>    
    46.                     <img src={imageList} />    
    47.                   </div>)    
    48.                 })}    
    49.               </Carousel>    
    50.     
    51.             </div>    
    52.           </div>    
    53.         </div>    
    54.       </div>    
    55.     );    
    56.   }    
    57. }    
Test the WebPart
  1. On the command prompt, type “gulp serve”.
  2. Open SharePoint site.
  3. Navigate to /_layouts/15/workbench.aspx.
  4. Locate and add the webpart to the page.

    SharePoint Framework - React based Carousel
  5. Verify if the Carousel is scrolling through images.

    SharePoint Framework - React based Carousel 
Troubleshooting
 
In some cases, SharePoint workbench (https://[tenant].sharepoint.com/_layouts/15/workbench.aspx) shows the below error although “gulp serve” is running.
 
SharePoint Framework - React based Carousel
 
Open the following URL in the next tab of the browser. Accept the warning message.
 
https://localhost:4321/temp/manifests.js
 
Summary
 
Carousel is a widely used functionality in SharePoint world. Modern SharePoint does not provide Carousel as a ready web part to use, however, we can utilize any third-party controls to meet the business needs.