Options
All
  • Public
  • Public/Protected
  • All
Menu

sn-client-js

sn-client-js

Gitter chat Build status Coverage Codacy Badge NPM version NPM downloads License semantic-release Commitizen friendly Greenkeeper badge

This component lets you work with the sensenet ECM Content Repository (create or manage content, execute queries, etc.) by providing a JavaScript client API for the main content operations.

This library connects to a SenseNet portal's REST API, but hides the underlying HTTP requests. You can work with simple load or create Content operations in JavaScript, instead of having to construct ajax requests yourself.

It also provides you the full SenseNet Content Type system hierarchy through Typescript classes with all the fields defined in the CTDs and the Content Type schemas with FieldSettings so that you can manage Content easy on client-side knowing the related fields and their settings.

Tested with the following Sense/Net Services version:

Sense/Net Services

Installation on an existing Sense/Net portal

Get the latest stable version with npm

npm install --save sn-client-js

or from the GitHub repository, build and place the downloaded and builded source into your project. If you want to use only the transpiled JavaScript modules, you can find them in the dist/src folder and import them like

var SN = require('/pathtomodule/sn-client-js');

If you want to use the module types you can find them in the src folder. Import them the following way:

import { Repository, ContentTypes } from 'sn-client-js';

let repository = new Repository.SnRepository();

this.Repository.Content.Create('Root/', { 
    Name: 'myFolder',
}, ContentTypes.Folder);

Installation into an external app with node and npm

To install the latest stable version

npm install --save sn-client-js

You can specify additional options when creating an SnRepository instance by the following way:

import { Repository, Config } from 'sn-client-js';

let repository = new Repository.SnRepository({
            RepositoryUrl: 'https://my-sensenet-site.com',
            ODataToken: 'OData.svc',
            JwtTokenKeyTemplate: 'my-${tokenName}-token-for-${siteName}',
            JwtTokenPersist: 'expiration'
        });
  • RepositoryURL: The component will communicate with your repositoy using the following url. This will fall back to your window.location.href, if not specified. To enable your external app to send request against your sensenet portal change your Portal.settings. For further information about cross-origin resource sharing in sensenet check this article.
  • ODataToken: Check your Sense/Net portal's web.config and if the ODataServiceToken is set, you can configure it here for the client side.
  • JwtTokenKeyTemplate - This will be the template how your JWT tokens will be stored (in local/session storage or as a cookie). ${tokenName} will be replaced with the token's name ('access' or 'refresh'), ${siteName} will be replaced with your site's name
  • JwtTokenPersist - You can change how JWT Tokens should be persisted on the client, you can use 'session', whitch means the token will be invalidated on browser close, or 'expiration', in that case the token expiration property will be used (See JWT Token docs for further details)

Import

CommonJS

var SN = require('sn-client-js');

let myRepository = new SN.Repository.SnRepository();

myRepository.Content.Create('Root/Path', {
    Name: 'MyFolderName',
}, SN.ContentTypes.Folder);

Typescript

import { Repository, ContentTypes } as SN from 'sn-client-js';

let repository = new Repository.SnRepository();
myRepository.Content.Create('Root/Path', {
    Name: 'MyFolderName'
}, ContentTypes.Folder)

Building sn-client-js

To run the linter and building the project, use:

npm run build

Running tests

To execute all unit tests and generate the coverage report, run:

npm t

Examples

Creating a Folder with the name 'Hello world'
repository.Content.Create('Root/Path', {
    Name: 'Hello world'
}, ContentTypes.Folder)
.subscribe(newFolder=>{
        console.log('New folder created: ', newFolder)
    }, err=> {
        console.error('Error happened during creating a Folder:', err)
    });

or

let folder = new ContentTypes.Folder({
    Name: 'Hello world'
}, repository);

repository.Content.Post('Root/Path', folder, ContentTypes.Folder)
.subscribe(newFolder=>{
        console.log('New folder created: ', newFolder)
    }, err=> {
        console.error('Error happened during creating a Folder:', err)
    });
Load a Content by its id
repository.Load(1234,{expand: 'Avatar'}, 'A.1', ContentTypes.User)
.subscribe( user=> {
        console.log('User:', user);
    }, err=>{
        console.error('Error happened during loading an user:', err)
    });
Get the Schema of the given ContentType
let schema = SN.Content.GetSchema('GenericContent');
Read Collection data
let collection = new SN.Collection([], repository.Content);
var options = new SN.ODataApi.ODataParams({ 
    select: ["DisplayName", "Lead"], 
    orderby: 'DisplayName', 
    metadata: 'no' });

let fetchContent = collection.Read('/NewsDemo/External', options); //gets the list of  the external Articles with their Id, Type and DisplayName fields.
   fetchContent
       .map(response => response.d.results)
    .subscribe({
        next: response => {
             //do something with the response
         },
         error: error => console.error('something wrong occurred: ' + error),
         complete: () => console.log('done'),
    });
Delete a Content from a Collection
let deleteContent = myCollection.Remove(3);
    deleteContent
    .subscribe({
        next: response => {
            //do something after delete
        },
        error: error => console.error('something wrong occurred: ' + error),
        complete: () => console.log('done'),
    });

Generated using TypeDoc