Wednesday, June 13, 2018

Create a dynamic content slider against a SharePoint list using jQuery. SharePoint 2007, 2010, 2013

On the front page of many SharePoint sites there is a section called "news" or "announcements". Located on the home page of your company or organization site, this is a quick way to disseminate important information to all users of your SharePoint site.

This is usually set up using a SharePoint list that contains the announcements or news items, and a content query web part on the front page gets the most recently published news item or article. This is a quick way to update the home page without having to edit the html source on that page every time the news or announcement changes.

However, the issue with this approach is that it only shows one announcement at a time. Sometimes, there may be multiple news items or announcements to show on the home page, but you don't have the space on the page for multiple items.

I was tasked with taking an existing news content query web part and replacing it with a Content Slider. This would show each news item on the page for a few seconds, and then scroll to the right for the next news item. A group of buttons at the bottom allows direct control of which news item you wish to see.

This is a fairly standard way of presenting information on the web, and so there are a lot of javascript and css based solutions for this challenge available. However, I wanted to host this solution in SharePoint 2007, and wanted to be able to use jQuery to extract information from the SharePoint news list. So it made sense to look into a few of the available jQuery content sliders to see which would meet my needs. The final choice ended up being awShowcase, you can download it from here:

Awkward Showcase on GitHub

Step 1: Download supporting libraries.

Within the SharePoint site that is going to be hosting your content slider, locate your site assets library. If it doesn't exist, create a document library called site assets.

Download and unzip the AWShowcase files locally, and place the unzipped files and folders in your site assets library. Download the jquery source file and SP Services jquery source files and place those in your site assets library as well. Your site assets library should look similar to this:


Step 2: Update your news or announcements library.

If you have an existing news or announcements list, you will need to add and update a couple of fields to make it work with this content slider. The first thing to add is a field called "Slider". This is a straight text field. If you want your announcement or news article to appear in the slider, you put the letter "Y" in this textbox.

You may already have a start date associated with your announcement/news list. If not, add it now. If you name it "StartDate" as one word, then you won't need to change the jquery for that field later.
You will also have a field that contains the announcement content. In my case, I called the field "HTML" because it allows rich html content to give richer tools to people entering the slider content. You will need to change the field name in the code below if it is not "HTML".

If you have not already enabled it, enable approval for the announcments/news list. The list items must be approved to show on the content slider.

Step 3: Add and populate the content editor web part.

Navigate to the page you wish to display the new content slider on and edit the web part page. Add a new content editor web part. Edit this web part and choose to edit the HTML source.

Add links to your supporting libraries at the top of your html content:

Now, copy and paste the following code underneath your supporting library links:



Step 4 - Modify the code to suit your environment.

Make the following changes in the code:
  1. Update all of the view fields in the CAML query to match your field names. So if the field I call "HTML" is called "Announcement" in your source list, change all instances of HTML to "Announcement". Make sure you change the names in all locations, use search and replace.
  2. Update the name of the announcements list to match the name of your source list. In the code above, our list is called "spotlight".
  3. Refer to the github link at the beginning of this article to understand the showcase property customizations. You will want to modify the width and height to fit your page.
Save the content editor web part code in SharePoint, and close the web part configuration window. Refresh the page to review the functionality. If the page doesn't work, first ensure you have the libraries correctly linked by uncommenting the "alert('jquery loaded')" line by removing the two forward slashes. Save you changes and refresh the page. If an alert pops up, jquery is working, if not, then that is the problem.

The other potential source of issues is within the field names. Be sure your field names are the internal SharePoint names for those items, rather than the name displayed on the list. If you are unsure what the internal name is, open properties for that list, and hover over the column names. The link that appears will contain the actual field name at the end.

The field looks like "Start Date" but when we hover over the field we see this URL

/_layouts/FldEdit.aspx?List=%7B06826D90%2D478A%2D423F%2DA762%2DDFCDD7005964%7D&Field=StartDate

we know the actual field name is "StartDate".

1 comment:

Greg said...

Hi, does this plugin HAVE to be within a content editor, as I have a custom landing page that hides all webparts and navs under it? can it be simply placed in a html and run that way? thanks

Post a Comment