May be an old thread, but it has to do with ampersands.

Apr 29, 2015 at 1:40 PM
Hello guys,

I know you don't like it Marc, but there is an issue I'm having with reading and writing a routine that handles a list item that has an ampersand in it. The data is "Creativity & Innovation" I know the easy solution would be to just have the user change the ampersand to an 'and', but I want them to be happy. I have tried to do it with the simple 'UpdateListItems' without changing the ampersand character, i.e. 'xmlencode'ing it. That just kept the ampersand as an ampersand in the List. So I tried to use the xmlencode to encode the ampersand, but it still remained an ampersand. I tried to xmlencode the code once again to get a &_a_m_p_;a_m_p;, yet the list version of it was still the &. I had to do it three times to get the item to show up as &_a_m_p_; in the list.

Now why do I need it to be the &_a_m_p_; value? Only because the routine I'm using has a list of connected jqueryui sortables, that I'm initiating a list with all the entries in one of the lists, and I need it to remove the entry with the ampersand when it has been stored in another list. It does not seem to work when I use just the ampersand in the object definition I use to manage all the data.

Can you offer any advice, or should I just stick with the solution I found?

(Note: I use the &_a_m_p_; representation to make the viewable version show up in the HTML code here. It seems to translate a & to just an &.)
Coordinator
Apr 29, 2015 at 1:47 PM
I don't like it when you have trouble, not when you ask for help. ;+)

Can you show the code you are using to do the encoding?

M.
Apr 29, 2015 at 2:05 PM
Hello Marc,

Sorry for not including any code. You may need more that this, but here is what I have written so far to encode the data before I submit it:
// handle Competency List Entries
for (i = 1; i < 6; i++) {
    var pushNamePrefix = "competencyList" + i;
//        console.log("pushNamePrefix: " + pushNamePrefix);
    for (j=1; j < 8; j++) {
        var pushName = pushNamePrefix + "Entry"+ j;
        if (ListReturnHash[pushNamePrefix][j-1] && ListReturnHash[pushNamePrefix][j-1] != null) {
            var xmlEncodedName = xmlencode(ListReturnHash[pushNamePrefix][j-1]);
            var xmlEncodedName = xmlencode(xmlEncodedName);
            var xmlRencodedName = xmlencode(xmlEncodedName); //needed to handle SharePoint list storing procedure's decoding of encoded characters
            console.log("ListReturnHash[" + pushNamePrefix + "][" + (j-1) + "]: " + xmlRencodedName);
            valuePairs.push([pushName, xmlEncodedName]);
        } else {
            valuePairs.push([pushName, ""]);
        }
    }
}

