2

I am trying to update a Page's content using SPServices.

When I run my code, it does update the correct page, but instead of updating the content, it just removes the content.

var newContent = "<p>This is a test</p>";

$().SPServices({
  operation: "UpdateListItems",
  listName: "Pages",
  ID: itemID,
  async: false,
  valuepairs: [["PublishingPageContent", newContent]]
})
1

Most probably it occurs since UpdateListItems operation accepts HTML escaped string as a parameter.

The following function could be used for encoding HTML string:

function htmlEncode(value){
  //create a in-memory div, set it's inner text(which jQuery automatically encodes)
  //then grab the encoded contents back out.  The div never exists on the page.
  return $('<div/>').text(value).html();
}

Source

Fixed example

var itemID = 4;
var newContent = "<p>Some content goes here</p>";
$().SPServices({
   operation: "UpdateListItems",
   listName: "Pages",
   ID: itemID,
   async: false,
   valuepairs: [["PublishingPageContent", htmlEncode(newContent)]],
   completefunc: function(xData, Status)
   {
         console.log('Page has been updated');      
   }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.