0
<h1 align="center">My List</h1>
<table id="maintable">
<tr>
<td>Name</td>
<td>
<input type="text" id="NameId"/>
</td>
<td>Age</td>
<td>
<input type="text" id="AgeId"/>
</td>
</tr>
<tr>
<td>DOB</td>
<td>
<input type="datetime" id="DOBId"/>
</td>
<td>Indian</td>
<td>
<input type="text" id="IndianId"/>
</td>
</tr>
<tr>
<td>Title</td>
<td>
<input type="text" id="TitleId"/>
</td>
<td>Admins</td>
<td>
<input type="text" id="AdminsId"/>
</td>
</tr>
<tr>
<td>Doc Name</td>
<td>
<input type="text" id="DocNameId"/>
</td>
<td>Doc Id</td>
<td>
<input type="text" id="DocId"/>
</td>
</tr>
</table>

Address
<textarea rows="4" cols="56" id="AddressId">
</textarea>






<script type="text/javascript" src="https://dept-dev.int.ally.com/sites/EA/Style%20Library/Scripts/Jquery/jquery.min.js" >
</script>  
<script type="text/javascript" src="/_layouts/15/SP.Runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/SP.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.init.js"></script>
<script language="ecmascript" type="text/ecmascript">  
var collListItem;  
$(document).ready(function () {
try
{
    getSetListItem();
}
catch(e)
{
    alert("error occured at ready method" + e.message);
}
}); 


function getSetListItem() {  
   //var empID = $("input[title='Name']").val(); 
   var  qString = window.location.href.split("?ID=")[1];
   var clientContext = new SP.ClientContext.get_current();  
   var oList = clientContext.get_web().get_lists().getByTitle('My list');  
   var camlQuery = new SP.CamlQuery();  
   camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ID\'/><Value Type=\'Numeric\'>'+ qString +'</Value></Eq></Where> </Query></View>');  
   collListItem = oList.getItems(camlQuery);  
   clientContext.load(collListItem);  
   clientContext.executeQueryAsync(Function.createDelegate(this, OnLoadSuccess),  
   Function.createDelegate(this, OnLoadFailed));  
}  

function OnLoadSuccess(sender, args) {  
   var listItemEnumerator = collListItem.getEnumerator();  
   while (listItemEnumerator.moveNext()) {  
      var oListItem = listItemEnumerator.get_current();  

document.getElementById('AgeId').value = (oListItem.get_item("Age"));

document.getElementById('NameId').value = (oListItem.get_item("Name"));

document.getElementById('DOBId').value = getDate(oListItem.get_item("DOB"));


document.getElementById('IndianId').value = (oListItem.get_item("Indian"));

document.getElementById('TitleId').value = (oListItem.get_item("Title"));

document.getElementById('AddressId').value = strip(oListItem.get_item("Address"));

document.getElementById('AdminsId').value = oListItem.get_item("Admins").get_lookupValue();

document.getElementById('DocNameId').value = oListItem.get_item("list_x0020_lookup").$2e_1;

document.getElementById('DocId').value = oListItem.get_item("list_x0020_lookup").$1E_1;

}
}
function strip(html)
{
   console.log(html);
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   console.log("text" + tmp.textContent);
   console.log("html" + tmp.innerHtml);
   return tmp.textContent || tmp.innerText || "";
}

function getDate(stringDate)
{
  var todayTime = new Date(stringDate);

   return todayTime.toLocaleDateString();


 //  var month = (todayTime .getMonth() + 1);
  // var day = (todayTime .getDate());
  // var year =(todayTime .getFullYear());
 // return month + "/" + day + "/" + year;
}


function OnLoadFailed(sender, args) {  
   alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
   }  



</script>  
  • 5
    You forgot to say please – Rob Windsor Apr 12 '16 at 11:50
  • 3
    The format of this question is not really in line with the communities policy. Rewriting code from one language to another is not really a problem per se but a work task – Robert Lindgren Apr 12 '16 at 12:17
  • more like a codereview thing when it is finished. – Raoul Mensink Apr 12 '16 at 12:49
  • running the code on console , it gives output as : ready state: 1 – user52650 Apr 15 '16 at 6:39
2

I have to say, I'm not a big fan of the do this for me questions, but this is a a good start to converting your JSOM to REST/AJAX. I haven't tested this, but this should be a good starting point for you.

    var  qString = window.location.href.split("?ID=")[1];
    var listName = "My list";
    var collListItem;
    $.ajax({
            url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$filter=ID eq " + qString,
            type: "GET",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(newItem),
            headers: { "Accept": "application/json;odata=verbose"},
            success: function(data){ 

                      var listItemEnumerator = data.d.results;  
   $.each(listItemEnumerator, function() {  
      var oListItem = $(this);  

document.getElementById('AgeId').value = oListItem.Age;

document.getElementById('NameId').value = oListItem.Name;

document.getElementById('DOBId').value = oListItem.DOB;


document.getElementById('IndianId').value = oListItem.Indian;

document.getElementById('TitleId').value = oListItem.Title;

document.getElementById('AddressId').value = strip(oListItem.Address);

document.getElementById('AdminsId').value = oListItem.Admins;

//document.getElementById('DocNameId').value = //oListItem.get_item("list_x0020_lookup").$2e_1;

//document.getElementById('DocId').value = //oListItem.get_item("list_x0020_lookup").$1E_1;

});
            },
            error:function (data) {
                  alert("Error: "+ data);
             }
        });
| improve this answer | |
  • o/p is coming as : {"d":{"results":[{"__metadata":{"id":"Web/Lists(guid'399ba06e-7d7a-4811-9d737f9313333aec')/Items(1)","uri":"deptdev.int.ally.com/sites/EA/_api/Web/…:"\"22\"","type":"SP.Data.Ally_x005f_1ListItem"},"Title":"Ally_1.1","Age":23},{"__metadata":{"id":"Web/Lists(guid'399ba06e-7d7a-4811-9d737f9313333aec')/Items(2)","uri":"deptdev.int.ally.com/sites/EA/_api/Web/…:"\"8\"","type":"SP.Data.Ally_x005f_1ListItem"},"Title":"Ally_1.2","Age":23}]}} – user52650 Apr 15 '16 at 10:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.