Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Query Discussion Board Replies using SPServices\JQuery and Page Viewer Web Part

Query Discussion Board Replies using SPServices\JQuery and Page Viewer Web Part.


Step 1: Kindly follow my intital post to understand how java script can be used in Content Editor Web Part.

Step 2: Refer here to understand about SPServices. This is specialized Jquery to execute SharePoint related operations.

Step 3: In a Web Part Page, Edit Page and Add Page Viewer Web Part.
Step 4: Write the following code in a text. save as html. upload into SharePoint Document Library. Link this html page to page viewer web part.

Step 5: Create a Discussion Board. Add dropdown column called "Reply Priority". This column is hidden in Discussion[Question] content type. In our case, Column "Reply Priority" is visible only to Message[Reply] content.




Step 6: We are trying to accomplish the following here. Display Replies from all questions and load a single table with a chosen Priority Level.





Consider there are 2 questions, Question 1, has one reply with Priority Level = Low, and Question 2 has one reply with Priority Level = Low. Now we need to extract both of these replies and display in a table when Priority Level = Low is chosen.


When Priority Level = Medium or High then delete these rows and load the table with replies with respective Priority Level.

<head>
<SCRIPT type=text/javascript src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></SCRIPT>
<SCRIPT type=text/javascript src="http://sample.com/sites/lib/jquery.SPServices-0.7.2.min.js"></SCRIPT>

<title>Filter Replies </title>
</head>

<body>

<table id="BasicTable" border=0 width="55%" align="left"
style="font-family: Verdana;font-size:8pt" >
<tr>
<td style="width:40%;"><B>Reply type :</B></td>
<td style="width:60%;" colspan="2">
<select id="DropStudies" onchange="getComboA(this)"
style="font-family: Verdana;font-size:8pt">
                                    <option value="Low">Low</option>
                                    <option value="Medium">Medium</option>
                                    <option value="High">High</option>
</select>
</td>
</tr>
</table>          
<br/><br/>

<table id="myDataTable" border=1 width="55%" align="left" style="font-family: Verdana;font-size:8pt;">
<tr align='left' bgcolor="#00579c">
            <td><B>ID</B></td>
            <td style="width:40%;"><FONT COLOR="white">
<B>Main Question</B></td>
            <td style="width:60%;"><FONT COLOR="white">
<b><B>Reply Details</B></td>
</tr>
</table>          

<SCRIPT type=text/javascript>

get() ;  

function getComboA(sel)
{
    var value = sel.options[sel.selectedIndex].value;
    LoadTableFunction();
}

function LoadTableFunction()
{                     
    var table = document.getElementById("myJsonDataTable");                 
    if(table.rows.length >1)
    {
            for(var i = table.rows.length - 1; i > 0; i--)
            {    table.deleteRow(i);}
    }
    get() ;                      
}

function get()
{                     
      var method = "GetListItems";                       
      var webURL =  $().SPServices.SPGetCurrentSite() ;               
      var list = "DBExample";                   
      var fieldsToRead = "<ViewFields>" +"<FieldRef Name='Name' />"
+"</ViewFields>";
                       
      //----------------------------------------------------------------------------
     // GET ALL REPLIES WITH CHOSEN REPLY PRIORITY
     // CONTENT TYPE = MESSAGE RETURNS ONLY REPLIES
     // CONTENT TYPE = DISCUSSION WILL RETURN THE QUESTIONS.
     // THAT IS NOT NEEDED HERE.
     // RESURCIVEALL QUERY OPTIONS
     //------------------------------------------------------------------------------
  
    var query = "<Query><Where><And><Eq><FieldRef Name='ContentType' />
<Value Type='Text'>Message</Value></Eq>";               
    query = query +"<Eq><FieldRef Name='Reply_x0020_Priority' />
                        <Value Type='Choice'>" + DropStudies.value +
"</Value></Eq></And></Where>";
     query = query + "<OrderBy><FieldRef Name='ID' Ascending='True' />
  </OrderBy></Query>";
                                               
     var qOptions = "<QueryOptions><ViewAttributes Scope='RecursiveAll'/>
</QueryOptions>";
                                               
     GetData(method,list,webURL,fieldsToRead,query,qOptions,list);            
};
           

