Tuesday, August 25, 2015

Send / Passing HTML Markup in SOAP XML as string

When we try to pass HTML markup as parameter string in SOAP XML, the SOAP request will get failed due to special characters / markups in the request XML.

I had faced this issue when I try to update SharePoint RichText list item using SharePoint web service post request.

Here's the code which was NOT working :

var soapEnv = "<?xml version=\'1.0\' encoding=\'utf-8\'?> \
<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' \
xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' \
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/\'> \
<soap:Body> \
<UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listName>MyListName</listName> \
<updates> \
<Batch OnError='Continue'> \
<Method ID='1' Cmd='Update'> \
<Field Name='ID'>1</Field> \
<Field Name='Desc'><div><strong>My  HTML markup content here  <br></strong></Field> \
</Method> \
</Batch> \
</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>";

if we post above SOAP xml , the webservice will throw error due to html markup in request XML.

To fix the above issue.

We can replace markup characters< with &lt;>with &gt; and & with &amp;.

         OR 

 We can use the easy way of using CDATA.

using CDATA , we can pass HTML markup placed inside SOAP xml request.

Here is the Working code:


  var soapEnv = "<?xml version=\'1.0\' encoding=\'utf-8\'?> \

<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' \
xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' \
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/\'> \
<soap:Body> \
<UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listName>MyListName</listName> \
<updates> \
<Batch OnError='Continue'> \
<Method ID='1' Cmd='Update'> \
<Field Name='ID'>1</Field> \
<Field Name='Desc'><![CDATA[<div><strong>My  HTML markup content here  <br></strong>]]></Field> \
</Method> \
</Batch> \
</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>";


No comments:

Post a Comment