storing Rich Text Editor data

Feb 6, 2015 at 7:59 PM
Edited Feb 6, 2015 at 8:00 PM
I 've implemented a YUI 2.9.0 Rich Text Editor, and things look like they are going to go alright, but I get an "error" returned from my attempt to save the data back to the Enhanced Rich Text field. It essentially doesn't save any changes.

Here's the code I have to store it:
function updateCandidateItems(candidateID, candidateHash) {

    var userID = candidateHash['candidateUserID'];
    var candidateTitleAndOrRank = candidateHash['candidateTitleAndOrRank'];
    var candidateName = candidateHash['candidateName'];
    var candidateDir = candidateHash['candidateDir'];
    var candidateLoc = candidateHash['candidateLoc'];
    var candidateEmail = candidateHash['candidateEmail'];
    var candidatePhone = candidateHash['candidatePhone'];
    var candidateYearsAtStrat = candidateHash['candidateYearsAtStrat'];
    var candidateClass = candidateHash['candidateClass'];
    var candidateSeek = candidateHash['candidateSeek'];
    var candidateGenderPref = candidateHash['candidateGenderPref'];
    var candidateMentorDescription = candidateHash['candidateMentorDescription'];
    var candidateProtegeDescription = candidateHash['candidateProtegeDescription'];
    var candidateComments = candidateHash['candidateComments'];


    $().SPServices({
        operation: "UpdateListItems",
        batchCmd: "Update",
        listName: "mentoringList",
        ID: candidateID,
        valuepairs: [["Title", userID],
                     ["userID", candidateTitleAndOrRank],
                     ["Name", candidateName],
                     ["Directorate", candidateDir],
                     ["email", candidateEmail],
                     ["phone", candidatePhone],
                     ["yearsAtUSSTRATCOM", candidateYearsAtStrat],
                     ["classification", candidateClass],
                     ["seeking", candidateSeek],
                     ["genderPref", candidateGenderPref],
                     ["mentorDescription", candidateMentorDescription],
                     ["protegeDescription", candidateProtegeDescription],
                     ["comments", candidateComments]],
        completefunc: function (xData, Status) {
            alert("Status: " + Status);
            if (Status == "Error") {
                console.log ("Unable to communicate with Sharepoint Server.  Please try again at a later time.");
            } else if ($(xData).hasSPError()) {
                console.log("An Error Has Occurred. Sharepoint Error: " + $(xData).getSPError());
                return;
            } else {

                alert("User Information Saved!");
            }

        }
    });


    return;
}

It returns that it has saved the user information, but when I debug, I get a message that the status is "error" Note: not "Error". When I look at the xData response I get:
xData.readyState = 4
xData.responseText = ""
xData.responseXML = null
xData.status = 400
xData.StatusText = "Bad Request"
I am attempting this on IE10 in Browser Mode: IE10 Compat View, Document Mode: Standards.

Any ideas?

Thank You
Feb 6, 2015 at 8:16 PM
Edited Feb 6, 2015 at 8:17 PM
Oh, here's the data before an edit:
<div class=ExternalClass5746AEBD20ED402FA1C4EC9B08D5E8DB>
<div><strong>Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of the year. For another, he really wanted to do his homework but was forced to do it in secret, in the dead of night. And he also happened to be a wizard. <br>~ Narrator <br></strong><br><br>Non-magic people (more commonly known as Muggles) were particularly afraid of magic in medieval times, but not very good at recognizing it. On the rare occasion that they did catch a real witch or wizard, burning had no effect whatsoever. The witch or wizard would perform a basic Flame Freezing Charm and then pretend to shriek with pain while enjoying a gentle, tickling sensation. Indeed, Wendelin the Weird enjoyed being burned so much that she allowed herself to be caught no less than forty-seven times in various disguises. <br>~ Bathilda Bagshot, A History of Magic <br><br><br>&quot;HELLO? HELLO? CAN YOU HEAR ME? I - WANT - TO - TALK - TO - HARRY - POTTER -&quot; <br>&quot;THERE IS NO HARRY POTTER HERE! I DON'T KNOW WHAT SCHOOL YOU'RE TALKING ABOUT! NEVER CONTACT ME AGAIN! DON'T YOU COME NEAR MY FAMILY!&quot; <br>~ Ron Weasley, Vernon Dursley; conversation on the telephone <br><br><br>&quot;Don't let the Muggles get you down!&quot; <br>~ Ron Weasley <br><br><br>&quot;Harry - this is a Pocket Sneakoscope. If there's someone untrustworthy around, it's supposed to light up and spin. Bill says it's rubbish sold for wizard tourists and isn't reliable, because it kept lighting up at dinner last night. But he didn't realize Fred and George had put beetles in his soup.&quot; <br>~ Ron Weasley <br><br><br>Extremely unusual though he was, at that moment Harry Potter felt just like everyone else - glad, for the first time in his life, that it was his birthday. <br>~ Narrator <br><br>Testing, Testing 1 - 2 - 3 - 4 <br></div></div>

