简体   繁体   中英

jquery spservices update listitem

i'm trying to update a listitem using jquery spservices. everything is working but when i try to add a href to a richtextfield it doesnt work. it only updates plain text not the href. below is code, it's just a test so those urls are for testing.

function fn_UpdateListItem(){
$().SPServices({
operation: 'UpdateListItems',
listName: 'Bedrijven',
ID: 1,
valuepairs: [["Software", "<a href='http://www.google.nl'>its a test.</a>"]],
completefunc: function(xData, Status) {
alert('test complete');
}
});
}

if i change the valuepairs to

valuepairs: [[\"Software\", \"test\"]],

it works it puts test in the rich text field. but with href it doesnt work. anyone knows how to fix ? thanks in advane

I got the same problem for Sharepoint 2010, in this case the var dfNotes = CKEDITOR.instances.notes.getData(); didn't work for me, i found this:

https://msdn.microsoft.com/en-us/library/office/ee658527(v=office.14).aspx

var value = SP.Utilities.HttpUtility.htmlEncode(html); 
Edit

Tested on Sharepoint 2016 SharePoint On-Premises , It works too, so i it should work for SharePoint Online aswell !!

This is how it worked for me:

function AddListItem(html, list) { 
    var value = SP.Utilities.HttpUtility.htmlEncode(html);        
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
        batchCmd: "New",
        listName: list,
        valuepairs: [["Title", 'Title'], ["Content", value]],
        completefunc: function(xData, Status) {
            console.log(Status);
        }
    });

} 

Here is the actual solution. Prior to submitting to a SharePoint list, the HTML-ized data are stored in XML, which doesn't take kindly to embedded HTML tags, so they need to be escaped. Thanks to feedback on the SPServices forum, I was able to determine this was the case in my example above.

I revised my code to look like this:

var dfNotes = CKEDITOR.instances.notes.getData();

$().SPServices({
    operation: "UpdateListItems",
    async: false,
    batchCmd: "Update",
    listName: list,
    ID: prog,
    valuepairs: [["Notes", $("#notes").text(dfNotes).html()]],
    completefunc: function (xData, Status) {
        alert($("#notes").html());
    }
});

The first line references a rich text editor field that contains newly modified text. Note the slight difference in the valuepairs line where it now uses .text().html() to escape the text for transmission via XML.

I hope this helps someone!

You need encode the html code (replace the characters < and > for JavaScript: Escaping Special Characters, &lt; and &gt; ; this is an example of some characters ), this way you going to have a string available to save in the rich content text field (Notes), when The item is updated your data going to have a html code.

This is the code:

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;');
}

Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM