Easy Way To Develop Against REST in SharePoint

Highlighted
Occasional Contributor

I rewrote my first REST library, presented at SPFest DC this year, in TypeScript and wanted to share the latest. It helps construct the url and transforms it if you are in the app web and reduces the number of calls to the server and code required to get work done.

 

Link to GitHub: SP REST

 

I hope this helps others out.

4 Replies
Highlighted

Nice tip @Gunjan Datta. Would you mind elaborating what the main benefits or differences are between your library and for example the perhaps more widely used Office PnP libraries for JS Core? (https://github.com/OfficeDev/PnP-JS-Core). 

 

If there's things in your library that the JS core doesn't cover, perhaps there's an opportunity to enhance the core with some logic from your side as well. 

 

Cheers :)

Tobias.

Highlighted

+1 on @Tobias Zimmergren comment.

 

Would also like to point you to the PnP YouTube channel which has a playlist for the Javascript Special Interest Group (SIG) in which the PnP-JS-Core library is discussed amongst things: https://www.youtube.com/playlist?list=PLR9nK3mnD-OXdcwfcHGsGr78nHWLRsv1x.

 

The SIG has a recurring weekly meeting in which your more than welcome to partipate and help furhter evolve the PnP-Core-JS library: http://aka.ms/officedevpnpofficehours

Bi-Weekly PnP JavaScript Special Interest Group meeting recordings. Discussions and topics around the SharePoint Framework and also on the PnP JavaScript Cor...
Highlighted

@Tobias Zimmergren

 

The library, also written in TypeScript, essentially builds the url via a mapper class and the metadata type. The documentation on my github site goes into detail of the library and has examples of all the objects you can create via 1 request.

 

Since you can switch between synchronous and asynchronous requests via a flag, I've been able to use the browser's console window and execute requests similar to powershell, when executing synchronous requests. This all runs under the context of the user executing the requests. This has been very useful when you do not have access to the farm, or need to execute a simple script. I don't believe, that the pnp-js-core has this type of feature.

 

Here is an example of creating a list asynchronously:

 

// This will create the web object, set the asynchronous flag and not execute a request to the server
(new $REST.Web_Async(false))
// This will execute a request to the server to create a list
.addList({
BaseTemplate: 100,
Description: "This is a test list.",
Title: "Test"
})
// This will execute after the list is created
.done(function(list) {
// Additional code goes here
});

 

Here is an example of getting list items from a list/library:

 

// This will execute one request to the server to get list items
// new $REST.ListItems("[List Name]", "[View XML or CAML Query]");

// The query will default the parent to "<View>"

 

var items = new $REST.ListItems("Site Assets", "<Query><Where><Gt><FieldRef Name='ID' /><Value Type='Integer'>0</Value></Gt></Where></Query>");

 

var items = new $REST.ListItems("Site Assets", "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>sprest.js</Value></Eq></Where></Query></View>");


Here is an example of getting the "sprest.js" file in the "Documents" list:

 

List: var file = new $REST.File("sprest.js", "documents");
Web: var file = new $REST.File("/sites/dev/shared documents/sprest.js");

Highlighted
There is a demo project I'm working on to give an example of using the library here: https://github.com/gunjandatta/sprest-list

The main script file is located here: https://github.com/gunjandatta/sprest-list/blob/master/Scripts/list.js

I tend to comment more than I code, so hopefully it's easy to read. I wanted to give examples of creating list/items, updating items and getting them. It's not 100% complete, but has examples of the 1 line code/requests to get the data for you app.

If this can help the PnP-JS-Core library in any way, that's a win in my book.

Thanks,
Gunjan