Coder Social home page Coder Social logo

backbone.sharepoint's Introduction

Backbone.SharePoint

Backbone.SharePoint extends Backbone Models and Collections so you can easily work with SharePoint Items and Lists. Its custom sync() method is a wrapper around the SharePoint ListData.svc REST service which is based on OData.

Backbone.SharePoint features:

  • create, read, update and delete SharePoint items as Backbone models
  • fetch multiple SharePoint items from a list as a Backbone collection
  • support for OData query string options (orderby, filter, select, etc)
  • JSON payloads
  • custom sync() to communicate with the ListData service
  • partial updates: only changed attributes are sent to the server during an update
  • lightweight

NEW: I am extending Backbone.SharePoint to also support the use of SharePoint Web Services. Experimental support for fetching Backbone Collections through Lists.asmx is now available in file backbone-sharepoint.soap.js

Contents

Because of the Same Origin Policy, your html file must served from the same domain as the SharePoint site you want to access. You can place your html file containing your app on the server file system or in an asset library.

index.html:

<!doctype html>
<html>
   ....
<script src="jquery.js"></script> 
<!-- you can also use zepto.js -->

<script src="underscore.js"></script>
<script src="backbone.js"></script>
<script src="backbone-sharepoint.odata.js"></script>
  ...

</html>

Now let's look at some examples how you can use Backbone.SharePoint. In these examples we will assume you have a subsite '/teamsite' in which you have created a Contacts list based on the standard contacts list template.

// You define a Contact Model for items by extending Backbone.SP.Item
// Required parameters are the SharePoint site and the name of the list

var Contact = Backbone.SP.Item.extend({
	site: '/teamsite',
	list: 'Contacts'
})


// Create a new contact, the attributes refer to item column names.
// Please note capitals. We follow SharePoint conventions
var contact = new Contact({ LastName: "Davis" })


// At this point we have a new contact model, but is not yet saved to the server.
// So let's save it to the server.
contact.save()

  ....

// Update the attributes of the Item:
contact.set({FirstName: "John"})
contact.save()

  ...


// Finally, to remove an item:
contact.destroy()

You can work with SharePoint lists just like collections.

// you can define a SP List by extending Backbone.SP.List.
// Configure the list by associating it with a Model extended from Backbone.SP.Item 
var Contacts = Backbone.SP.List.extend({
	model: Contact
})

// now, you can create a list
var contacts = new Contacts


// get contacts list from the server
contacts.fetch()


// the fetch options allow you to use query options
// for example, the request below will fetch only the LastName and FirstName columns
// for item 11..15 when ordered descending by LastName
contacts.fetch({
	select: 'LastName, FirstName',
	orderby: 'LastName desc',
	top: 5,
	skip:10
})



   ....


// This is how you can create a new contact, save it to the server and add it to the list (collection)
contacts.create({
	LastName: "Peel",
	FirstName: "Emma"
})

Hopefully this is sufficient to get you going!

The 'test' directory contains a unit tests based on QUnit. Open test.html in browser to run the tests.

Instead of the odata file, include the backbone-sharepoint soap version:

<!doctype html>
<html>
<head>
<script src="jquery.js"></script> 
<!-- you can also use zepto.js -->

<script src="underscore.js"></script>
<script src="backbone.js"></script>
<script src="backbone-sharepoint.soap.js"></script>
<script>
var Contact = Backbone.SP.Item.extend({
})


var Contacts = Backbone.SP.List.extend({
    model: Contact,
    site: '/teamsite',
    list: 'Contacts',
    view: ''  //  default view, you can also provide viewId: '{GUID}'
})


var ContactsView = Backbone.View.extend({
    tagName: "ul",
    tpl: _.template(
		"<% _.each(contacts, function(contact) { %>" +
			"<li><%= contact.Title + ', ' + contact.FirstName %></li>" +
		"<% }); %>"
	),
    render: function () {
        $(this.el).html(this.tpl({ contacts: this.collection.toJSON() }));
        $('body').append(this.el);
    }
});


$(function () {
    var contacts = new Contacts;
    var view = new ContactsView({ collection: contacts });
    contacts.bind("reset", view.render, view);
    contacts.fetch()
})
</script>
</head>
<body>
</body>
</html>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.