Showing posts with label RESTAPI. Show all posts
Showing posts with label RESTAPI. Show all posts

Monday 10 December 2018

Accessing Office 365 Project Web App Objects using REST API

This article describes accessing the objects present on the Office 365 project web app site collection using REST API.

PWA abbreviated as Project Web App, is available on Office 365, and it used for easily and efficiently planning projects, track status, and collaborate with other virtually. It is an application built on top of SharePoint, which has collaborating features. Any PWA site collection will have the project web app settings feature enabled on the site collection. To know more about PWA, refer to the official documentation available on the MSDN site.

Let us see the high level REST API (service end points), which can be used for accessing objects available on Office 365 PWA site collections.

Note: Replace the Office 365 PWA url with your respective URL.

To access the project server and list of available objects,
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer

The REST APIs used for accessing objects available on the project servers are explained below.


Custom Fields:

The enterprise custom fields are created on the project plans for managing additional project attributes. The custom field can be at project or task level. The following API is used to access all the enterprise custom fields.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields

One specific field can be accessed using the field GUID:
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')

Tuesday 28 June 2016

Create, Retrieve, Update or Delete Sub Sites On SharePoint Using REST API

In this post, you will learn how to retrieve, create, update or delete the sites under a site collection, using REST API's on SharePoint 2013 / SharePoint 2016 / SharePoint Online sites like O365. The sites are considered as sub sites within the site collection.


Steps Involved:
  • Identify the site URL and build the REST API URL.
  • Use AJAX jQuery call to accomplish the necessary operation.
  • You can set the return value type in the AJAX call request. In my case, I have specified JSON to be the return value in the data parameter.
  • 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) at the site collection level.


Create Sub Site:

The operation will create a sub site within the site collection. 
  • The REST API will complete the create operation http://siteurl/_api/contextinfo.
  • The method or type should be POST. 
  • The form digest value is required to validate the create operation. Hence, before creating the request, the form digest value should be retrieved. We can use an AJAX call to accomplish all the tasks. The REST API will retrieve form digest value  http://siteurl/_api/contextinfo. Or simply from the request digest control, we can retrieve the form digest.value.$('#__REQUESTDIGEST').val().
  • For creating a site, we need to send the necessary information to the Service. For this, we will use JSON format, where we will send site title, site template, site URL, description, unique permissions etc.
  • In the header, send the required content type and form digest value for the security validation.
To get the form digest value, you can use the logic, given below:
  1. $.ajax  
  2.     ({    
  3.         url: "/_api/contextinfo",    
  4.         type: "POST",    
  5.         async: false,    
  6.         headers: { "accept""application/json;odata=verbose" },    
  7.         success: function(data){    
  8.              requestDigest = data.d.GetContextWebInformation.FormDigestValue;  
  9.         },    
  10.         error: function(data){    
  11.         }    
  12.     });  
The code snippet given below helps in creating a site. In this example, the form digest value is retrieved directly from the page control. (The operation given above is not required)
  1. var restAPIURL = "/_api/web/webinfos/add";  
  2. var newSiteData = JSON.stringify(
  3. {
  4.     'parameters': {  
  5.          '__metadata': {   
  6.                'type''SP.WebInfoCreationInformation'   
  7.          },  
  8.          'Url''SubSite',   
  9.          'Description''Subsite created from REST API',  
  10.          'Title''SubSite',  
  11.          'Language': 1033,  
  12.          'WebTemplate''STS#0',  
  13.           'UseUniquePermissions'false  
  14.     }  
  15.  });  
  16. $.ajax  
  17. ({    
  18.     url: restAPIURL,    
  19.     type: "POST",    
  20.     async: false,  
  21.     data: newSiteData,    
  22.     headers: {  
  23.         "accept""application/json;odata=verbose",  
  24.         "content-type""application/json;odata=verbose",  
  25.         "X-RequestDigest": $('#__REQUESTDIGEST').val()  
  26.     },   
  27.     success: function(data){    
  28.          console.log('site created');  
  29.     },    
  30.     error: function(data){   
  31.         console.log('Error creating site');   
  32.     }    
  33. });   


Retrieve Sub Site:

The site properties can be retrieved, using REST API. The URL to access the properties is,http://subsiteurl/_api/web

This will get the basic properties of the site. To get more properties, we need to use select and expand parameters in the URL. For example, the URL given above will not fetch more information on the features. To get the active features on the site, we need to pass the feature on the select and expand parameters to retrieve the information. Similarly, other collection values needs to be retrieved with the select and expand parameters.

