Showing posts with label SPServices.SPGetCurrentSite. Show all posts
Showing posts with label SPServices.SPGetCurrentSite. 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.

Build dynamic Url using the Current SharePoint Site Url Using Javascript, Content Editor WebPart and SPServices.

Build dynamic Url using the Current SharePoint Site Url Using Javascript, Content Editor WebPart and SPServices.

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

Step 2:This is an extension of my previous post.

Step 3:Cosider you need to have an hyperlink in the SharePoint page that uses the Current SharePoint Site URL, then it can be accomplished this way. 

In this code the link generated will be be to take user to a page within Pages directory of the SharePoint Site. If you append the Source attribute to the link then SharePoint knows how to bring them back.

This link = current Site URL + new page location + new page name + Source address + Querystring

<html>
<head>
<SCRIPT type=text/javascript src="/mysites/myDocLibrary/JsFiles/jquery-1.4.2.min.js"></SCRIPT>
<SCRIPT type=text/javascript src="/mysites/myDocLibrary/JsFiles/JsFiles/jquery.SPServices-0.5.6.min.js"></SCRIPT>
<SCRIPT type=text/javascript>


linkPage = function()
 {
     var thisSite = $().SPServices.SPGetCurrentSite();
     var linkToNewPage = thisSite + "/Pages/NewPage.aspx";
     var sourceURL = document.URL;
     var linkToNewPageFinal = linkToNewPage +"?Source="+ sourceURL +    "&MyQueryString=KeyWords";
      location.href = inkToNewPageFinal;
}

</SCRIPT>
</head>

<Body>

<a href ="javascript:linkPage()">Link to New Page</a>

</Body>
</html>

Step 4: 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 5: Clicking of "Link to New Page" will take to http://sample.com/sites/basicJs/Pages/NewPage.aspx?Source=http://sample.com/sites/basicJs/Default.aspx&MyQueryString=KeyWords

Step 6:Refet to my next post to extract the values of the QueryString.

Leave your comments below.

Get Current SharePoint site Url using SpServices in Content Editor Web Part

Get Current Url of the SharePoint Site using SpServices in 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: Within Content Editor Web Part insert this code


<html>
<head>
<SCRIPT type=text/javascript src="/mysites/myDocLibrary/JsFiles/jquery-1.4.2.min.js"></SCRIPT>
<SCRIPT type=text/javascript src="/mysites/myDocLibrary/JsFiles/JsFiles/jquery.SPServices-0.5.6.min.js"></SCRIPT>
<SCRIPT type=text/javascript>
      
            var thisSite = $().SPServices.SPGetCurrentSite();
           //alert(thisSite);
            document.write("The Current Site is " + thisSite);

</SCRIPT>
</head>

<Body>

</Body>
</html>



Step 3 : 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.