Showing posts with label REST API. Show all posts
Showing posts with label REST API. Show all posts

Saturday 18 June 2016

Retrieve Video Portal, Channels And Videos From Office 365 Using REST Search And List API


Retrieve Video Portal Detail:

First, we will see how we can retrieve the video portal information from Office 365 using SharePoint 2013 REST Search API.

The video portal is a part of the Point Publishing hub site. The site template used to create the portal is POINTPUBLISHINGHUB. The REST search API can be used along with the Web template ID to get the relevant information. Web template property should be used in query parameter to get the channels using search. Template ID should be passed as the Web template value.

The search API will be,
  • https://siteurl/_api/search/query?QueryText='WebTemplate:POINTPUBLISHINGHUB'

The following code snippet shows the JQuery Ajax call to get the video portal information from Office 365 site, using search API:
  1. $.ajax
  2. ({  
  3. "http://siteurl/_api/search/query?QueryText='WebTemplate:POINTPUBLISHINGHUB'",  
  4.     type: "GET",  
  5.     async: false,  
  6.     headers: { "accept""application/json;odata=verbose" },  
  7.     success: function(data){  
  8.         var portalInfo = data.d.query.PrimaryQueryResult.RelevantResults;  
  9.           
  10.             var portalResults = portalInfo.Table.Rows.results;  
  11.         for(var i = 0; i< portalResults.length; i++){  
  12.             console.log("Title : "+portalResults[i].Cells.results[3].Value);  
  13.             console.log("Created By : "+portalResults[i].Cells.results[4].Value);  
  14.             console.log("portal URL : "+portalResults[i].Cells.results[6].Value);  
  15.         }  
  16.         //Similarly other results can be retrieved and displayed  
  17.     },  
  18.     error: function(data){  
  19.     }  
  20. });  

Retrieve Channels:

The channels are separate site collections present on the portal. The site template used to create a channel (site collection) on the video portal is POINTPUBLISHINGTOPIC. Hence, we can use the template ID to query the portal to get the channels available. 

The search API will be,
  • https://siteurl/_api/search/query?QueryText='WebTemplate:POINTPUBLISHINGTOPIC'
Some of the parameters that can be retrieved using the above URL are Channel Name, Created by, Channel URL, etc.

The following code snippet shows the JQuery Ajax call to get the video channels:
  1. $.ajax
  2. ({  
  3.     url: "http://siteurl/_api/search/query?QueryText='WebTemplate:POINTPUBLISHINGTOPIC'",  
  4.     type: "GET",  
  5.     async: false,  
  6.     headers: { "accept""application/json;odata=verbose" },  
  7.     success: function(data){  
  8.         var channelInfo = data.d.query.PrimaryQueryResult.RelevantResults;  
  9.           
  10.         console.log("Available Channels Count : " + channelInfo.RowCount);  
  11.         var channelResults = channelInfo.Table.Rows.results;  
  12.         for(var i = 0; i< channelResults.length; i++){  
  13.             console.log("Title : "+channelResults[i].Cells.results[3].Value);  
  14.             console.log("Created By : "+channelResults[i].Cells.results[4].Value);  
  15.             console.log("Channel URL : "+channelResults[i].Cells.results[6].Value);  
  16.         }  
  17.         //Similarly other results can be retrieved and displayed  
  18.     },  
  19.     error: function(data){  
  20.     }  
  21. });  

Retrieve Videos:

Next, we will see how we can retrieve the videos from the portal in different ways.

1. Using List REST API: From the channels identified, we will access the videos from the respective channels. REST API is used to query SharePoint list items that will be used. To access a video from one single channel, find the following REST List API URL, given below:
  • https://channelurl/_api/web/lists/GetByTitle('Videos')/Items
Channel URL is the channel path name, found from the previous query.

To select necessary properties for the videos, use select parameter and specify the properties. Some of the properties displayed in my example are Title, Author Name, and Video URL. Here, to get the author name, an expanded query needs to be used.

The following code snippet shows the Jquery Ajax call to get the videos present on the specified channel, using List API:
  1. $.ajax
  2. ({  
  3.     url: "http://videoportal_url/_api/web/lists/GetByTitle('Videos')/Items?$select=Title,FileRef,Author/Title&$expand=Author",  
  4.     type: "GET",  
  5.     async: false,  
  6.     headers: { "accept""application/json;odata=verbose" },  
  7.     success: function(data){  
  8.         if(data.d != null && data.d.results.length > 0){  
  9.             var videoResults = data.d.results;  
  10.               
  11.             console.log("Available Videos Count : " + videoResults.length);  
  12.         for(var i = 0; i< videoResults.length; i++){  
  13.             console.log("Title : "+videoResults[i].Title);  
  14.             console.log("Created By : "+videoResults[i].Author.Title);  
  15.             console.log("Video URL : "+videoResults[i].FileRef);  
  16.         }  
  17.     }  
  18.     //Similarly other results can be retrieved and displayed  
  19.     },  
  20.     error: function(data){  
  21.     }  
  22. });   

2. Using Search REST API Query: In the section above, we have seen how we can retrieve the videos from the channels using List REST API. Now, we will see how we can retrieve the videos (same set of information) from the channel using search REST API.

