Home > SharePoint > jQuery Ticker for SharePoint Announcement List

jQuery Ticker for SharePoint Announcement List

I had a request to add a news ticker to a SharePoint page that would be driven from data contained in a SharePoint announcement list.  I recently read a blog post written by Jan Tielen illustrating how to get SharePoint list items using jQuery.   This seemed pretty straight forward, but I needed something animated.

To recap what Jan Tielen did, he illustrated how to use jQuery to retrieve SharePoint list items with the following code:

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>Tasks</listName> \
                        <viewFields> \
                            <ViewFields> \
                               <FieldRef Name='Title' /> \
                           </ViewFields> \
                        </viewFields> \
                    </GetListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";

        $.ajax({
            url: "_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: processResult,
            contentType: "text/xml; charset=\"utf-8\""
        });
    });

    function processResult(xData, status) {
        $(xData.responseXML).find("z\\:row").each(function() {
            var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
            $("#tasksUL").append(liHtml);
        });
    }
</script>

<ul id="tasksUL"/> 


A search for jQuery news tickers led me to Sam Collett’s TexoTela Blog.  The news ticker example there seemed to be a perfect fit for what I wanted and is driven from an unordered list.  This just happens to be the output of Jan’s example.  Seems like all my work was done for me!  Well, turns out, it almost was.

The news ticker is a jQuery extension available at jQuery SVN.  There are a couple of ways to implement it including an option to add the ticker to the page and add the list items via JavaScript as shown in the example below from the blog post.

var newnews = $("<ul>").attr("class", "newsticker");
newnews.append("<li>News Item 1</li>");
newnews.append("<li>News Item 2</li>");
newnews.append("<li>News Item 3</li>");
newnews.append("<li>News Item 4</li>");
newnews.appendTo("#mynews").newsTicker();


To put it all together, I did the following:

  • Added the jQuery library and news ticker plug-in scripts to a document library in the site.  I’m not a big fan of external references when I can avoid them.
  • Created a new Content Editor Web Part on my page and copied in Jan’s code.  I then changed the javascript references to point to the js files in my document library.
  • I added a style section to the HTML to format the ticker a bit differently as recommended in the news ticker post.
  • I updated the web service request to point to my announcements list.
  • I updated the processResult function to add and populate the <ul> tag using the script illustrated above.

This was the final result:

<script type="text/javascript" src="/SiteFiles/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/SiteFiles/jquery.newsticker.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>Announcement Test</listName> \
                        <viewFields> \
                            <ViewFields> \
                               <FieldRef Name='Title' /> \
                           </ViewFields> \
                        </viewFields> \
                    </GetListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";
        $.ajax({
            url: "_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: processResult,
            contentType: "text/xml; charset=\"utf-8\""
        });
    });

function processResult(xData, status) {
        var newnews = $("<ul>").attr("class", "newsticker");
        $(xData.responseXML).find("z\\:row").each(function() {
            var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
            newnews.append(liHtml);
        });

        newnews.appendTo("#tasksUL").newsTicker();
    }
</script>

<style type="text/css">
.newsticker {
    list-style-type: none;
    background: red;
    margin: 0;
}
</style>

<ul id="tasksUL">
</ul> 

 

My example isn’t pretty and certainly needs some refinement, but it does prove the concept.  Furthermore, this technique that was applied using no development tools and requiring nothing more than contributor access to the site.

Author: Scott Categories: SharePoint Tags:
  1. santhosh
    July 17th, 2009 at 08:04 | #1

    Thanks for your excellent post.i need a one help.in my site i need to display a two different list in two different CEWP.how can i do.

    i have try to display Two list in two CEWP but it’s display in single CEWP.

    Please Help Me.

  2. Steve Cheng
    August 3rd, 2009 at 19:36 | #2

    Scott,

    Great job with the Sharepoint integration. I applied this to my site and it works, however, whenever the page is scroll to the point where this newsticker is partially out of view, the page jumps to the top when the next news appears.
    Is there any way to prevent this? It seems like a .focus() issue but I could not find it.

    I appreciate your help,
    Steve

  3. Scott
    August 11th, 2009 at 02:34 | #3

    @Steve Cheng

    I haven’t encountered that issue, but I have not deployed the code anywhere outside of a test environment. I’ll take a look as soon as I get a chance to let you know what I find.

    If you’ve figured out how to prevent the page jump, please let me know and I’ll update the post with your fix.

    Thanks

  4. Scott
    August 11th, 2009 at 02:35 | #4

    @santhosh
    I’m not sure I understand the problem. If you post some more details, I’ll be glad to try to help.

  5. Craig
    August 11th, 2009 at 14:47 | #5

    Hey Scott. Thanks, this is a great post and well explained. I’ve been trying to find a way of getting this to work in Sharepoint for some time now. Unforunately I’m still not quite there and I don’t know why (not being a developer doesn’t help!). I wonder if you could help at all?

    I copied the .js files to a doc library called ‘development code’ and changed my references as below. the list I am referencing is simply called ‘announcements’, so I changed the web service request appropriately.

    My page now loads without any errors but also doens’t display anything in the webpart. Any help would be greatly appreciated!

    regards,
    Craig

    $(document).ready(function() {
    var soapEnv =
    ” \
    \
    \
    Announcements \
    \
    \
    \
    \
    \
    \
    \
    “;
    $.ajax({
    url: “_vti_bin/lists.asmx”,
    type: “POST”,
    dataType: “xml”,
    data: soapEnv,
    complete: processResult,
    contentType: “text/xml; charset=\”utf-8\”"
    });
    });

    function processResult(xData, status) {
    var newnews = $(”").attr(”class”, “newsticker”);
    $(xData.responseXML).find(”z\\:row”).each(function() {
    var liHtml = “” + $(this).attr(”ows_Title”) + “”;
    newnews.append(liHtml);
    });

    newnews.appendTo(”#tasksUL”).newsTicker();
    }

    .newsticker {
    list-style-type: none;
    background: red;
    margin: 0;
    }

  6. Craig
    August 11th, 2009 at 14:52 | #6

    Apparently I can’t even post code correctly - or edit my post! doh! Please feel free to edit my previous post/delete this one as appropriate.

    $(document).ready(function() {
    var soapEnv =
    ” \
    \
    \
    Announcements \
    \
    \
    \
    \
    \
    \
    \
    “;
    $.ajax({
    url: “_vti_bin/lists.asmx”,
    type: “POST”,
    dataType: “xml”,
    data: soapEnv,
    complete: processResult,
    contentType: “text/xml; charset=\”utf-8\”"
    });
    });

    function processResult(xData, status) {
    var newnews = $(”").attr(”class”, “newsticker”);
    $(xData.responseXML).find(”z\\:row”).each(function() {
    var liHtml = “” + $(this).attr(”ows_Title”) + “”;
    newnews.append(liHtml);
    });

    newnews.appendTo(”#tasksUL”).newsTicker();
    }

    .newsticker {
    list-style-type: none;
    background: red;
    margin: 0;
    }

  7. Craig
    August 11th, 2009 at 15:23 | #7

    Further to my previous post(s), I’ve narrowed it down to the UL but still baffled.

    Message: Object doesn’t support this property or method
    Line: 867 ()

  1. July 23rd, 2009 at 04:01 | #1