Tuesday, August 28, 2012

SharePoint 2007 - Save RSS Feed into List using JQuery

The idea is to fetch the RSS feed from multiple sources and then save into the SharePoint 2007 List so that we can perform search, archive the RSS news, display data nicely in Data View etc etc.



The following JQuery script will fetch the RSS feed from multiple sources and then save the data into the SharePoint 2007 list via lists.asmx:
<script src="/Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>


<script type="text/javascript">
  
    function LoadFeed() 
    {
        var varFeedURL = new Array();
            varFeedURL[0] = "http://feeds.bbci.co.uk/news/business/rss.xml";
            varFeedURL[1] = "http://www.ft.com/rss/home/uk";
            varFeedURL[2] = "http://www.ft.com/rss/home/us";

            for (i = 0; i < varFeedURL.length; i++)
            {

                $.get(varFeedURL[i], function(data) {
                    var $xml = $(data);
                    $xml.find("item").each(function() {
                        var $this = $(this),
                            item = {
                                title: $this.find("title").text(),
                                link: $this.find("link").text(),
                                description: $this.find("description").text(),
                                pubDate: $this.find("pubDate").text()
                        }
                        
                        
                        //Show only Today's Items
                        var a = new Date(item.pubDate);
                        var b = new Date(); //Today's Date
                        
                        var msDateA = Date.UTC(a.getFullYear(), a.getMonth()+1, a.getDate());
                           var msDateB = Date.UTC(b.getFullYear(), b.getMonth()+1, b.getDate());
                                                   
                        if (parseFloat(msDateA) == parseFloat(msDateB))
                        {
                        
                            var content = document.getElementById('content');
                            content.appendChild(document.createTextNode(item.title)); 
                            content.appendChild(document.createElement('br'));
                            content.appendChild(document.createTextNode(item.description));
                            content.appendChild(document.createElement('br'));
                            content.appendChild(document.createTextNode(item.link));
                            content.appendChild(document.createElement('br'));
                            content.appendChild(document.createElement('hr'));
                            
                            CreateNewItem(item.title, item.description, item.link);
                        }
                        
                    });
                });
            }
        alert('Done');
    }        
        
        
        function CreateNewItem(varTitle, varDescription, varLink) 
        {
                    var batch =
                        "<Batch OnError=\"Continue\"> \
                            <Method ID=\"1\" Cmd=\"New\"> \
                                <Field Name=\"Title\">" + varTitle + "</Field> \
                                <Field Name=\"Description\">" + varDescription + "</Field> \
                                <Field Name=\"Link\">" + varLink + "</Field> \
                            </Method> \
                        </Batch>";
                
                    var soapEnv =
                        "<?xml version=\"1.0\" encoding=\"utf-8\"?> \
                        <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
                            xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \
                            xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
                          <soap:Body> \
                            <UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"> \
                              <listName>RSSData</listName> \
                              <updates> \
                                " + batch + "</updates> \
                            </UpdateListItems> \
                          </soap:Body> \
                        </soap:Envelope>";
                
                    $.ajax({
                        url: "http://portalstg.amr.kworld.kpmg.com/_vti_bin/lists.asmx",
                        beforeSend: function(xhr) {
                            xhr.setRequestHeader("SOAPAction",
                            "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
                        },
                        type: "POST",
                        dataType: "xml",
                        data: soapEnv,
                        complete: processResult,
                        contentType: "text/xml; charset=utf-8"
                    });
                }
                
                function processResult(xData, status) {
                    //alert(status);
                }
        
  
    </script>

<input id="btnGo" onclick="LoadFeed()" type="button" value="Get RSS Data" />


Ref: http://weblogs.asp.net/jan/archive/2009/04/10/creating-list-items-with-jquery-and-the-sharepoint-web-services.aspx


1 comment:

  1. summer wildlife camp
    Offer your kids a lifetime journey of wild by enrolling them at Frolic Boonies 2013 summer camp! Adventure-packed activities such as trekking, bird watching, horse riding, sky watching etc to provide a unique & entirely fresh summer.

    for more details log on to :- http://www.wildfun.in/feed/

    ReplyDelete

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...