UpdateListItems issue handling tags in text fields

Jan 13, 2011 at 6:44 PM

I am certainly having way to much fun with SPServices!!

I have been working on a way to bring in RSS feeds into a list, without creating duplicates, to allow adding our own metadata alongside the feed.  This will be particularly helpful when reading in information from one SharePoint site to another.

The only issue I am encountering is that information comes in (via jFeed which I highly recommend) the rss description field, which I then pass it to a new item's Description field (multiline - plain text, rich text, does not matter) - it drops the text as soon as it reaches the first tag.  For most RSS feeds this is fine, I simply lose all the social media tags, but with a SharePoint list, the first value in the rss description field is always <div>.

I was looking for a way to handle this, without going through a painful split process that would be unique to every list I encounter.

Is there any way to get the SPService to bring in the text, and ignore the fact that the tags are there?

Here is my code - thanks!!

<SCRIPT type=text/javascript src="http://idnetmonitor/sites/rc/mastercode/jquery.current.min.js"></SCRIPT>
<SCRIPT type=text/javascript src="http://idnet.dominionnet.com/sites/itbaet/ResourceCenter/Java/jquery.jfeed.js"></SCRIPT>
<SCRIPT type=text/javascript src="http://idnetmonitor/sites/rc/mastercode/jquery.SPServices.current.min.js"></SCRIPT>
<SCRIPT type=text/javascript>


function CopyRSS()
 {

$(document).ready(function() {

// getting the information for each item in the RSS feed, returned as variables item.title, item.id, item.link, item.description, item.updated

    jQuery.getFeed({
      url: 'http://rss.cnn.com/rss/cnn_latest.rss',
      success: function(feed) {

            jQuery('#result').append();
            
            var html = '';
            
            for(var i = 0; i < feed.items.length && i < 250; i++) {
            
             var item = feed.items[i];


// getting a count of rows where the item.id already exists in the list
  $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "cnn2",
        CAMLViewFields: "<ViewFields><FieldRef Name='FeedID' /></ViewFields>",
        CAMLQuery: "<Query><Where><Eq><FieldRef Name='FeedID' /><Value Type='Text'>" + item.id + "</Value></Eq></Where></Query>",
        CAMLRowLimit: 1,
        completefunc: function (xData, Status) {
        $(xData.responseXML).find("rs\\:data").each(function() {
          var  itemCount = $(this).attr("ItemCount");

//  if the row count = 0, i.e. the item is not already in the list, it is added
 if(itemCount==0){

	$().SPServices({
	        operation: "UpdateListItems",
	        async: false,
	        listName: "cnn2",
	        updates: "<Batch OnError='Continue' PreCalc='TRUE'>" +
	                    "<Method ID='1' Cmd='New'>" +
	                        "<Field Name='Title'>" + item.title + "</Field>" +
	                        "<Field Name='Link'>" + item.link + "</Field>" +
	                        "<Field Name='Updated'>" + item.updated + "</Field>" +
	                        "<Field Name='FeedID'>" + item.id + "</Field>" +
	                        "<Field Name='Description'>" + item.description + "</Field>" +
	                    "</Method>" +
	                "</Batch>",
	        completefunc: function (xData, Status) {
	        }
	      });
             }
          });
           }
          });
         }
        } 
      });
    });
   }

</script>

<input type=button value="Create List Items from RSS feed" onClick="javascript:CopyRSS();"/>
Jan 13, 2011 at 6:50 PM

More robust searching in the discussions came up with this little piece of code, which I am not sure how to use to clean up the variable item.description that the jFeed retrieves:

function escapeHTMLEncode(str) {
              var div = document.createElement('div');
              var text = document.createTextNode(str);
              div.appendChild(text);
              return div.innerHTML;
       }

Am a JavaScript novice - how would this be used in my code? will it clean up all "<" and ">".

Thanks!

Bill

Coordinator
Jan 15, 2011 at 4:59 PM

Bill:

I think it is indeed an escaping issue. You need to pass the item.description escaped for it to "make it" into the list item.

There's a private function in SPServices that I use to do this, and you could do the same:

 // Escape string characters
 function escapeHTML(s) {
  return s.replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
 }

So, to just plant the logic directly into your code:

 "<Field Name='Description'>" + item.description.replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;').replace(/>/g,'&gt;') + "</Field>" +

M.

Jan 18, 2011 at 2:04 PM

Marc,

That worked perfectly - now RSS feeds I pull in from commercial resources, as well as from internal SharePoint lists come out formatted beautifully.

Thanks so much for your guidance!

Bill

Coordinator
Jan 18, 2011 at 2:09 PM

Excellent! And you're welcome.

M.