And here is it before it goes into the "UpdateListItems":
<div class="ExternalClass5746AEBD20ED402FA1C4EC9B08D5E8DB">
<div><strong>Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of the year. For another, he really wanted to do his homework but was forced to do it in secret, in the dead of night. And he also happened to be a wizard. <br>~ Narrator <br></strong><br><br>Non-magic people (more commonly known as Muggles) were particularly afraid of magic in medieval times, but not very good at recognizing it. On the rare occasion that they did catch a real witch or wizard, burning had no effect whatsoever. The witch or wizard would perform a basic Flame Freezing Charm and then pretend to shriek with pain while enjoying a gentle, tickling sensation. Indeed, Wendelin the Weird enjoyed being burned so much that she allowed herself to be caught no less than forty-seven times in various disguises. <br>~ <strong>Bathilda Bagshot, A History of Magic <br></strong><br><br>"HELLO? HELLO? CAN YOU HEAR ME? I - WANT - TO - TALK - TO - HARRY - POTTER -" <br>"THERE IS NO HARRY POTTER HERE! I DON'T KNOW WHAT SCHOOL YOU'RE TALKING ABOUT! NEVER CONTACT ME AGAIN! DON'T YOU COME NEAR MY FAMILY!" <br>~ Ron Weasley, Vernon Dursley; conversation on the telephone <br><br><br>"Don't let the Muggles get you down!" <br>~ Ron Weasley <br><br><br>"Harry - this is a Pocket Sneakoscope. If there's someone untrustworthy around, it's supposed to light up and spin. Bill says it's rubbish sold for wizard tourists and isn't reliable, because it kept lighting up at dinner last night. But he didn't realize Fred and George had put beetles in his soup." <br>~ Ron Weasley <br><br><br>Extremely unusual though he was, at that moment Harry Potter felt just like everyone else - glad, for the first time in his life, that it was his birthday. <br>~ Narrator <br><br>Testing, Testing 1 - 2 - 3 - 4 <br></div></div> 


Note, I did an update by bolding the <strong>Bathilda Bagshot, A History of Magic <br></strong>.

When I get the reload back, I get no change.
Coordinator
Feb 6, 2015 at 9:49 PM
You'll need to escape the HTML so that the XML going to the server is valid. You text above becomes what I show below.