The content type used for video is cloud video. Navigate to SharePoint Video library from channel site and find out the content, type of list, cloud video, and content type. The search API will be,
  • https://siteurl/_api/search/query?QueryText=%27contenttypeid:list_cloudvideo_contenttypeid%27 
The following code snippet shows the Jquery Ajax call to get the videos, present on the specified channel, using the list video content type.
  1. $.ajax
  2. ({  
  3.     url: "http://siteurl/_api/search/query?QueryText='list_cloudvideo_contenttypeid'",  
  4.     type: "GET",  
  5.     async: false,  
  6.     headers: { "accept""application/json;odata=verbose" },  
  7.     success: function(data){  
  8.         var videosInfo = data.d.query.PrimaryQueryResult.RelevantResults;  
  9.           
  10.         console.log("Available Videos Count on Channel : " + videosInfo.RowCount);  
  11.     var videoResults = videosInfo.Table.Rows.results;  
  12.     for(var i = 0; i< videoResults.length; i++){  
  13.         console.log("Title : "+videoResults[i].Cells.results[3].Value);  
  14.         console.log("Created By : "+videoResults[i].Cells.results[4].Value);  
  15.         console.log("Video URL : "+videoResults[i].Cells.results[6].Value);  
  16.     }  
  17.     //Similarly other results can be retrieved and displayed  
  18.     },  
  19.     error: function(data){  
  20.     }  
  21. });   

Monday 13 June 2016

Retrieve Delve Boards And Their Documents From Office 365 Using REST API


In this post, you will learn how to retrieve boards and documents (added to board) from Office 365 Delve, using REST API. Jquery Ajax and search APIs are used to perform the operation.


Retrieve Boards:


Board is just a way to categorize the documents on the site. The operation shown below will fetch all the board names that the user has added it to his documents. Say for example, user has added board called “Cricket” to two documents and “Tennis” to one document. This means the user is following two boards. They are Cricket and Tennis (these values will be listed on Delve left pane). There can be boards added by other users as well, which will not be listed on the user's Delve page. We will see how we can retrieve these boards from Office 365 Delve.
  • The search API is used to pull the information. 
  • The necessary parameters to query the board details are the query text and properties. In the query text, we need to pass the path to get the board names available on Delve. We need to set the external content to be true in the properties, so it will retrieve all the properties related to the board.
  • Using Ajax call with get method, retrieve the board names. Set the necessary parameters in the Ajax method.
    1. var getBoardURL = "/_api/search/query?QueryText='Path:\"TAG://PUBLIC/?NAME=*\"'&Properties='IncludeExternalContent:true'";  
    2. $.ajax  
    3. ({  
    4.     url: getBoardURL,  
    5.     method: 'GET',  
    6.     async: false,  
    7.     headers:   
    8.     {  
    9.         "Accept""application/json; odata=verbose"  
    10.     },  
    11.     success: function(response)   
    12.     {  
    13.         // Get the exact result set.    
    14.         var resultSet = response.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;  
    15.         // Loop through result set to get the properties for each boards.    
    16.         for (var i = 0; i < resultSet.length; i++)   
    17.         {  
    18.             console.log("Title    : " + resultSet[i].Cells.results[3].Value);  
    19.             console.log("Actor ID : " + resultSet[i].Cells.results[1].Value);  
    20.         }  
    21.     },  
    22.     error: function(data) {}  
    23. });    
From the response, we can get the board names, their corresponding actor IDs, path and other board details.



Retrieve Documents from board:


  • The board actor ID (identified in the section, above) is used to retrieve the respective board documents. 
  • To retrieve the documents, we will the use search API again to pull the information. 
  • The necessary parameters are the query text and properties. In the query text, pass * or any keyword to retrieve the documents. In the properties parameter, pass the graph query along with actor ID (identified in the section, above) of the board and action ID. (Here, action ID should be 1045 to denote the board). Here, we are passing single board actor ID to retrieve the particular board documents. You can change the logic to get multiple board's documents.
    1. var actorId = ""// Set the actor id of board (can be identified from above section) to see the documents.    
    2. getDocumentURL = "/_api/search/query?QueryText='*'&Properties='GraphQuery:actor(" + actorId + "\\,action\\:1045)'"  
    3. $.ajax  
    4. ({  
    5.     url: getDocumentURL,  
    6.     method: 'GET',  
    7.     async: false,  
    8.     headers:  
    9.     {  
    10.         "Accept""application/json; odata=verbose"  
    11.     },  
    12.     success: function(response)   
    13.     {  
    14.         // Get the exact result set.    
    15.         var resultSet = response.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;  
    16.         // Loop through result set to get the documents from the board.    
    17.         for (var i = 0; i < resultSet.length; i++)   
    18.         {  
    19.             console.log("Title    : " + resultSet[i].Cells.results[3].Value);  
    20.             console.log("URL      : " + resultSet[i].Cells.results[6].Value);  
    21.             // Similarly other values can be retrieved     
    22.         }  
    23.     },  
    24.     error: function(data) {}  
    25. });   
Note: In these cases, you can check the result values in a Browser debugger console. You can change the logic to display the results on the pages. Place these scripts on the page, using the Web parts (Content Editor / Script Editor / any custom Web part).