+ All Categories
Home > Documents > Introduction to interacting with list data using Knockout and REST web services.

Introduction to interacting with list data using Knockout and REST web services.

Date post: 28-Mar-2015
Category:
Upload: summer-blanks
View: 212 times
Download: 0 times
Share this document with a friend
Popular Tags:
34
SharePoint and Knockout for the REST of Us Introduction to interacting with list data using Knockout and REST web services
Transcript
Page 1: Introduction to interacting with list data using Knockout and REST web services.

SharePoint and Knockout for the REST of Us

Introduction to interacting with list data

using Knockout and REST web services

Page 2: Introduction to interacting with list data using Knockout and REST web services.

Introductions

Who is the one they call ‘Xenox’?

Page 3: Introduction to interacting with list data using Knockout and REST web services.

How did I get here?

Musician

Developer

Father

SharePointian

Federal Team

SharePoint Developer Planet Technologies

Page 4: Introduction to interacting with list data using Knockout and REST web services.

Agenda

What are we going to do today?

Page 5: Introduction to interacting with list data using Knockout and REST web services.

Recipe

MVVM in briefKnockout overviewSharePoint REST overviewConnecting the Dots

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 6: Introduction to interacting with list data using Knockout and REST web services.

MVVM (Model View ViewModel)

What is it?

Page 7: Introduction to interacting with list data using Knockout and REST web services.

MVVMSeparation of powers/interestsModel

Holds the information Independent of UI

View Formats the information State representation of the view model

ViewModel Encapsulates behavior Code representation of data and operations

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 8: Introduction to interacting with list data using Knockout and REST web services.

Knockout.js

Overview

Page 9: Introduction to interacting with list data using Knockout and REST web services.

Knockout.js

JavaScript LibraryFree, Open SourceNo dependencies (other than JavaScript)Compatible with all major browsersUsed to implement Model View ViewModel pattern2-Way Data BindingNotify subscribers about changes

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 10: Introduction to interacting with list data using Knockout and REST web services.

Knockout.js -> View

Where your information is displayedEssentially the HTML portion

<input type="text"/><input type="checkbox"/><select> … </select>

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 11: Introduction to interacting with list data using Knockout and REST web services.

Knockout.js -> Model

Defines what is kept in your data store Looks like JSON{

j: 'Java',

s: 'Script',

o: 'Object',

n: 'Notation'} Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 12: Introduction to interacting with list data using Knockout and REST web services.

Knockout.js -> ViewModel

Where the most of your logic and coding isBinding definitionsThe glue that holds it all together

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 13: Introduction to interacting with list data using Knockout and REST web services.

Knockout Demo

Simple binding

Page 14: Introduction to interacting with list data using Knockout and REST web services.

Starting out small

Create a Simple SharePoint List (MODEL)Representatives

First NameLast NameSpecialty

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 15: Introduction to interacting with list data using Knockout and REST web services.

Starting out small

Build a simple web page (VIEW)Use simple HTML Controls

<p>First Name: <strong data-bind="text:FirstName"></strong></p><p>Last Name: <strong data-bind="text:LastName"></strong></p><p>Specialty: <strong data-bind="text:Specialty"></strong></p>

Page 16: Introduction to interacting with list data using Knockout and REST web services.

Apply the glueDefine bindings and Link it to HTML with Data Bindings (VIEWMODEL)