M.
&lt;div class=&quot;ExternalClass5746AEBD20ED402FA1C4EC9B08D5E8DB&quot;&gt;<br/>&lt;div&gt;&lt;strong&gt;Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of the year. For another, he really wanted to do his homework but was forced to do it in secret, in the dead of night. And he also happened to be a wizard. &lt;br&gt;~ Narrator &lt;br&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;Non-magic people (more commonly known as Muggles) were particularly afraid of magic in medieval times, but not very good at recognizing it. On the rare occasion that they did catch a real witch or wizard, burning had no effect whatsoever. The witch or wizard would perform a basic Flame Freezing Charm and then pretend to shriek with pain while enjoying a gentle, tickling sensation. Indeed, Wendelin the Weird enjoyed being burned so much that she allowed herself to be caught no less than forty-seven times in various disguises. &lt;br&gt;~ &lt;strong&gt;Bathilda Bagshot, A History of Magic &lt;br&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;&quot;HELLO? HELLO? CAN YOU HEAR ME? I - WANT - TO - TALK - TO - HARRY - POTTER -&quot; &lt;br&gt;&quot;THERE IS NO HARRY POTTER HERE! I DON'T KNOW WHAT SCHOOL YOU'RE TALKING ABOUT! NEVER CONTACT ME AGAIN! DON'T YOU COME NEAR MY FAMILY!&quot; &lt;br&gt;~ Ron Weasley, Vernon Dursley; conversation on the telephone &lt;br&gt;&lt;br&gt;&lt;br&gt;&quot;Don't let the Muggles get you down!&quot; &lt;br&gt;~ Ron Weasley &lt;br&gt;&lt;br&gt;&lt;br&gt;&quot;Harry - this is a Pocket Sneakoscope. If there's someone untrustworthy around, it's supposed to light up and spin. Bill says it's rubbish sold for wizard tourists and isn't reliable, because it kept lighting up at dinner last night. But he didn't realize Fred and George had put beetles in his soup.&quot; &lt;br&gt;~ Ron Weasley &lt;br&gt;&lt;br&gt;&lt;br&gt;Extremely unusual though he was, at that moment Harry Potter felt just like everyone else - glad, for the first time in his life, that it was his birthday. &lt;br&gt;~ Narrator &lt;br&gt;&lt;br&gt;Testing, Testing 1 - 2 - 3 - 4 &lt;br&gt;&lt;/div&gt;&lt;/div&gt; 
Marked as answer by trout0525 on 2/10/2015 at 7:30 AM
Feb 7, 2015 at 1:28 AM
Edited Feb 7, 2015 at 1:42 AM
Marc is right, You need to use a function to scape the HTML.
function fn_UpdateListItem(){

    var link = htmlEscape('<a href='http://www.google.nl'>its a test.</a>');

    $().SPServices({
        operation: 'UpdateListItems',
        listName: 'Bedrijven',
        ID: 1,
        valuepairs: [["Software", link]],
        completefunc: function(xData, Status) {
            alert('test complete');
        }
     });
}

//This function makes the magic
function htmlEscape(str) {
    return String(str)
        .replace(/&/g, '&amp;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');
}
Marked as answer by trout0525 on 2/10/2015 at 7:30 AM
Feb 7, 2015 at 7:48 AM
I thought using the jQuery .html() function would do that, but it didn't appear to help. Is the htmlEscape function any different?
Coordinator
Feb 7, 2015 at 2:34 PM
Edited Feb 7, 2015 at 2:35 PM
jQuery's .html() just gets you the HTML stored in the element. The escaping is a separate thing. There you're converting characters from one thing
"<"
to another
"&lt;"
M.
Feb 9, 2015 at 1:28 PM
Edited Feb 9, 2015 at 1:31 PM
Marc and Tabares,

Your replies look promising, but they don't appear to work completely. I've implemented the function htmlEscape, Tabares. I get what looks like a response like this goin to the server:
<div class="ExternalClass5746AEBD20ED402FA1C4EC9B08D5E8DB">
<div><strong>Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of the year. For another, he really wanted to do his homework but was forced to do it in secret, in the dead of night. And he also happened to be a wizard. <br>~ Narrator <br></strong><br><br>Non-magic people (more commonly known as Muggles) were particularly afraid of magic in medieval times, but not very good at recognizing it. On the rare occasion that they did catch a real witch or wizard, burning had no effect whatsoever. The witch or wizard would perform a basic Flame Freezing Charm and then pretend to shriek with pain while enjoying a gentle, tickling sensation. Indeed, Wendelin the Weird enjoyed being burned so much that she allowed herself to be caught no less than forty-seven times in various disguises. <br>~ Bathilda Bagshot, A History of Magic <br><br><br>"<strong><font style="font-size: 17px;">HELLO? HELLO?</font></strong> CAN YOU HEAR ME? I - WANT - TO - TALK - TO - HARRY - POTTER -" <br>"THERE IS NO HARRY POTTER HERE! I DON'T KNOW WHAT SCHOOL YOU'RE TALKING ABOUT! NEVER CONTACT ME AGAIN! DON'T YOU COME NEAR MY FAMILY!" <br>~ Ron Weasley, Vernon Dursley; conversation on the telephone <br><br><br>"Don't let the Muggles get you down!" <br>~ Ron Weasley <br><br><br>"Harry - this is a Pocket Sneakoscope. If there's someone untrustworthy around, it's supposed to light up and spin. Bill says it's rubbish sold for wizard tourists and isn't reliable, because it kept lighting up at dinner last night. But he didn't realize Fred and George had put beetles in his soup." <br>~ Ron Weasley <br><br><br>Extremely unusual though he was, at that moment Harry Potter felt just like everyone else - glad, for the first time in his life, that it was his birthday. <br>~ Narrator <br><br>Testing, Testing 1 - 2 - 3 - 4 <br></div></div>
It still doesn't seem to save it with the new formatting, or even new text I attempt to enter. Is there anything I'm missing?

Note: I tried saving the text above to this text box, and it doesn't appear to work either. It appears to return it to the HTML format. Do I need to escape the ampersands?

Thank You.
Feb 9, 2015 at 1:36 PM
Well I tried to change the '&*' to '&amp*' and that didn't appear to work.
Feb 9, 2015 at 1:41 PM
Edited Feb 9, 2015 at 1:43 PM
Oops, it looks like a missed a semicolon, but when I changed it, it still didn't seem to work.

would it have anything to do with the <div> declaration?
Feb 9, 2015 at 1:44 PM
If that's the actual content of the field going to the server, the. It's not escaped. All HTML/XML characters need to be escaped prior to calling the update method. The prior post above has code on how to do that.

Paul
-- sent from mobile
Feb 9, 2015 at 1:48 PM
Edited Feb 9, 2015 at 1:55 PM
Paul, if you read the bottom, it is escaped. When I look at the preview for the message, it is escaped. It show the '"' as &quot; and the '<' as &lt;. Sorry.
Feb 9, 2015 at 1:54 PM
Edited Feb 9, 2015 at 1:56 PM
This is, after conversion to the HTML for this text editor, is how it looks when I send it to the server:
&lt;div class=&quot;ExternalClass5746AEBD20ED402FA1C4EC9B08D5E8DB&quot;&gt;
&lt;div&gt;&lt;strong&gt;Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of the year. For another, he really wanted to do his homework but was forced to do it in secret, in the dead of night. And he also happened to be a wizard. &lt;br&gt;~ Narrator &lt;br&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;Non-magic people (more commonly known as Muggles) were particularly afraid of magic in medieval times, but not very good at recognizing it. On the rare occasion that they did catch a real witch or wizard, burning had no effect whatsoever. The witch or wizard would perform a basic Flame Freezing Charm and then pretend to shriek with pain while enjoying a gentle, tickling sensation. Indeed, Wendelin the Weird enjoyed being burned so much that she allowed herself to be caught no less than forty-seven times in various disguises. &lt;br&gt;~ Bathilda Bagshot, A History of Magic &lt;br&gt;&lt;br&gt;&lt;br&gt;&quot;&lt;strong&gt;&lt;font style=&quot;font-size: 17px;&quot;&gt;HELLO? HELLO?&lt;/font&gt;&lt;/strong&gt; CAN YOU HEAR ME? I - WANT - TO - TALK - TO - HARRY - POTTER -&quot; &lt;br&gt;&quot;THERE IS NO HARRY POTTER HERE! I DON&#39;T KNOW WHAT SCHOOL YOU&#39;RE TALKING ABOUT! NEVER CONTACT ME AGAIN! DON&#39;T YOU COME NEAR MY FAMILY!&quot; &lt;br&gt;~ Ron Weasley, Vernon Dursley; conversation on the telephone
Note: I had to double escape the text to make it appear correct for this RTE.
Feb 9, 2015 at 2:36 PM
Do I need to worry about the "UNICODE byte order mark overriding conflicting codepage utf-8 from (META tag)" in the F12 developer console?
Feb 10, 2015 at 1:19 PM
I know the answer seems to be that I need to use the escapeHTML function to format my text into the XML version:
    function htmlEscape(str) {
        return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
    }
I have tried it and it looks like the code I use formats the data correctly:
//code from the function that sends the data to updateCandidate
candidateHash['candidateProtegeDescription'] = htmlEscape($("#candidateProtegeDescription").val());
where "#candidateProtegeDescription" is the textarea that uses the Rich Text Editor. The data gets formatted with all the five characters transformed. It still appears to get an "error" returned from the function to "UpdateListItems", when I check what gets returned from the Status of xData, the xData is:
xData.readyState = 4
xData.responseText = ""
xData.responseXML = null
xData.status = 400
xData.StatusText = "Bad Request"
This would appear to be the same error that Marc had said is shown by sending the wrong XML data to the SPServices function. I can't figure out what might be going wrong. Are there anymore debugging techniques I could try?

Thanks for all the help so far. I'm sure we can figure this out.
Feb 10, 2015 at 2:16 PM
I should simplify things a bit. You have one big update here and maybe the error is not with the RTE field but some other. Try with simple data first and ensure you can actually get an update through. Then, one by one, add more fields and complex data structures.

Is there a way you can post the entire XML message that is being created?

Paul
-- sent from mobile
Marked as answer by trout0525 on 2/10/2015 at 7:30 AM
Feb 10, 2015 at 2:55 PM
Edited Feb 10, 2015 at 4:58 PM
Hello Paul,

Thanks a lot. I forgot about the KISS factor (Keep It Simple Stupid). :o)

