Easily pluggable data layer for SharePoint Framework(SPFx) applications following Repository pattern and some SharePoint utilities
Create the SPFx project according to Microsoft's documentation
Install the package using the command below
npm i jitz-sharepoint-utilities
Import the package in your project
import UtilityService from 'jitz-sharepoint-utilities/lib/services/UtilityService';
import UserService from 'jitz-sharepoint-utilities/lib/services/UserService';
import { IRepository } from 'jitz-sharepoint-utilities/lib/repositories/IRepository';
import Repository from 'jitz-sharepoint-utilities/lib/repositories/Repository';
export interface IProduct extends IModel{
}
Create the data model for the lists used on the project
import { IModel, IPersonOrGroup } from 'jitz-sharepoint-utilities/lib/common/IModels';
export interface IProduct extends IModel{
customFieldsInTheList:string;
....
....
}
Create the repository class for the list
import { IWebPartContext } from '@microsoft/sp-webpart-base';
import {IProduct} from '../yourModelFile';
import Repository from 'jitz-sharepoint-utilities/lib/repositories/Repository';
export default class ProductRepository extends Repository<IProduct> {
constructor(context: IWebPartContext,_listName:string="Products"){
super(context,_listName);
}
}
Use the repository
import { IRepository } from 'jitz-sharepoint-utilities/lib/repositories/IRepository';
private _productRepository:IRepository<IProduct>;
constructor(props: yourProps, state: yourState) {
super(props);
this.state = {
...
...
};
this._userService = new UserService(this.props.context);
this._productRepository = new ProductRepository(this.props.context);
}
getProducts = () => {
this._productRepository.getAll().then((data)=>{
...
...
});
}
Use User service
this._userService.getCurrentUser().then((user){});
this._userService.getCurrentUserGroups().then((groups){});
this._userService.checkIfCurrentUserInGroup(groupName).then((result){});
this._userService.checkIfUserIsInGroup(userId, groupName).then((result){});
this._userService.getUserById(id).then((user){});
SPFx Typescript in SharePoint components SharePoint