function GetData(method,list,webURL,fieldsToRead,query,qOptions,Folder)
{                     
$().SPServices
({
    operation: method,
    async: false, 
    webURL: webURL,
    listName: list,
    CAMLViewFields: "<ViewFields Properties='True' />",
    CAMLQuery: query,
    CAMLQueryOptions: qOptions,                                           
    completefunc: function (xData, Status)
    {
       $(xData.responseXML).SPFilterNode("z:row").each(function() 
       {
       // GET ID OF THE REPLY                                                     
      //--------------------------------------------
      var ID = $(this).attr("ows_ID");                                                                                                                               
                                                                       
      // QUESTION IS  n[5].toString()
     // LINK TO THE QUESTION IS IN var mainQuestion
    //-----------------------------------------------------------
    var fileDirRef  =  $(this).attr("ows_FileDirRef");
    var n= fileDirRef.split("/");
    var mainQuestion = "http://sample.com/sites/" + n[1].toString() + "/" +
 n[2].toString() +"/"+ n[3].toString()  +
"/"+ n[4].toString() + "/"+ n[5].toString();
    mainQuestion = mainQuestion.replace(/[" "]/g, "%20");
                                                           
   // REMOVE THE REPLY FROM THE OTHER DETAILS IN BODY
   //----------------------------------------------------------------------
    var QuestionDetail = $(this).attr("ows_Body");                                                
    var q = QuestionDetail.split("From");
    var q1 = q[0].toString();
  // REMOVE THE HORIZONTAL LINE
  //-----------------------------------------------
    var q2 = q1.split("<hr class=ms-disc-quotedtext>");                                        
                                   
// REPLY IS  q2[0].toString()
// LINK TO THE QUESTION IS IN var directLink
//--------------------------------------------
var directLink = "http:/sample.com/sites/" + n[1].toString() + "/" +
 n[2].toString() +"/"+ n[3].toString()  +
"/"+ n[4].toString() + "/DispForm.aspx?ID=" +  ID.toString();
directLink = directLink.replace(/[" "]/g, "%20") + "&Source=" + mainQuestion;
var reply = q2[0].toString() + "<a href='" + directLink + "'target=_blank>Link to Reply</a>";                                            
                                                                                                                       
$("#myDataTable").append("<tr align='left'>" +
            "<td align='left' valign='top'>"+ID+"</td>" +
             "<td align='left' valign='top'>
<a href='" + mainQuestion + "'target=_blank>"
+ n[5].toString() +"</a></td>" +                                                                      
            "<td align='left' valign='top'>"+reply+"</td>" +                                                              
            "</tr>");          
      });
  }
  });
}

</script>
</body>
</html>
 


 
Step 7: To remove the identity that the code was generated from Page Viewer Web Part, within the Web Part Setting, Under Appearance, For Chrome Type select "None". Click OK. Click Apply. Publish the Page.

Step 8: Refer this post to get Items from SharePoint List and display in SharePoint using Content Editor Web Part and HTML table
.

Leave your comments below.

Get JSON Web Service Data using Content Editor Web Part and Insert that data into SharePoint List.

Get JSON Web Service Data using Content Editor Web Part and Insert that data into SharePoint List.

Step 1: Kindly follow my intital post to understand how java script can be used in Content Editor Web Part.

Step 2: Refer here to understand more about JSON Web Service.

Step 3: Refer here to understand about SPServices. This is specialized Jquery to execute SharePoint related operations.

Step 4: In a Web Part Page, Edit Page and Add Content Editor Web Part.

Step 5: Write the following code in a text. save as html. upload into SharePoint Document Library. Link this html page to content editor web part or Write code in Web Part's text editor directly.


Step 6: We are trying to accomplish the following here. Using a button "Get JSON Data we will retrieve the JSON data and updata a HTML table. Now another button "Update into SharePoint List" enables you to update the retrieved data into a SharePoint List.

Initially the button to retrieve the JSON data is displayed from the Content Editor Web Part.


When clicked, it retrieves the data and populates a HTML table.Now the button to Update this data into a SharePoint list is also made visible.

When "Update to SharePoint List" is clicked, the first record is displayed. Followed by a message which confirms that this record has been inserted into a SharePoint list.





Alert messages will repeat for all 5 records. Then go to the SharePoint list to confirm if the list has been updated.




The code is as below.

<HEAD>

<TITLE>GetData JSON web service </TITLE>

<script type="text/javascript" src="http://MyJSSite.com/sites/BasicJS/Lib/jquery-1.3.2.min.js"></script>
<script type=text/javascript src="http://MyJSSite.com/sites/BasicJS/Lib/jquery.SPServices-0.5.6.min.js"></SCRIPT>



