Introduction
Bootstrap carousel is a stable carousel among the 3rd party carousels. In this article, I am using the PnpV2 library for consuming Sharepoint operations.
Open a command prompt and create a directory for the SPFx solution.
md spfx-BootstrapCarousel
Navigate to the above created directory.
cd spfx-BootstrapCarousel
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter for the default name (spfx-BootstrapCarousel in this case) or type in any other name for your solution.
Selected choice - Hit Enter
Target for the component
Here, we can select the target environment where we are planning to deploy the client web part; 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 same folder or create a subfolder for our solution.
Selected choice - same folder.
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
Permissions to access web APIs
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice - BootstrapCarousel
Web part description
Hit Enter to select the default description or type in any other value.
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
The Yeoman generator will perform a 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:
npm shrinkwrap
In the command prompt, type the below command to open the solution in the code editor of your choice.
NPM Packages used:
- npm install react-bootstrap bootstrap
- npm install @pnp/[email protected] --save //for sharepoint operations
- npm install @pnp/polyfill-ie11 --save //for iell support
Important setup
- protected onInit(): Promise<void> {
- return super.onInit().then(_ => {
- // other init code may be present
- sp.setup({
- // set ie 11 mode
- ie11: true,
- // only needed when working within SharePoint Framework
- spfxContext: this.context
- });
- });
- }
Necessary imports
- import "@pnp/polyfill-ie11";
- import * as React from 'react';
- import "bootstrap/dist/css/bootstrap.min.css";
- import Carousel from 'react-bootstrap/Carousel';
- import { IBootstrapCarouselProps } from './IBootstrapCarouselProps';
- import { sp } from "@pnp/sp/presets/all";
- <Carousel>
- <Carousel.Item>
- <img
- className="d-block w-100"
- src="holder.js/800x400?text=First slide&bg=373940"
- alt="First slide"
- />
- <Carousel.Caption>
- <h3>First slide label</h3>
- <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
- </Carousel.Caption>
- </Carousel.Item><Carousel>
in BootstrapCarousel.tsx
- import * as React from 'react';
- import "bootstrap/dist/css/bootstrap.min.css";
- import Carousel from 'react-bootstrap/Carousel';
- import { IBootstrapCarouselProps } from './IBootstrapCarouselProps';
- import { sp } from "@pnp/sp/presets/all";
- export interface Iurl {
- //File: Array<urlproperties>;
- File: urlproperties;
- MyTitle:string;
- MYDesc:string;
- }
- export interface urlproperties {
- ServerRelativeUrl: string;
- }
- interface IPnpstate {
- MYPnpFile:Iurl[];
- }
- export default class BootstrapCarousel extends React.Component<IBootstrapCarouselProps, IPnpstate> {
- constructor(props: IBootstrapCarouselProps, state: IPnpstate) {
- super(props);
- this.state = {
- MYPnpFile: []
- };
- }
- public componentDidMount(){
- this._getpnpfile()
- .then((response: Iurl[]) => {
- console.log(response);
- this.setState({ MYPnpFile: response });
- });
- }
- private _getpnpfile() {
- /* sp.web.lists.getByTitle("PnpLibrary")
- .items.expand("Folder", "File").get()
- .then((items: any[]): void => {
- console.log(items);
- }, (error: any): void => {
- });*/
- return sp.web.lists.getByTitle("PnpLibrary")
- .items.expand("Folder", "File").top(5).get();
- }
- public render(): React.ReactElement<IBootstrapCarouselProps> {
- return (
- <div >
- { this.state.MYPnpFile.length>0 && <MybootstrapCarousel bindoutput={this.state.MYPnpFile} />}
- </div>
- );
- }
- }
- const MybootstrapCarousel = (props) => {
- //var abc= (document.getElementById("textBox2") as HTMLInputElement).value ;
- const Bindvalue = props.bindoutput.map((Outfile,index) =>
- <Carousel.Item>
- <img
- className="d-block w-100"
- src={Outfile.File.ServerRelativeUrl}
- alt="MY slide"
- />
- <Carousel.Caption>
- <h3>{Outfile.MyTitle}</h3>
- <p>{Outfile.MYDesc}</p>
- </Carousel.Caption>
- </Carousel.Item>
- );
- const MAslider = (
- <Carousel interval={2000} nextIcon={<span aria-hidden="true" className="carousel-control-next-icon" />}>
- {Bindvalue}
- </Carousel>
- );
- return (
- <>
- {MAslider}</>
- );
- };
Here I am using list name as PnpLibrary,MyTitle as single line of text and MYDesc as multi line of text.
Important Properties
- activeIndex - Controls the currently visible slide
- controls - Shows the Carousel previous and next arrows for changing the current slide(Default-true)
- fade - Cross fade slides instead of the default slide animation(Default-false)
- indicators - Show a set of slide position indicators(Default-true)
- interval -The amount of time to delay between automatically cycling an item. If
null, carousel will not automatically cycle.(Default-5000ms) - keyboard - Whether the carousel should react to keyboard events.(Default-true)
- nextIcon - Overrides the default button icon for the "next" control(eg
<span aria-hidden="true" className="carousel-control-next-icon" />) - onSelect - Callback fired when the active item changes.
(eventKey: number, event: Object | null) => void - onSlid - Callback fired when a slide transition ends.
(eventKey: number, direction: 'left' | 'right') => void - onSlide - Callback fired when a slide transition starts.
(eventKey: number, direction: 'left' | 'right') => void - pause - If set to this, pauses the cycling of the carousel on
mouseenterand resumes the cycling of the carousel onmouseleave. If set tofalse, hovering over the carousel won't pause it. (default-hover) - prevIcon - Override the default button icon for the "previous" control(eg.
<span aria-hidden="true" className="carousel-control-prev-icon" />) - slide - Enables animation on the Carousel as it transitions between slides. (Default-true)
- touch - Whether the carousel should support left/right swipe interactions on touchscreen devices. (Default-true)
Expected output

Conclusion
We learned how to implement bootstrap carousel in SPFX. I hope this
helps someone. Happy coding :)

Rajkiran SwainPosted Dec 13, 2021, 9:38 AM
Can you please help me to bind PropertyPaneSlider value inside Carousal Interval time .
Raj AkbarPosted Sep 18, 2020, 12:30 AM
Hi Madhan can you please upload the githut project here?
Anisha AntonyPosted Jun 11, 2020, 11:10 AM
Thank you for this post. It really helps