Thanks for all your help.
Apr 29, 2015 at 2:07 PM
You may want this as well:
function xmlencode(string) {
return string.replace(/\&/g,'&'+'amp;').replace(/</g,'&'+'lt;').replace(/>/g,'&'+'gt;').replace(/\'/g,'&'+'apos;').replace(/\"/g,'&'+'quot;');
}
Coordinator
Apr 29, 2015 at 2:10 PM
Here's the function I use internally to SPServices, which as you can see, I borrowed from dracoblue long ago:
/* Taken from http://dracoblue.net/dev/encodedecode-special-xml-characters-in-javascript/155/ */
    var xml_special_to_escaped_one_map = {
        '&': '&amp;',
        '"': '&quot;',
        '<': '&lt;',
        '>': '&gt;'
    };
    var escaped_one_to_xml_special_map = {
        '&amp;': '&',
        '&quot;': '"',
        '&lt;': '<',
        '&gt;': '>'
    };

    function encodeXml(string) {
        return string.replace(/([\&"<>])/g, function (str, item) {
            return xml_special_to_escaped_one_map[item];
        });
    }

    function decodeXml(string) {
        return string.replace(/(&quot;|&lt;|&gt;|&amp;)/g,
            function (str, item) {
                return escaped_one_to_xml_special_map[item];
            });
    }

/* Taken from http://dracoblue.net/dev/encodedecode-special-xml-characters-in-javascript/155/ */
Apr 29, 2015 at 2:13 PM
I'll see if the dracoblue object works any better for encoding.

Thank you for the response. I'll let you know how it goes.
Apr 29, 2015 at 2:23 PM
Edited Apr 29, 2015 at 2:24 PM
Hey Marc,

I did just notice an error in my code above, that I didn't exactly need to do it three times. Only twice. :o) This line:
valuePairs.push([pushName, xmlEncodedName]);
need to be
valuePairs.push([pushName, xmlRencodedName]);
To have worked the first time through, but I'm still giving the dracoblue code a try.
Coordinator
Apr 29, 2015 at 2:27 PM
Your code is a little too circuitous for me to decipher, but you should only escape once.

M.
Apr 29, 2015 at 2:42 PM
Hello Marc,

No such luck señor. It still needs to go through twice. Here's my two functions for storing and setting things up:
    function setupValuePairs() {
            // setup data valuePairs to enter into list
            var userInfoHash = getUserFormInfo();
            var ListReturnHash = readFormData();

            // format time for proper input to SharePoint list
            var userInfoHashDate = userInfoHash['inputDate'].replace(/ /g, '');  // strip spaces that may enter date string
            var inputDate = userInfoHashDate + "T00:00:00";
            var ISOInputDate = new Date(inputDate);
            var ISODate = $().SPServices.SPConvertDateToISO({dateToConvert:new Date(ISOInputDate)});

            var valuePairs = [["Title", userInfoHash['userID']],
                                              ["LastName", userInfoHash['userInfoLastName']],
                                              ["FirstName", userInfoHash['userInfoFirstName']],
                                              ["MiddleInitial", userInfoHash['userInfoMiddleInitial']],
                                              ["Date", ISODate],
                                              ["GradeRank", userInfoHash['userInfoGradeRank']],
                                              ["DutySection", userInfoHash['userInfoDepartment']],
                                              ["SupervisorName", userInfoHash['userInfoManager']],
                                              ["SupervisionLength", userInfoHash['userInfoSupervisionLength']],
                                              ["InteractionFrequency", userInfoHash['userInfoInteractionFrequency']],
                                              ["InteractionFrequencyType", userInfoHash['userInfoInteractionFrequencyType']]];

            // handle Competency List Entries
            for (i = 1; i < 6; i++) {
                    var pushNamePrefix = "competencyList" + i;
                    for (j=1; j < 8; j++) {
                            var pushName = pushNamePrefix + "Entry"+ j;
                            if (ListReturnHash[pushNamePrefix][j-1] && ListReturnHash[pushNamePrefix][j-1] != null) {
    //                              var xmlEncodedName = xmlencode(ListReturnHash[pushNamePrefix][j-1]);
    //                              var xmlEncodedName = xmlencode(xmlEncodedName);
    //                              var xmlRencodedName = xmlencode(xmlEncodedName); //needed to handle SharePoint list storing procedure's decoding of encoded characters
                                    var xmlEncodedName = encodeXml(ListReturnHash[pushNamePrefix][j-1]);
                                    console.log("ListReturnHash[" + pushNamePrefix + "][" + (j-1) + "]: " + xmlEncodedName);
                                    valuePairs.push([pushName, xmlEncodedName]);
                            } else {
                                    valuePairs.push([pushName, ""]);
                            }
                    }
            }

            // handle Competency Index List Entries
            for (i = 1; i < 6; i++) {
                    var pushNamePrefix = "competencyList" + i;
                    var hashIndexName = "competencyIndexList"+ i;
                    for (j=1; j < 8; j++) {
                            var pushName = pushNamePrefix + "IndexNum"+ j;
                            if (ListReturnHash[hashIndexName][j-1] && ListReturnHash[hashIndexName][j-1] != null) {
                                    valuePairs.push([pushName, ListReturnHash[hashIndexName][j-1]]);
                            } else {
                                    valuePairs.push([pushName, ""]);
                            }
                    }
            }

            return(valuePairs);
    }
and
function storeInput(){

    // check if user has already entered information.
    var user = getUserAccountInfo();
    var AccountName = user.AccountName;
    var foundID = findSharePointListID(AccountName);
    var valuePairs = setupValuePairs();

    if (foundID) { // Update user info
        updateUserData (foundID, valuePairs);
    } else { // New Entry
        $().SPServices({
            operation: "UpdateListItems",
            async: false,
            batchCmd: "New",
            listName: "CompetenciesBillet",
            valuepairs: valuePairs,
            completefunc: function (xData, Status) {
                console.log("Status: " + Status);
                if (Status == "error") {
                    console.log ("Unable to communicate with Sharepoint Server.  Please try again at a later time.");
                    return;
                } else {
                    alert("User Information Saved!");
                }
            }
        });
    }

    return;
}

function updateUserData (foundID, valuePairs) {
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
        batchCmd: "Update",
        listName: "CompetenciesBillet",
        ID: foundID,
        valuepairs: valuePairs,
        completefunc: function (xData, Status) {
            console.log("Status: " + Status);
            var out = $().SPServices.SPDebugXMLHttpResult({
                node: xData.responseXML,
                outputId: messageArea
            });

//            $("#messageArea").html("").append("<b>This is the output from the UpdateListItems operation:</b>" + out);
//            $("#messageArea").append("<b>Refresh to see the change in the list above.</b>");

            if (Status == "Error") {
                console.log ("Unable to communicate with Sharepoint Server.  Please try again at a later time.");
            } else {
//                alert("User Information Saved!");
            }
        }
    });
}
Coordinator
Apr 29, 2015 at 5:03 PM
It's sort of hard to see what's going on there.

You only need to encode values once. I'd suggest that you look at the POST data and see if the encoding is actually happening the way you think it is going in.

M.
Apr 29, 2015 at 5:51 PM
Hello Marc,

I have looked at the console.log output for the data it tries to store, and it shows it properly encoded as &amp; Yet, when I go and look at it in the SharePoint list it is only displayed as an &. Note, if you don't remember, I'm still stuck on SP 2007.

Note, when I write my message here in the CodePlex page, I have to type it twice, as in &amp;amp, to simple see &amp;. That is if you're seeing what I am seeing in my preview and in my IE window after I post..)
Coordinator
Apr 29, 2015 at 5:57 PM
When it's stored in the list, it should be just an ampersand, though, right?

In other words, "Creativity & Innovation" should be stored as "Creativity & Innovation".

M.
Jul 21, 2015 at 3:02 PM
Hello Marc,

Sorry to get back to you so late on this. I guess I got it to work for me, but it still has to store it in the list as '&amp;' Things appear to be working with my code, though. I've got another issue I came here for. I'll post it soon.

Thank you for all your help.
Coordinator
Jul 21, 2015 at 3:05 PM
If everything is working correctly, you'd send in
"Creativity &amp; Innovation"
and what is stored in the list is
"Creativity & Innovation"
M.