This article is continuation of Part-1 & Part-2.

Let's start with Provider Hosted MVC App with AngularJS - Part 3. In this part we are going to create dynamic TileView using CSS and AngularJS.

SharePoint HostWeb List Structure


The following is a list structure which we are referencing for our application.



Figure 1: App

We are going to create the following UI Design and final output format.

UI Change

In top navigation bar new option added to show tileview i.e. TileView.



Figure 2: Tile

Output



Figure 3: output

Solution Changes in term of new controller, view , javascript and CSS files.



Figure 4: Solution

Let's start with service layer coding to fetch list data.

Step 1: Session Management in AngularJS

When we navigate from one page to another page, we need ' hostweburl ' & ' appweburl ' URL values to access SharePoint site data, so we need to save those values in session to access on another page.

There are two ways to save data in session - sessionStorage & localStorage.
  1. var hostweburl;
  2. var appweburl;
  3. //The SharePoint App where the App is actualy installed
  4. hostweburl = decodeURIComponent(QueryStringParameter('SPHostUrl'));
  5. //The App web where the component to be accessed by the app are deployed
  6. appweburl = decodeURIComponent(QueryStringParameter('SPAppWebUrl'));
  7. var Sessionhostweburl = sessionStorage.getItem("hostweburl");
  8. var Sessionappweburl = sessionStorage.getItem("appweburl");
  9. if (Sessionhostweburl == null || Sessionappweburl == null)
  10. {
  11. sessionStorage.setItem("hostweburl", hostweburl);
  12. sessionStorage.setItem("appweburl", appweburl);
  13. }
  14. if (hostweburl == null || appweburl == null || hostweburl == 'undefined' || appweburl == 'undefined')
  15. {
  16. hostweburl = sessionStorage.getItem("hostweburl");
  17. appweburl = sessionStorage.getItem("appweburl");
  18. }
  19. console.log("hostweburl : " + hostweburl);
  20. console.log("appweburl : " + appweburl);

app-data-service.js

New service layer method to get SharePoint List data in order to create TileView.

  1. this.getTileData = function()
  2. {
  3. var TileArray = [];
  4. var deferred = $q.defer();
  5. // Load 15hives js files and continue to the successHandler
  6. $.getScript(scriptbase + "SP.Runtime.js",
  7. function()
  8. {
  9. $.getScript(scriptbase + "SP.js",
  10. function()
  11. {
  12. $.getScript(scriptbase + "SP.RequestExecutor.js",
  13. function()
  14. {
  15. //Represents the context for objects and operations.
  16. var context = new SP.ClientContext(appweburl);
  17. //Access host web from app using JavaScript CSOM
  18. var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
  19. context.set_webRequestExecutorFactory(factory);
  20. var appContextSite = new SP.AppContextSite(context, hostweburl);
  21. var web = appContextSite.get_web();
  22. context.load(web);
  23. //Access HostWeb List
  24. var list = web.get_lists().getByTitle('lstTileLinks');
  25. var camlQuery = SP.CamlQuery.createAllItemsQuery();
  26. this.listItems = list.getItems(camlQuery);
  27. context.load(this.listItems);
  28. context.executeQueryAsync(
  29. Function.createDelegate(this, function()
  30. {
  31. var ListEnumerator = this.listItems.getEnumerator();
  32. while (ListEnumerator.moveNext())
  33. {
  34. var currentItem = ListEnumerator.get_current();
  35. TileArray.push({
  36. ID: currentItem.get_item('ID'),
  37. Title: currentItem.get_item('Title'),
  38. TileURL: currentItem.get_item('TileURL')
  39. });
  40. }
  41. console.log(TileArray);
  42. deferred.resolve(TileArray);
  43. }),
  44. Function.createDelegate(this, function(sender, args) {
  45. deferred.reject('Request failed. ' + args.get_message());
  46. console.log("Error : " + args.get_stackTrace());
  47. }));
  48. });
  49. });
  50. });
  51. return deferred.promise;
  52. }; // Get

app-tile-controller.js

