Skip to content

List Operations

Sumit Kanchan edited this page Aug 11, 2020 · 3 revisions

SharePoint Online List Operations

In this, I will explain the methods and their use in the SPListOperations Class. To install the package do

> npm i spfxhelper

And then import the following in the code file

import { SPListOperations } from 'spfxhelper';

getInstance()

  • This method was earlier used to create the object of the class as then singleton was implemented.
  • Now that has been removed and this method does not hold much meaning as it now just creates a new object. Instead, you can use the new for the same
let lstOps:SPListOperations = SPListOperations.getInstance(spHttpClient, webUrl, logSource);
let lstOps:SPListOperations = new SPListOperations(spHttpClient, webUrl, logSource);

Parametrs

  • spHttpClient: object to query SharePoint
  • webUrl: Url on which site query needs to be executed
  • logSource: Logging can be found under same category

Return type: SPListOperation


getListBytitle()

  • This method returns the list details based on the title passed
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let listData = await listOps.getListByTitle("Sample");

Parameter

  • Title: Title of the target list

Return Type: IListGet

{
 ok: boolean,     // If the query execution is ok
 exists: boolean, // If the list already exists
 details?:any,    // Details of the created/existing list, will be undefined in case of error
 error?: Error    // Error if it occurred, will be undefined in case of success
}

createList()

  • This method returns the list details based on the title passed
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let listData = await listOps.createList({ allowContentTypes: true, allowFolder: true, baseTemplate: BaseTemplate.GenericList, description: `this is the sample list`, title: `sample` });

Parameter

  • title: Title of the target list
  • allowContentTypes: Do you want to allow Content Types
  • allowFolder: Do you want to allow create Folder
  • baseTemplate: Base template of the list of Type BaseTemplate
  • description: Description of the list

Return Type: IListGet

{
 ok: boolean,     // If the query execution is ok
 exists: boolean, // If the list already exists
 details?:any,    // Details of the created/existing list, will be undefined in case of error
 error?: Error    // Error if it occured, will be undefined in case of success
}

getListItemsByQuery()

  • This method returns the list items based on the query
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let listData = await listOps.getListItemsByQuery('Sample','?$select=title');

Parameter

  • title: Title of the target list
  • query: Query on which basis items will be retrieved

Return Type: IListItemsResponse

{
 ok: boolean,        // If the query execution is ok
 result?: any[],      // result array (expecting that this will return multiple items, undefined in case of an error
 error?: Error,      // Error if it occurred, will be undefined in case of success
 nextLink?: string   // next link for the query, if it is returned
}


getListItemsByNextLink()

  • This method returns the results based on the 'nextLink' retrieved from the previous query. Can be utilized while retreiving all items recursively
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let listData = await listOps.getListItemsByNextLink('<NEXT LINK>');

Parameter

  • nextLink: 'nextLink' retrieved from the previous query

Return Type: IListItemsResponse

{
 ok: boolean,        // If the query execution is ok
 result?: any[],      // result array (expecting that this will return multiple items, undefined in case of an error
 error?: Error,      // Error if it occurred, will be undefined in case of success
 nextLink?: string   // next link for the query, if it is returned
}

getListItemByID()

  • This method returns the list item based on the item id
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let listData = await listOps.getListItemsByQuery('Sample','2');

Parameter

  • title: Title of the target list
  • ID: Item id which needs to be retrieved

Return Type: IListItemResponse

{
 ok: boolean,        // If the query execution is ok
 result?: any,       // result, undefined in case of an error.
 error?: Error,      // Error if it occurred, undefined in case of success
}

getListItems()

  • This method returns the list items based on the query
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let listData = await listOps.getListItems('Sample',10);

Parameter

  • title: Title of the target list
  • Row Count: No. of items to be retrieved

Return Type: IListItemsResponse

