npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sharepoint-list-item-model

v1.1.5

Published

Makes working with SharePoint lists and list items easy

Downloads

23

Readme

SharePoint List Item model

This is an abstract class that makes it easy to create models based on SharePoint list items in TypeScript. It is made for use with the SharePoint Framework and Node.js and uses the SharePoint pnp-js.

How to use

Import the dependencies:

import {SPListItemModel, SPList, SPField} from "sp-list-item-model"

Create a class that extends SPListItemModel and decorate it with @SPList(ListName: string, SiteURL: string) SiteURL is optional and defaults to the site that is the current execution context.

The fields with the @SPField(InternalName: string) decorator represent fields in the list item. InternalName is optional and defaults to the class property name. If the internal name of the field is different from your property name, then specify it here.

@SPList("employees", "https://mysite.sharepoint.com")
class Employee extends SPListItemModel {
    
    @SPField()
    Name: string
    
    @SPField()
    Address: string
    
    @SPField()
    Salary: number
    
    @SPField("aabbcc")
    HiredOn: Date
    
    //Add your own methods or properties
    NotASharePointField: string
    
    SomeMethod(): void {
        //do something
    } 
        
}
Creating an item
let e = new Employee();
e.Name = "John Doe";
e.Address = "123 Some Street";
e.Salary = 50000;
e.HiredOn = new Date();
e.submit()
    .then(()=>console.log("Item created"))
Retrieving a list item by its ID
Employee.getItemById(123)
    .then(e=>console.log(e.Name))
Retrieving all items from the list
Employee.getAllItems()
    .then(employees=>{
        employees.forEach(employee=>console.log(employee.Name))
    })
Retrieving items from the list using an OData filter
Employee.getItemsByFilter(`${Employee.getInternalName("Salary")} Ge 5000`)
    .then(employees=>{
        employees.forEach(employee=>console.log(employee.Name))
    })
Retrieving an item and updating its information

Note: the submit method checks if the fields you are updating have been changed since the item was loaded, and if so, will reject the promise. If you do not want this, then you can call e.submit(false) .

Employee.getItemById(123)
    .then(e=>{
        e.Name = "Joe Bloggs";
        return e.submit();
    })
    .then(()=>console.log("Updated"))
Updating an item without loading it first
let e = new Employee();
e.ID=123;
e.Name = "Joe Bloggs";
e.submit()
    .then(()=>console.log("Updated"));
Deleting an item
Employee.deleteItemById(123)
    .then(()=>{
        //do something
    })
Authentication

If you are accessing SharePoint data from the Sharepoint Framework, then this will be done on behalf of the currently authenticated user, so authentication is not needed. If you want to access data from Node.JS you will need to authenticate. You can use pnp-auth for this (see the documentation for pnp-auth for instructions).