The code snippet given below helps to retrieve the site properties.
  1. //var restAPIURL = "/subsite/_api/web?$select=Features&$expand=Features/Id"  
  2. var restAPIURL = "/subsite/_api/web"  
  3. $.ajax  
  4. ({    
  5.     url: restAPIURL,    
  6.     type: "GET",  
  7.     headers: { "accept""application/json;odata=verbose" },  
  8.     success: function(data){    
  9.          console.log("Title : " + data.d.Title);  
  10.          console.log("Description : " + data.d.Description);  
  11.     },    
  12.     error: function(data){   
  13.         console.log('Error getting info');   
  14.     }    
  15. });  


Update Sub Site

The update operation will be very similar to creating an operation.
  • The URL for updating the operation will be, http://subsiteurl/_api/web.
  • The method or type will be POST.
  • The properties that need to be updated are sent, using JSON string.
  • In the header parameter, we need to set and send the content type for the data type, form digest value for the security validation and “Merge” method for the update operation.
The code snippet given below helps in updating the site properties. Here, just update the title of the sub site created in the above sections:
  1. var updateSiteData = JSON.stringify(
  2. {  
  3.     '__metadata': {   
  4.          'type''SP.Web'   
  5.     },  
  6.     'Description''Subsite updated from REST API',  
  7.     'Title''New SubSite',  
  8.  });  
  9.   
  10. restAPIURL = "/subsite/_api/web"  
  11. $.ajax  
  12. ({    
  13.     url: restAPIURL,    
  14.     type: "POST",  
  15.     async: false,  
  16.     data: updateSiteData,  
  17.     headers: {  
  18.          "accept""application/json;odata=verbose",  
  19.         "content-type""application/json;odata=verbose",  
  20.         "X-RequestDigest": $('#__REQUESTDIGEST').val(),    
  21.         "X-HTTP-Method""MERGE"  
  22.     },   
  23.     success: function(data){    
  24.          console.log('site updated');  
  25.     },    
  26.     error: function(data){   
  27.         console.log('Error updating site');   
  28.     }    
  29. });  


Delete Sub Site:

The delete operation will be very similar to create or update operations. Here, no input data is required.
  • The URL will be http://subsiteurl/_api/web.
  • The type or method will be POST.
  • In the header parameter, we need to set and send the content type for data type, form digest value for the security validation and “Delete” method for the delete operation.
The code snippet given below helps delete the site.
  1. var restAPIURL = "/subsite/_api/web"  
  2. $.ajax  
  3. ({    
  4.     url: restAPIURL,    
  5.     type: "POST",  
  6.     async: false,  
  7.     headers: {  
  8.          "accept""application/json;odata=verbose",  
  9.         "content-type""application/json;odata=verbose",  
  10.         "X-RequestDigest": $('#__REQUESTDIGEST').val(),    
  11.         "X-HTTP-Method""DELETE"  
  12.     },   
  13.     success: function(data){    
  14.          console.log('site deleted');  
  15.     },    
  16.     error: function(data){   
  17.         console.log('Error deleting site');   
  18.     }    
  19. });   


Sunday 19 June 2016

Retrieving videos from o365 video portal in multiple ways

In this post, we will see how we can retrieve videos and related information from o365 video portal with multiple ways. First we will see how we can retrieve all the videos from the portal using content type and also we will see how we can retrieve videos from the portal with multiple filters.


Retrieve All Videos Using Site Content Type ID:

The videos are stored on the portal using cloud video content type. At the video library (list level), the cloud video content type will differ for multiple channels. Also this content type will differ on various sites. Video cloud content type is present at site level. The content type id of video cloud will be common for any o365 portal. The content type id is 0x010100F3754F12A9B6490D9622A01FE9D8F012. It is 40 digits.

At the library level, the video cloud content type id consists of 74 digits. But first 40 digits will be common, since the list level video cloud content type is inherited from site level video cloud content type.

Using REST Search API, the all the videos present across various channels can be retrieved from the portal in a single call with site level cloud video content type. In the query text, we will pass the content type id by appending '*'. So that all the videos which has the content type starting with the same digits will be retrieved.

The Search API will be,
/_api/search/query?QueryText=%27contenttypeid:0x010100F3754F12A9B6490D9622A01FE9D8F012*%27

The results are based on search indexing. The crawl should run periodically to get the video results. And also the properties retrieved here will be more focused on analytical information with some basic video properties. The information like processing status, video running time, channel information will not be retrieved here.  