{
 ok: boolean,        // If the query execution is ok
 result?: any[],      // result array (expecting that this will return multiple items, undefined in case of an error.
 error?: Error,      // Error if it occurred, will be undefined in case of success
 nextLink?: string   // next link for the query, if it is returned
}

createListItem()

  • This method creates the list item
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.createListItem("Sample", [{ fieldName: 'Title', fieldValue: 'Title' }]);

Parameter

  • title: Title of the target list
  • Item values: Array of Field name and Field Value

Return Type: IListItemResponse

{
 ok: boolean,        // If the query execution is ok
 result?: any,       // result, undefined in case of an error.
 error?: Error,      // Error if it occurred, will be undefined in case of success
}

createFolderInDocLib()

  • This method creates the folder in the document library
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.createFolderInDocLib("Shared Documents", "Test SPFx");

Parameter

  • title: Internal name of the target document library
  • Folder Name: Name of the folder

Return Type: IListItemResponse

{
 ok: boolean,        // If the query execution is ok
 result?: any,       // result, undefined in case of an error.
 error?: Error,      // Error if it occurs, will be undefined in case of success
}

createFolderInList()

  • This method creates the folder in the list
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.createFolderInList("Sample", "Test SPFx");

Parameter

  • title: Internal name of the target list
  • Folder Name: Name of the folder

Return Type: IListItemResponse

{
 ok: boolean,        // If the query execution is ok.
 result?: any,       // result, undefined in case of an error.
 error?: Error,      // Error if it occurs, will be undefined in case of success.
}

updateListItem()

  • This method updates the list item
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.updateListItem("Sample", 2, [{ fieldName: 'Title', fieldValue: 'Title New' }]);

Parameter

  • title: Internal name of the target list
  • Item Id: ID of the item that needs to be updated
  • Item Values: Array of field values that needs to be updated

Return Type: IListItemResponse

{
 ok: boolean,        // If the query execution is ok.
 result?: any,       // result, undefined in case of an error.
 error?: Error,      // Error if it occurs, will be undefined in case of success.
}

getListMetadata()

  • This method returns the request object for the creation of the list
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.getListMetadata({ title: 'Sample', allowContentTypes: false, allowFolder: true, baseTemplate: BaseTemplate.GenericList, description: 'This is the sample list' }]);

Parameter

  • lstDetail: Details of the list that needs to be created

Return Type: ISPPostRequest

{
 url: string,        // url 
 body: string,       // payload of the request
}

getListsDetailsByBaseTemplateID()

  • This method returns the lists with the matching base template
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.getListsDetailsByBaseTemplateID(BaseTemplate.GenericList);

Parameter

  • baseTemplate: Base Template of the list of type enum 'BaseTemplate'

Return Type: ISPBaseResponse

{
 result: any,        // result from the query, will be an array of items
 ok: boolean,        // response from the query
 error: Error        // Error object if any occurred else undefined
}

getViewsByList()

  • This method returns the views created in a list
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.getViewsByList("Sample");

Parameter

  • listTitle: Title of the list

Return Type: IListViews

{
 views: any[],        // result from the query, will be an array of items
 ok: boolean,        // response from the query
 error: Error        // Error object if any occurred else undefined
}

getDefaultView()

  • This method returns the default view of a list
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.getDefaultView("Sample");

Parameter

  • listTitle: Title of the list

Return Type: IListViews

{
 views: any,        // result from the query, will be a single object
 ok: boolean,        // response from the query
 error: Error        // Error object if any occurred else undefined
}

getContentTypesByList()

  • This method returns the content types associated with a list
let listOps: SPListOperations = new SPListOperations(this.props.spHttpClient, this.props.weburl, "SPFX");
let items = await listOps.getContentTypesByList("Sample");

Parameter

  • listTitle: Title of the list

Return Type: ISPBaseResponse

{
 result: any,        // result from the query, will be an array of items
 ok: boolean,        // response from the query
 error: Error        // Error object if any occurred else undefined
}