Thursday, 1 February 2018

Get Sharepoint List Data Using LookUp by REST API

<html>
<head>

<script src="http://spweb.ccs.utc.com/sites/innvoationsite/SiteAssets/jquery-3.1.1.min.js"></script>
<script>
var productid;
$(document).ready(function () {
 productid = getUrlParameter('ProductNo');
 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getListItem);

 });
 var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};
 function getListItem()
 {

  $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('TestLibrary')/items?$select=Title,ID,Product/ID,File/ServerRelativeUrl,File/Name&$expand=File,Product&$filter=Product/ID eq "+productid,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {

var Result = data.d.results ;
console.log(Result);
for(var i=0;i<Result.length;i++)
{
var fileurl=Result[i].File.ServerRelativeUrl;
var filename=Result[i].File.Name;
var fileid=Result[i].ID;
var fullurl=window.location.protocol + '//' + window.location.host + Result[0].File.ServerRelativeUrl;
if(i==0)
{
 $('#divattachments').append("<a id='"+fileid+"' class='tablinks' onclick='test(this)' name='" + window.location.protocol + '//' + window.location.host + fileurl+"'>"+filename +"</a>");
 document.getElementById(fileid).className += " active";

 $('#container').append("<object id='obj' data='"+fullurl+"' >object can't be rendered</object>");
 $('#container').addClass('pdfcontainer')

 $('#divaddcomment').append("<a onclick='addcomment("+fileid+")'>Add Comment</a>");
     $('#divviewcomment').append("<a onclick='viewcomment("+fileid+")'>View Comment</a>");
}
else
{
 $('#divattachments').append("<a id='"+fileid+"' class='tablinks' onclick='test(this)' name='" + window.location.protocol + '//' + window.location.host + fileurl+"'>"+filename +"</a>");
}
}

},

     error: function(err){
alert("Request failed :"+JSON.stringify(err));
       }
 });

 }
 function addcomment(obj)
 {
SP.UI.ModalDialog.ShowPopupDialog('/sites/innovationSite/TestLibrary/Forms/AddComment.aspx?ID='+obj); return false;
 }
 function viewcomment(obj)
 {
SP.UI.ModalDialog.ShowPopupDialog('/sites/innovationSite/TestLibrary/Forms/ViewComment.aspx?ID='+obj); return false;
 }
 function test(obj)
{
     var i, tablinks;
     var docurl = obj.name;
var docid = obj.id;
 // var div1 = document.getElementById("obj");
 // div1.setAttribute("data", docurl);

     $('#container').empty();
     $('#container').append("<object id='obj' data='"+docurl+"' >object can't be rendered</object>");

     tablinks = document.getElementsByClassName("tablinks");
     for (i = 0; i < tablinks.length; i++)
{
        tablinks[i].className = tablinks[i].className.replace(" active", "");
}
    obj.className += " active";
$('#divaddcomment').empty();
$('#divviewcomment').empty();
$('#divaddcomment').append("<a onclick='addcomment("+docid+")'>Add Comment</a>");
$('#divviewcomment').append("<a onclick='viewcomment("+docid+")'>View Comment</a>");
}

</script>

 <style>
.tab {
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab a {
    background-color: inherit;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of buttons on hover */
.tab a:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
.tab a.active {
    background-color: #ccc;
}
 .pdfcontainer { overflow: auto; -webkit-overflow-scrolling: touch; height: 500px; }
  object { width: 100%; height: 1000px }
 
 .linkstyle{
margin:7px 2px 1px;
padding:5px;
display: inline-block;
 }


</style>
<title>AddNewProject</title></head>
<body>
<div id="divattachments" class="tab">
</div>
<div id="divaddcomment" class='linkstyle'>
</div>
<div id="divviewcomment" class='linkstyle'>
</div>
<div id="container">
      <!-- <object id="obj" data="https://o365.sharepoint.com/sites/test/testdocs/test.pdf" >object can't be rendered</object> -->
  </div>
</body>
</html>

No comments:

Post a Comment