New controller added into solution for TileView creation.
  1. (function()
  2. {
  3. console.log('app-tile-controller.js loaded ..');
  4. 'use strict'
  5. angular.module(window.AngularApp_constants.architecture.modules.MainApp.moduleKey)
  6. .controller('TileController', HomeController)
  7. HomeController.$inject = ['$scope', '$location', 'app-data-service'];
  8. function HomeController($scope, $location, AngularDataService)
  9. {
  10. /********* Fill Tile *********/
  11. $scope.TileMaster = [];
  12. LoadTileData();
  13. function LoadTileData()
  14. {
  15. console.log("LoadTileData called");
  16. var promiseGet = AngularDataService.getTileData();
  17. promiseGet.then(function(resp)
  18. {
  19. $scope.TileMaster = resp;
  20. },
  21. function(err)
  22. {
  23. $scope.Message = "Error " + err.status;
  24. console.log($scope.Message);
  25. });
  26. }
  27. }
  28. })();

TileMasterController

New controller added into solution for tile creation.
  1. public class TileMasterController: Controller
  2. {
  3. // GET: TileMaster
  4. public ActionResult TileMaster()
  5. {
  6. return View();
  7. }
  8. }

TileMaster.cshtml

  1. @{
  2. ViewBag.Title = "Home Page";
  3. }
  4. <!-- Javascript from hives Layout folder -->
  5. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
  6. <script type="text/javascript" src="/_layouts/15/sp.js"></script>
  7. <script type="text/ecmascript" src="/_layouts/SP.Core.js"></script>
  8. <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
  9. <!-- Basic JS Files -->
  10. <script src="~/AngularJSScript/BasicJS/angular.min.js"></script>
  11. <script src="~/AngularJSScript/BasicJS/trNgGrid.js"></script>
  12. <script src="~/AngularJSScript/BasicJS/angular-messages.js"></script>
  13. <!-- Custom JS Files -->
  14. <script src="~/AngularJSScript/CustomJS/app-constants.js"></script>
  15. <script src="~/AngularJSScript/CustomJS/app-main.js"></script>
  16. <script src="~/AngularJSScript/CustomJS/app-data-service.js"></script>
  17. <script src="~/AngularJSScript/CustomJS/app-tile-controller.js"></script>
  18. <link href="~/Content/TileBranding.css" rel="stylesheet" />
  19. <div ng-app="AngularApp">
  20. <div ng-controller="TileController">
  21. <br />
  22. <br />
  23. <ul>
  24. <li ng-repeat="Tile in TileMaster">
  25. <div class="ActiveTile">
  26. <div class="TileHeading">
  27. <a href="{{ Tile.TileURL }}" target="_blank">{{ Tile.Title }} </a>
  28. </div>
  29. </div>
  30. </li>
  31. </ul>
  32. </div>
  33. </div>

TileBranding.css

  1. .ActiveTile {
  2. display: inline-block;
  3. position: relative;
  4. width: 250px;
  5. height: 150px;
  6. /*background: #ccffcc;*/
  7. background-color: rgb(0, 179, 125);
  8. z-index: 0;
  9. margin: 15px 40px 2px -2px;
  10. transition: all .15s ease-in-out;
  11. }
  12. .ActiveTile:hover {
  13. /*background: #9CC;*/
  14. background-color: #00CC66;
  15. z-index: 100;
  16. /*transform: scale(1.2,1.2);*/
  17. box-shadow: 0 5px 10px 0 rgba(0,0,0,.2);
  18. }
  19. .TileHeading {
  20. color: powderblue;
  21. font-size:20px;
  22. margin: 63px 10px 10px 60px;
  23. }
  24. /************************************/
  25. ul, li {
  26. list-style: none;
  27. margin: 0;
  28. padding: 0;
  29. display: inline;
  30. }
  31. a {
  32. display: block;
  33. width: 100%;
  34. height: 100%;
  35. text-decoration: none !important;
  36. color: white;
  37. font-size: 18px;
  38. }

I hope you all liked this article as you liked the Part 1 and Part 2 of the series..

Thank you!