<script type="text/javascript">var Person = function () {

var self = this;this.FirstName = ko.observable("Dale");this.LastName = ko.observable("Doback");this.Specialty = ko.observable("Research and

Development");}

var myRep = new Person();ko.applyBindings(myRep);

</script>

Page 17: Introduction to interacting with list data using Knockout and REST web services.

Big Deal, what next?

Modify HTML to allow editsBind <input> controls

Modify valueUpdate propertyvalueUpdate: ‘afterkeydown’

Bind a <select> with options

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 18: Introduction to interacting with list data using Knockout and REST web services.

Web Services

What options to I have within SharePoint?

Page 19: Introduction to interacting with list data using Knockout and REST web services.

Web Services Options - Lists

Web Services (SOAP) http://<site>/_vti_bin/Lists.asmx

REST Interface (OData) http://<site>/_vti_bin/listdata.svc

New in 2013 http://<site>/_api/

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 20: Introduction to interacting with list data using Knockout and REST web services.

Web Services – listdata.svc Construct HTTP request to query SharePoint list data

HTTP verb Data operation

GET Retrieve

POST Create

PUT Update (update all fields and use default values for any undefined fields)

DELETE Delete

MERGE Update (update only the fields that are specified and changed from current version)

Page 21: Introduction to interacting with list data using Knockout and REST web services.

listdata.svc – Lets get some

GET site information as ATOM feed (XML) http://{URL}/_vti_bin/listdata.svc

Get List data in XML /listdata.svc/{listname}

Get specific list item by ID /listdata.svc/{listname}(1)

Brings back list item #1

Available as JSON Also!Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 22: Introduction to interacting with list data using Knockout and REST web services.

OData query options

$metadata Shows data entity model for every list in site

$orderby Sorts by Property /listdata.svc/{listname}?$orderby=Title desc

$filter Filters by Property (works for lookup properties as

well) /listdata.svc/{listname}?$filter=Title eq ‘Some Value’ /listdata.svc/{listname}?$filter=Specialty/Title eq

‘Business’

$select Bring back only what you need /listdata.svc/{listname}?

$select=Title, ID

$expand Returns related data inline (Joins) /listdata.svc/{listname}?

$expand=Specialty

Many Many More …. See ODATA References

Page 23: Introduction to interacting with list data using Knockout and REST web services.

Demo

Putting it together LIVE

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 24: Introduction to interacting with list data using Knockout and REST web services.

Get SharePoint Data Create a Specialty List in SharePoint

Title Active

Make the Specialty dropdown list an observable Connect <select> options to SharePoint with REST call

<script type="text/javascript">var sOptions = ko.observable();

$.getJSON(_spPageContextInfo.webServerRelativeUrl + "_vti_bin/listdata.svc/Specialty?$select=Title,Id,Active&$filter=Active

eq true", {}, dataCallBack);

function dataCallBack(data) { sOptions(data.d.results); }</script>

Page 25: Introduction to interacting with list data using Knockout and REST web services.

Connect it

Add optionsText: binding to set select display textAdd optionsValue: binding to set value stored

<p>Specialty: <select data-bind="options:sOptions, optionsText:'Title',value:SpecialtyId, optionsValue:'Id'"></select></p>

self.SpecialtyId = ko.observable();

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 26: Introduction to interacting with list data using Knockout and REST web services.

Add functionality

Add button to page and register click: binding with the ViewModel

<input type="button" data-bind="click: AddRepresentative" value="Add Representative" />

self.AddRepresentative = function () {alert('You want to add ' + self.FirstName() + ' ' +

self.LastName());}

Page 27: Introduction to interacting with list data using Knockout and REST web services.

Post to a SharePoint ListCreate function to post to list

self.AddRepresentative = function () {var url = _spPageContextInfo.webServerRelativeUrl +

"_vti_bin/listdata.svc/Representatives";

$.ajax({type: 'POST',url: url,contentType: 'application/json',processData: false,data: ko.toJSON(myPerson),success: function () {

alert('You added ' + self.FirstName() + ' ' + self.LastName());

}});

Page 28: Introduction to interacting with list data using Knockout and REST web services.

Real Time Feed

Bind a 2nd view model Select the node of the DOM you want to bind to

<script type="text/javascript">var dispVm = {

results: ko.observable(), };

ko.applyBindings(dispVm, document.getElementById('divDisplay'));</script>

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 29: Introduction to interacting with list data using Knockout and REST web services.

Real Time Feed

Add foreach: binding

<div style="float:right; padding:25px;" id="divDisplay"><div data-bind="foreach:results">

<p><strong data-bind="text: FirstName"></strong><strong data-bind="text: LastName"></strong><span data-bind="if: (Specialty !=

null)">&nbsp;(<em data-bind="text:

Specialty.Title"></em>)</span></p>

</div></div>

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 30: Introduction to interacting with list data using Knockout and REST web services.

Real Time Feed

Add polling to retrieve list items

(function poll() {$.ajax({

url: _spPageContextInfo.webServerRelativeUrl + "_vti_bin/listdata.svc/Representatives?

$expand=Specialty",success: function (data) {

//Update the DisplaydispVm.results(data.d.results);

}, dataType: "json", complete: poll, timeout: 30000});

})();

Copyright: 2008 Columbia Pictures. All Rights Reserved.

Page 31: Introduction to interacting with list data using Knockout and REST web services.

We’ve only scratched the surface

Controlling text and appearance The visible

binding The text binding The html binding The css binding The style binding The attr binding

Working with form fields The click binding The event binding The submit binding The enable binding The disable binding The value binding The hasfocus binding The checked binding The options binding The selectedOptions

binding The uniqueName

binding

Control flow The foreach

binding The if binding The ifnot binding The with binding

Rendering templates The template

binding

Page 32: Introduction to interacting with list data using Knockout and REST web services.

Where do we go from here?

ExperimentWrite custom WCF ServicesWeb Parts

Page 33: Introduction to interacting with list data using Knockout and REST web services.

References MSDN - Using the REST Interface

http://msdn.microsoft.com/en-us/library/ff798339.aspx

MSDN – SharePoint Foundation REST Interface http://msdn.microsoft.com/en-us/library/ff521587(v=office.14).aspx

ODATA.ORG – URI Conventions http://

www.odata.org/documentation/uri-conventions#FilterSystemQueryOption

Knockout.js http://knockoutjs.com/

Long Polling Example http://

techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

Page 34: Introduction to interacting with list data using Knockout and REST web services.

Please fill out an Evaluation

Thank you!

http://bit.ly/spsbmorexlg


Recommended