In this post, I wanted to show how you can modify the SharePoint framework Hello World web part and add other custom JavaScript libraries to create a simple slideshow.
Prerequisites
I’m going to start my tutorial after following these steps:
Setup SharePoint Tenant
Setup your machine
HelloWorld WebPart
HelloWorld, Talking to SharePoint
Once that is all working fine, you should have something that looks like this:
Note: In the solution below, I have removed some of the HTML rendered in the SolutionNameWebPart.ts file (optional):
this.domElement.innerHTML = ` <div class="${styles.solutionName}"> <div class="${styles.container}"> <div id="spListContainer" /> </div> </div>`;
Pull in images from SharePoint picture library
Create a picture library in your SharePoint site called “Slideshow”. Upload a couple of images into this library for testing purposes.
Inside your project, open up SolutionNameWebPart.ts (found inside your WebPart folder). Change the REST API call so that the picture library item URLs are fetched. Currently the REST query (found in the _getListData function) is pulling list and library names, change it to:
this.context.pageContext.web.absoluteUrl + "/_api/web/lists/GetByTitle('slideshow')/Items?$select=EncodedAbsUrl"
This will return the picture library item URLs.
Add the EncodedAbsUrl as a string to the ISPList object:
export interface ISPList { Title: string; Id: string; EncodedAbsUrl: string; }
In the “_renderList” function, change the item loop to this:
items.forEach((item: ISPList) => { html += `<img src="${item.EncodedAbsUrl}" alt="image" />`; });
This will now use the EncodedAbsUrl as the image location. Running this using gulp serve should now show the images from the picture library. You may want to add in some mock data for local tests.
Making it responsive
The images are rendered at their actual size. Some CSS is required to make the images responsive. Add the class ${styles.cdbImage} to the image tag.
html += `<img src="${item.EncodedAbsUrl}" class="${styles.cdbImage}" alt="image" />`;
Open the SolutionName.module.scss file and add the following code inside the last brace.
.cdbImage{width:100%;height:auto;}
Serve the files again and the images will now be responsive.
Adding jQuery and Cycle2
Download using Node Package Manager
When adding common JavaScript libraries to projects, Node Package Manager is an excellent tool to quickly bundle items. Run the following nodeJS package manager commands:
npm install jquery
npm install jquery-cycle-2
Two extra folders are now created under “node_modules”.
Install TypeScript definitions
In order to use these libraries in TypeScript, a definition file is required for IntelliSense and compilation. jQuery can be added via the TypeScript definition tool in the nodeJS command line:
tsd install jquery –save
jQuery Cycle2 is not available via this command line but can be downloaded from here:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/jquery.cycle2/jquery.cycle2.d.ts
Download it and add it to a new folder called jquerycycle under the “typings” folder. The typings folder contains all the TypeScript definition files. jQuery has automatically been added here. However we need to manually add the Cycle2 definition.
In the root of the typings folder, open the file named tsd.d.ts. This file controls all the definitions which are used in the project. jQuery has already been added, manually add a new line for jQuery Cycle2.
/// <reference path="jquery/jquery.d.ts" /> /// <reference path="jquerycycle/jquery.cycle.d.ts" />
Add libraries to project
Open the config.json file (under the config folder) in the project. This lists all the external references. jQuery and Cycle2 need to be added here so they can be used in the project. In the “externals” section, add:
"jquery": "node_modules/jquery/dist/jquery.js", "jquery-cycle": "node_modules/jquery-cycle-2/src/jquery.cycle.all.js"
The libraries can now be included in the project. Go back to the solutionNameWebPart.ts file and add:
import * as myjQuery from 'jquery'; require('jquery-cycle');
The object myjQuery should be a unique name for your project to avoid conflicts. jQuery cycle is added using require as it has a dependency on jQuery.
At the end of the _renderList function in the web part class, add the following code to initialise the slideshow:
myjQuery( document ).ready(function() { myjQuery('#spListContainer').cycle(); });
Refreshing the page, should now give you a responsive slideshow.