Page layouts provide easy-to-use templates for content creators to use when creating SharePoint site content. In scenarios where a certain page design is used repetitively, a page layout is ideal. One example of this is an image carousel used by landing pages. In this recipe, we will create a JavaScript-based image carousel that displays images from a picture library in the SharePoint site.
To simplify the process of creating the JavaScript image carousel, we will use the jQuery framework (http://www.jquery.com) and jQuery bxSlider plugin (http://www.bxslider.com).
For this recipe, we will use the page layout we previously created in the Creating a page layout with three columns of web part zones recipe.
Follow these steps to create a page layout with an image carousel:
PageLayout_ThreeColumn.aspx
(for our example, we have renamed it PageLayout_ImageCarousel.aspx
).PageLayout_ImageCarousel.aspx
page layout.PageLayout_ImageCarousel.aspx
page layout.ImageCarousel.css stylesheet
file and an ImageCarousel.js
JavaScript file to the resources
folder located at _catalogs/masterpage/resources
.<SharePointWebControls:CssRegistration ID="customCss" name="<% $SPUrl:~Site/_catalogs/masterpage/resources/imagecarousel.css %>" runat="server"/> <SharePointWebControls:CssRegistration ID="bxSliderCss" name="<% $SPUrl:~Site/_catalogs/masterpage/resources/jquery.bxslider.css %>" runat="server"/> <SharePointWebControls:ScriptLink ID="jQuery" Name="~site/_catalogs/masterpage/resources/jquery-2.0.2.min.js" runat="server"></SharePointWebControls:ScriptLink> <SharePointWebControls:ScriptLink ID="bxSliderJs" Name="~site/_catalogs/masterpage/resources/jquery.bxslider.js" runat="server"></SharePointWebControls:ScriptLink> <SharePointWebControls:ScriptLink ID="customJavaScript" Name="~site/_catalogs/masterpage/resources/imagecarousel.js" runat="server"></SharePointWebControls:ScriptLink> <SharePointWebControls:ScriptLink Name="sp.js" runat="server" Localizable="false" LoadAfterUI="true" /> <SharePointWebControls:ScriptLink Name="sp.runtime.js" runat="server" Localizable="false" LoadAfterUI="true" /> <SharePointWebControls:ScriptLink Name="sp.core.js" runat="server" Localizable="false" LoadAfterUI="true" />
<div class="ms-table ms-fullWidth">
element.<div>
elements it contains, add the following <div>
tag to contain the image carousel:<div class="ImageCarousel"></div>
ImageCarousel.css
stylesheet, provide a default height, width, and display for the image carousel as follows:.ImageCarousel { height: 400px; width: 800px; display: block; }
ImageCarousel.js
JavaScript file, create a function to initialize the carousel and use the ExecuteOrDelayUntilScriptLoaded
function to execute the function after the SharePoint JavaScript files load as shown in the following code:function InitializeImageCarousel() { } ExecuteOrDelayUntilScriptLoaded(InitializeImageCarousel, "sp.js");
var context = new SP.ClientContext.get_current();
var web = context.get_web();
Images
picture library from the SharePoint site as follows:var list = web.get_lists().getByTitle('Images'),
var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<View><RowLimit>5</RowLimit></View>'),
var items = list.getItems(camlQuery);
Id
, Title
, and FileRef
properties as follows:context.load(items, 'Include(Id, Title, FileRef)'),
executeQueryAsync
method on the context to execute the query and provide delegate functions to execute on success or failure of the request, as shown in the following code:context.executeQueryAsync( Function.createDelegate(this, function (sender, args) { // Success }), Function.createDelegate(this, function (sender, args) { // Failed }));
Sys.Debug.trace('Request failed. ' + args.get_message() + ' ' + args.get_stackTrace());
<ul>
container object for the image carousel using the following line of code:var slider = $('<ul class="bxslider"></ul>'),
<li>
element representing the image using the following code:var listItemEnumerator = items.getEnumerator(); while (listItemEnumerator.moveNext()) { var oListItem = listItemEnumerator.get_current(); var itemHtml = $('<li><img title="' + oListItem.get_item("Title") + '" src="' + oListItem.get_item("FileRef") + '" /></li>'), itemHtml.appendTo(slider); }
<ul>
image carousel container to the following image carousel <div>
we have on the page:var imageCarousel = $('.ImageCarousel'), slider.appendTo(imageCarousel);
$('.bxslider').bxSlider({ captions: true, slideWidth: 750 });
<div>
to match the bxSlider
height to ensure there is no overlapping content:$('.ImageCarousel').height($('.bx-wrapper', $('.ImageCarousel')).height());
Image Carousel
).In addition to the server-side object model, SharePoint provides additional object models to interact with SharePoint content. In this recipe, we used the JavaScript object model (JSOM). Using JSOM, we retrieved the current site from the current SharePoint context. We used a CAML query to retrieve the Images picture library in the current site. We then created an unordered list with the images and used our JavaScript libraries and plugins (jQuery and bxSlider) to create our image carousel. Once we had our page layout created, we created a page with it to observe our image carousel.
3.141.42.116