I updated only one field. It returned with a success. It did update the field. I'll add them one by one and see where it goes from there. Should I have trouble with just the RTE field(s), where do you think I should go? I'll post a full XML here soon too.

Thank You
Feb 10, 2015 at 3:29 PM
OK,

I'm hoping I'm not the only one, but I feel fairly stupid. I think others have things they have overlooked. All the fields were going fine, so I went and tried the textarea with the RTE, and it WORKED!!!! So, now I had to figure out what was going wrong still, because I'm that way. I found out that I had not htmlEscaped another textarea with a RTE.

I guess I was under the assumption that not EVERYTHING had to be XML formatted for it to be able to update just one field, or at least I thought I had things looking the way I thought would be okay. I guess I was wrong. Now that all my text areas with RTE are htmlEscaped, things seem to be good. Heck only took almost two days.

Thanks Paul, for reminding me of better debugging techniques. I was a little too anxious. And Thanks to the rest, Tabares and Marc. I'm not sure who to mark as the answer. Marc was first, then Tabares gave me the function, and finally Paul reminded me of better debugging. Can I mark them all? I'll try, otherwise, Marc gets the logical solution.
Feb 10, 2015 at 3:31 PM
​Cool. Glad you found the root cause... Yes, in XML, all values to all fields need to be escaped...​

_________
Paul T