CAML Encode and Decode

url encode
Use the CAML encoder and decoder to encode or decode CAML while working with SharePoint Data Source Control queries. This script utilizes both HTML rendering to decode and a PHP function to encode. And, of course, I have included the simple script below for those that are curious.

show me!
<html>
<head>
 <title>devtrends.com CAML Encode and Decode</title>

 <STYLE>
 <!--
 .outbox {
 padding:10px;
 margin:4px;
 margin-bottom:15px;
 border:1px dashed #9E7760;
 background-color:#DBCABF;
 }
 -->
 </STYLE>

</head>

<body style="background-color:#FFFFFF;">

<p style="text-align:center;font-family:verdana;font-size:20px;">sharepoint caml encode and decode</p>

<center>
<?php

 if ($_POST['dir'] == "e") {
 ?><div><?php
 echo camlencode($_POST['caml']);
 ?></div><?php
 } elseif ($_POST['dir'] == "d") {
 ?><div><?php
 echo $_POST['caml'];
 ?></div><?php
 }

?>

 <form method="post" action="index.php">
 <input type="text" name="caml" size="50" />

 <select name="dir">
 <option value="e"<?php if($_POST['dir'] == "e"){echo "selected";} ?>>Encode</option>
 <option value="d"<?php if($_POST['dir'] == "d"){echo "selected";} ?>>Decode</option>
 </select>

 <input type="submit" value="process" />
 </form>
</center>

<p style="text-align:center;font-family:verdana;font-size:12px;">note: the decode utilizes standard HTML rendering and the encode will only change the following characters (", <, >).</p>

<p style="text-align:center;font-family:verdana;font-size:10px;"><a href="http://www.devtrends.com/">a simple service of www.devtrends.com</a></p>

</body>
</html>

<?php

function camlencode($mycamlstr) {

 $mycamlstr = str_replace("<", "&amp;lt;", $mycamlstr);
 $mycamlstr = str_replace(">", "&amp;gt;", $mycamlstr);
 $mycamlstr = str_replace("\"", "&amp;quot;", $mycamlstr);

 return $mycamlstr;
}

function camldecode($mycamlstr) {

 $mycamlstr = str_replace("&lt;", chr(60), $mycamlstr);
 $mycamlstr = str_replace("&gt;", chr(62), $mycamlstr);
 $mycamlstr = str_replace("&quot;", chr(34), $mycamlstr);

 return $mycamlstr;
}

?>

2 Comments

  1. Front End Dev in SharePoint Is...Draining said:

    Thank you, thank you! Not sure if I just wasn’t Googling for the right term or what, but I expected to find more of these quick CAML encode/decode tools online. Glad I finally found yours. This is much faster than the alternative ways that involve putzing around with SPD, etc.Thanks!

    August 25, 2012
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *