Create a custom web part to display news

I had no idea how those news-like web parts were created until recently when I decided to try it out. I didn’t invent this myself, of course, thanks to Google, and lots of  SharePoint gurus who care to share. Also, I have to say, this is a really plain and simple web part I managed to create. After all, I am only a beginner in this journey. OK, let’s get started.

Add a custom field to a Content Query Web Part

In a standard CQWP, the fields available are Link, Image, Title and Description. I wanted to add the Article Date so it’s more like a News summary web part. This is done through modifying the XSL style sheet, the file name is ItemStyle. Instead of change the default one, I’m going to create a custom XSL file.

  1. Site contents > Style Library > XSL Style Sheets
  2. Download the ItemStyle file, save as ‘CustomItemStyle’ and use an Editor to open the file.
  3. Copy the lines of the Default Style and copy it right after the lines.
  4. Edit the new style name by replacing the first line with
    Note: This will be the Item style name your users can select.
  5. Go to the bottom of this style, just above < div class=”description”>, insert
    < div class="article-date">
      < xsl:value-of select="ddwrt:FormatDate(@DisplayDate, 2057, 3)"/>
    < /div>

    Notes:
    1. DisplayDate will be the field name for displaying the article date of an item.
    2. By default, the date will be formatted to YYYY-MM-DD hh:mm:ss. The format bit I added in makes the dates look like this 23 June 2016.
    3. For this FormatDate function to work, you will need this line at the beginning of the file.
    xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime
    4. Remove the space after < in each line for the code to work

  6. Save the XSL file and upload it to the XSL Style Sheets library.
  7. Check in and publish as a major version

Create a custom Content Query Web Part

Now, we need to point the Content Query Web Part to the custom style file we just created.

  1. On a page, add a Content Query Web Part
  2. Edit the Web Part properties and change anything that you wanted to include it into the custom web part. For example, I change the Web Part title to be News.
  3. Click the down arrow and click Export
  4. Edit the exported file in a text editor
  5. Edit the node with the name “ItemXslLink” to be /sites/[sitecollection]/Style%20Library/XSL%20Style Sheets/CustomItemStyle.xsl
  6. Save the file.

Upload the custom web part

The last thing we do here is to upload the web part file into the site collection.

  1. Site Settings, Click Web parts under Web Designer Galleries
  2. From the Files tab, select upload
  3. Rename the web part if you want to, and select/create a Web Part group for it.

Add the custom web part to a page

You will find the web part in the group you selected in the previous step. As you edit the properties of the web part, note the style “WithArticleDate” is available with an additional line for DisplayDate.
customnews.JPG

Here is my end product
newsitems.JPG

Thanks to the authors of the following articles:

Displaying Custom Fields in a SharePoint 2013 Content Query Web Part

Custom Date Formats in SharePoint XSL

Customizing the Content Query Web Part and Custom Item Styles

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s