<SCRIPT type=text/javascript>         
 var JsonData = "http://test.basic.com//%8766987%7D/%7BA561528C?format=daas-JSON";
 function JSONFunction()
 {
  var fullurl = JsonData + '&callback=?';
  alert("The JSON URL IS :: " + fullurl);

   $.getJSON(fullurl,'',function(data)
   {
   //alert("Inside function-data");
   var receivedData = data.entries;
   alert("Length is :: " + receivedData.length.toString());
   for (var i = 0; i < receivedData.length; i++)
   {
    var linktitle = receivedData[i]['LinkTitle'].toString();
    var type  = receivedData[i]['Activity_Type'].toString();
    var edate = receivedData[i]['End_Date'].toString();
    AddRowToTable(i.toString(),linktitle,type,edate);
   }
   document.getElementById('myJsonDataTable').style.visibility = "visible";
   document.getElementById('SpInsertButton').style.visibility = "visible";
  
   });
 }
  
 function AddRowToTable(num,linktitle,type,edate)
 {
     $("#myJsonDataTable").append("<tr align='middle'>" +
    "<td align='left'>"+num+"</td>" +
                                "<td align='left'>"+linktitle+"</td>" +
     "<td align='left'>"+type+"</td>" +
    "<td align='left'>"+edate+"</td>" +     "</tr>");
                               
 }

 function UpdateSharepointFunction()
 {
  var fullurl = JsonData + '&callback=?';
   $.getJSON(fullurl,'',function(data)
   {
   var receivedData = data.entries;
   for (var i = 0; i < receivedData.length; i++)
   {
    var linktitle = receivedData[i]['LinkTitle'].toString();
    var type  = receivedData[i]['Activity_Type'].toString();
    var edate = receivedData[i]['End_Date'].toString();
    CreateNewItem(linktitle,type,edate);
   }

   });
 }

 function CreateNewItem(linktitle,type,edate)
 {
     alert( linktitle + "----" + type + "---- " + edate);
     $().SPServices({
         operation: "UpdateListItems",
         async: false,
         batchCmd: "New",
         listName: "FromWebService",
         valuepairs: [["Title", linktitle], ["PType", type], ["EndDate", edate]],
         completefunc: function(xData, Status) {
         alert("completed");
         }
     });   
 }


</SCRIPT>
</head>

<BODY>

<button onclick="JSONFunction()">Get JSON data</button>

<table id="myJsonDataTable" border =1 width="70%" style="visibility:hidden">
<tr align='left' bgcolor="lightBlue">
 <td><B>Num</B></td>
 <td><B>Title</B></td>
 <td><B>Type</B></td>
 <td><B>End Date</B></td>
</tr>
</table>

<button id="SpInsertButton" onclick="UpdateSharepointFunction()" style="visibility:hidden">Update into SharePoint List</button>

</BODY>
</HTML>
 
Step 7: While inserting into SharePoint custom list, this code again retrieves data from JSON to use the latest data.

Step 8: To remove the identity that the code was generated from Content Editor Web Part, within the Web Part Setting, Under Appearance, For Chrome Type select "None". Click OK. Click Apply. Publish the Page.

Step 9: Refer this post to see the data extracted from GeoPlugin JSON Web Service using a Content Editor Web Part.

Leave your comments below.

Get Geoplugin JSON Web Service Data in SharePoint Page using Content Editor Web Part


Get Geoplugin JSON Web Service Data in SharePoint Page using Content Editor Web Part

Step 1: Kindly follow my intital post to understand how java script can be used in Content Editor Web Part.

Step 2: GeoPlugin is a readily available JSON Web Service. Refer here to understand more about it.

Step 3: In a Web Part Page, Edit Page and Add Content Editor Web Part.

Step 4: Write the following code in a text. save as html. upload into SharePoint.Link this html page to content editor web part or Write code in Web Part's text editor directly.


<HEAD>
<TITLE>GetData JSON web service - Geoplugin</TITLE>

<script type="text/javascript" src="http://myserver.com/myData /MyScripts/jquery-1.3.2.min.js"></script>
<SCRIPT type=text/javascript>
      
    var JsonData = "http://www.geoplugin.net/json.gp?ip=66.249.75.200&jsoncallback=?";
    $(document).ready(function()
    {
           var url = JsonData;                          
           $.ajax({
                     type: 'GET',
                     url: JsonData,
                     async: false,                                     
                     data: { get_param: 'value' },                                       
                     dataType: 'json',
                      success: function(data)
                      {                                                                           
                              for (var i in data)
                              {     $("#divId1").append("<li> " +   i + "------" + data[i] +  "</li>");     }
                      },
                      error: function(e)
                      {
                             alert(e.message);
                      }
           });                          
 });         

</SCRIPT>
</head> 
<BODY>      <div id="divId1"/>    </div>   </BODY>
</HTML>

Step 6: To remove the identity that the code was generated from Content Editor Web Part, within the Web Part Setting, Under Appearance, For Chrome Type select "None". Click OK. Click Apply. Publish the Page.


Step 8: Now you can see the data extracted from GeoPlugin in a Content Editor Web Part.

Step 9: Refer this post to see how data from JSON can be inserted into SharePoint list using content editor web part.

Leave your comments below.