The following code snippet shows the JQuery Ajax call to get the videos from Office 365 video portal, using REST search API:

  1. $.ajax({  
  2.     url: "/_api/search/query?QueryText=%27contenttypeid:0x010100F3754F12A9B6490D9622A01FE9D8F012*%27",  
  3.     type: "GET",  
  4.     async: false,  
  5.     headers: { "accept""application/json;odata=verbose" },  
  6.     success: function(data){  
  7.         var videosInfo = data.d.query.PrimaryQueryResult.RelevantResults;  
  8.           
  9.         console.log("Available Videos Count on Channel : " + videosInfo.RowCount);  
  10.     var videoResults = videosInfo.Table.Rows.results;  
  11.     for(var i = 0; i< videoResults.length; i++){  
  12.         console.log("Title : "+videoResults[i].Cells.results[3].Value);  
  13.         console.log("Created By : "+videoResults[i].Cells.results[4].Value);  
  14.         console.log("Video URL : "+videoResults[i].Cells.results[6].Value);  
  15.     }  
  16.     //Similarly other results can be retrieved and displayed  
  17.     },  
  18.     error: function(data){  
  19.     }  
  20. });  

The necessary parameters can be only be retrieved using selectProperties option in the API. The example looks like, 
/_api/search/query?QueryText=%27contenttypeid:0x010100F3754F12A9B6490D9622A01FE9D8F012*%27&selectproperties=%27Title%27


Retrieve Videos using Video & List APIs:

The REST Video API used to retrieve the videos from channel will be,
https://videoportalurl/_api/VideoService/Channels('channelid')/GetAllVideos

Here we will see various ways to filter out the videos from the portal. 

The video results here are instant results. More information like processing status, video download URL, owner details, running information, tags, channel and other basic information can be retrieved instantly. Here we can get the required parameters for videos using select parameters. The properties which are retrieved from the operation can be used in the select parameters to retrieve only specific information. For example, in the following example, we have retrieved only file name and video url using the REST API.

User can apply following use cases for retrieving videos. 
  • User can get only playable(processed) videos by applying processing status filter. (VideoProcessingStatus:2)
  • User can get only incomplete videos by applying the status filter (Not processed). (VideoProcessingStatus:1)
  • Filter only videos with the known property values. (AnyProperty:Value)
  • Most played videos (Sort Asc/desc using VideoDurationInSeconds)
 There are many other scenarios where filter and other parameters can be used to retrieve videos with REST Video API. These filter functionalities are available with REST List API as well. 

The API will look like,
https://videoportalurl/_api/VideoService/Channels('channelid')/GetAllVideos?$select=FileName, ServerRelativeUrl,Author&$expand=Author/Name&$filter=VideoProcessingStatus eq 2

OR

https://channelsiteURL/_api/web/lists/GetByTitle('Videos')/Items?$filter= VideoProcessingStatus eq 2

Both the above API will get same set of JSON response, which can be processed and displayed. But the response is different from search API response.

The following code snippet shows the Jquery Ajax call to get the videos present on the specified channel with filters and select parameters, using REST List API:
  1. $.ajax  
  2. ({    
  3.     url: "https://chennal_url/_api/web/lists/GetByTitle('Videos')/Items?$select=Title&$filter=VideoProcessingStatus eq 2",    
  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.                 
  12.         for(var i = 0; i< videoResults.length; i++){    
  13.             console.log("Title : "+videoResults[i].Title);      
  14.         }    
  15.     }    
  16.     //Similarly other results can be retrieved and displayed    
  17.     },    
  18.     error: function(data){    
  19.     }    
  20. });   
The following code snippet shows the Jquery Ajax call to get the videos present on the specified channel with filters and select parameters, using REST Video API:
  1. $.ajax({  
  2.     url: "https://videoportal_url/_api/VideoService/Channels('channelid')/GetAllVideos?$select=Title&$filter=VideoProcessingStatus eq 2",  
  3.     type: "GET",  
  4.     async: false,  
  5.     headers: { "accept""application/json;odata=verbose" },  
  6.     success: function(data){  
  7.         if(data.d != null && data.d.results.length > 0){  
  8.             var videoResults = data.d.results;  
  9.             for(var i = 0; i< videoResults.length; i++){  
  10.                 console.log("Title : "+videoResults[i].Title);  
  11.             }  
  12.         }  
  13.         //Similarly other results can be retrieved and displayed  
  14.     },  
  15.     error: function(data){  
  16.     }  
  17. });  

Note: Thus you have learned how to retrieve video details using REST search, REST video and REST list APIs with filter and select parameters. Also we understood the basic difference in getting the response using various REST API methods.