Building A Custom Widget that accesses SharePoint Lists - akumina/AkuminaTraining GitHub Wiki

Applies to

Akumina Foundation 3.3.0.0 and later

Overview

Although the Generic List Control is often sufficient for widgets that draw data from SharePoint, situations may arise when the best solution is to have a Custom Widget draw data from SharePoint. In this document, we will create a Custom Widget and retrieve list data via the GetList functionality.

Hello World List Widget

Screenshot

image1

How to Deploy

  1. Create the HelloWorldList_AK list within your Akumina Foundation site. The HelloWorldList_AK list will need an additional text field called ‘Value’
  2. Download digitalworkplace.custom.js from the “/Style Library/DigitalWorkplace/JS” folder within SharePoint
  3. Paste the Widget Definition within digitalworkplace.custom.js
  4. Upload the updated digitalworkplace.custom.js to the “/Style Library/DigitalWorkplace/JS” folder within SharePoint
  5. In the Management Apps tab of AppManager, click on the View Manager. Click “Add New”. In the left pane navigate to “/DigitalWorkplace/Content/Templates/” for the folder path. Click “Choose File”, navigate to your custom template (helloworld3.html). Click “Save”.
  6. In the Management Apps tab of AppManager, click on the Widget Manager app. Then click on Add Widget. Create your widget with the values in the Widget Manager – Widget Definition section. Click Save & Exit
  7. In the Manage Widgets window, find the HelloWorldList widget and click its ‘View Widget Definitions’ button. Then click on ‘Add New’. Create your widget instance with the values in the Widget Manager – Widget Instance section. Click Save & Exit
  8. Copy the Widget Snippet of your Widget Instance.
  9. Paste the snippet into a Content Editor Web Part on a page within the site. Publish the page.
  10. Flush your cache by clicking on the Akumina icon in the left rail, clicking the refresh icon, then clicking “Refresh All Cache”
  11. Refresh the page. You will see your Custom Widget pulling data from the HelloWorldList_AK list.

HelloWorldList_AK

Create a new custom list called 'HelloWorldList_AK'. Add an additional column called 'Value' with the type 'Single Line of Text'. Add the following values to the list. image2

Widget Definition

if ((typeof Akumina.AddIn.HelloWorldListWidget) === 'undefined') {
    Akumina.AddIn.HelloWorldListWidget = function () {
        var _cur = this;
        this.GetPropertyValue = function (requestIn, key, defaultValue) {
            var propertyValue = "";
            for (var prop in requestIn) {
                if (key.toLowerCase() == prop.toLowerCase()) {
                    propertyValue = requestIn[prop];
                    break;
                }
            }
            return (propertyValue == undefined || propertyValue.toString().trim() == "") ? defaultValue : propertyValue;
        };
        this.SetDefaultsProperties = function (requestIn) {
            var requestOut = requestIn
            requestOut.SenderId = _cur.GetPropertyValue(requestIn, "id", "");
            requestOut.DisplayTemplateUrl = _cur.GetPropertyValue(requestIn,
            "displaytemplateurl", "");
            //requestOut.Test = _cur.GetPropertyValue(requestIn, "test", "");
            return requestOut;
        };
        //"Init" is the main function called from the framework, everything else is specific to this widget
        this.Init = function (HelloWorldRequest) {
            _cur.HelloWorldRequest = _cur.SetDefaultsProperties(HelloWorldRequest);
            _cur.HelloWorldRequest.EditMode = Akumina.AddIn.Utilities.getEditMode();
            //Widget Framework
            var widgetName = "HelloWorldList";
            Akumina.Digispace.WidgetPropertyViews.AddViewForProperty(widgetName,"DisplayTemplateUrl", "TemplatePicker");
           
           _cur.Prerender();
        };
        this.Prerender = function () {
            var targetDiv = this.HelloWorldRequest.SenderId;
            $("#" + targetDiv).html(Akumina.Digispace.ConfigurationContext.LoadingTemplateHtml);
            //subscribe to loader completed event, this is fired at the end of the DWP page lifecycle
            Akumina.Digispace.AppPart.Eventing.Subscribe('/loader/completed/', _cur.Render);
            //subscribe to refresh event, called by Widget Manager on DWP
            Akumina.Digispace.AppPart.Eventing.Subscribe('/widget/updated/', _cur.RefreshWidget);
        };
        this.Render = function () {
            var request = {}
            //data.Test = _cur.HelloWorldRequest.Test;
			request.listName = "HelloWorldList_AK";
			request.isRoot = true;
			request.selectFields = ["Title","Value"].join();
			
			//Create instance of Akumina.Digispace.Data.SharePoint
			var spcaller = new Akumina.Digispace.Data.SharePoint();
			
			//Call GetList
			spcaller.GetList(request).then(function (data) {
				//retrieve response from the object returned
				var response = data.response;
				
				//pass response data to Success Handler
				_cur.onQuerySucceeded(response);
				
			},
			function (error) {
				_cur.onQueryFailed(error.sender, error.args);
			});
           
        };
		this.onQuerySucceeded = function(response) {
			var listItemInfo = '';
			var listItemEnumerator = response.listItems.getEnumerator();
			var data = {};
			data.Items = [];
			
			while (listItemEnumerator.moveNext()) {
				var oListItem = listItemEnumerator.get_current();
				var returnItem = {};
				
				//Populate item object with field values
				returnItem.Title = oListItem.get_item('Title');
				returnItem.Value = oListItem.get_item('Value');
				
				//Push item
				data.Items.push(returnItem);
			}
			if (!_cur.HelloWorldRequest.EditMode) {
				_cur.BindTemplate(_cur.HelloWorldRequest.DisplayTemplateUrl, data, _cur.HelloWorldRequest.SenderId);
			}
		};
        this.RefreshWidget = function (newProps) {
            if (newProps["id"] == _cur.HelloWorldRequest.SenderId) {
                _cur.HelloWorldRequest = _cur.SetDefaultsProperties(newProps);
                _cur.Render();
            }
        };
        this.BindTemplate = function (templateUri, data, targetDiv) {
            new Akumina.Digispace.AppPart.Data().Templates.ParseTemplate(templateUri, data).done(function (html) {
                $("#" + targetDiv).html(html);
            });
        };
    }
}

Template

helloworld3.html

<div class="interAction">
{{#Items}}
   <h3 style="color:blue;">View 3: This is the view 3! {{Title}} - {{Value}}</h1>
{{/Items}}
</div>

Widget Manager - Widget Definition

image3

WidgetName

HelloWorldList

WidgetClass

Akumina.AddIn.HelloWorldListWidget

WidgetViews

Hello World List

View Name

Hello World List

Relative Template Url

/Style Library/DigitalWorkPlace/Content/Templates/helloworld3.html

Widget Manager - Widget Instance

Most of the fields here will be auto generated image4

Title

Hello World List

WidgetOptions

Hello World List

Display View

Checked

Selected View

Selected

Widget Manager - Widget Snippet

image5

⚠️ **GitHub.com Fallback** ⚠️