Introduction
In this blog, we will learn to perform Create, Read and Update operations using factory methods in the SharePoint Framework (SPFx).
A Factory Pattern or Factory Method Pattern defines an interface or abstract class for creating an object, but lets the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.
The Advantage of Factory Design Pattern Factory Method Pattern is that it allows the sub-classes to choose the type of objects to create. It also promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class so that it will work with any classes that implement that interface or that extend the abstract class.
IFactory.ts
Install @microsoft/sp-http to your .ts file as shown below.
- npm install --save @microsoft/sp-http
- import * as pnp from "@microsoft/sp-http";
- import { SPHttpClient, SPHttpClientResponse } from "@microsoft/sp-http";
- export interface IFactory {
- getItems(requester: SPHttpClient, siteUrl: string, listName: string, params: string, oListItemID: string): Promise<any[]>;
- addItems(requester: SPHttpClient, siteUrl: string, listName: string, body:string): Promise<any[]>;
- updateItems(requester: SPHttpClient, siteUrl: string, listName: string, oListItemID: string, body:string): Promise<any[]>;
- }
Install Jquery and @microsoft/sp-http to your .ts file as shown below.
- npm install --save @microsoft/sp-http
- npm install --save jquery
- import * as pnp from "@microsoft/sp-http";
- import $ from "jquery";
- import { SPHttpClient, SPHttpClientResponse } from "@microsoft/sp-http";
- import { IFactory } from "../factory/IFactory";
- import $ from 'jquery';
- export class ListItemFactory implements IFactory {
- /*Get items from sharepoint action item list*/
- public async getItems(requester: SPHttpClient, siteUrl: string, listName: string, params: string, oListItemID: string): Promise<any[]> {
- try {
- return requester.get(`${siteUrl}/_api/web/lists/getbytitle('${listName}')/items(${oListItemID})?${params}`,
- SPHttpClient.configurations.v1,
- {
- headers: {
- "Accept": "application/json;odata=verbose",
- "odata-version": ""
- }
- })
- .then((response: SPHttpClientResponse) => {
- return response.json();
- })
- .then((json) => {
- return json;
- });
- } catch (error) {
- console.log(`Error occured in getItems method in ListFactory.ts. Error message: ${error.message}`);
- throw error;
- }
- }
- /*Insert items to sharepoint action item list*/
- public addItems(requester: SPHttpClient, siteUrl: string, listName: string, body:string): Promise<any[]>{
- try{
- return requester.post(`${siteUrl}/_api/web/lists/getbytitle('${listName}')/items`,
- SPHttpClient.configurations.v1,
- {
- headers: {
- "Accept": "application/json;odata=verbose",
- 'Content-type': 'application/json;odata=verbose',
- "odata-version": ""
- },
- body: body
- })
- .then((response: SPHttpClientResponse) => {
- return response.json();
- })
- .then((json) => {
- return(json);
- });
- }catch(error){
- console.log(`Error occured in addItems method in ListFactory.ts. Error message: ${error.message}`);
- throw error;
- }
- }
- /*Update items to sharepoint action item list*/
- public updateItems(requester: SPHttpClient, siteUrl: string, listName: string, oListItemID: string, body:string): Promise<any[]>{
- try {
- return requester.post(`${siteUrl}/_api/web/lists/getbytitle('${listName}')/items(${oListItemID})`,
- SPHttpClient.configurations.v1,
- {
- headers: {
- "Accept": "application/json;odata=verbose",
- 'Content-type': 'application/json;odata=verbose',
- "odata-version": "",
- 'IF-MATCH': '*',
- 'X-HTTP-Method': 'MERGE'
- },
- body: body
- })
- .then((response: SPHttpClientResponse): any => {
- return true;
- },(error: any): any => {
- return false;
- });;
- } catch (error) {
- console.log(`Error occured in updateItems method in ListFactory.ts. Error message: ${error.message}`);
- throw error;
- }
- }
- }

Join the conversation! Your thoughts help the community grow.