-
Notifications
You must be signed in to change notification settings - Fork 3
List Operations
Sumit Kanchan edited this page Aug 11, 2020
·
3 revisions
In this, I will explain the methods and their use in the SPListOperations Class. To install the package do
> npm i spfxhelperAnd then import the following in the code file
import { SPListOperations } from 'spfxhelper';- 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);- spHttpClient: object to query SharePoint
- webUrl: Url on which site query needs to be executed
- logSource: Logging can be found under same category
- 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");- Title: Title of the target list
{
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
}- 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` });- 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
{
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
}- 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');- title: Title of the target list
- query: Query on which basis items will be retrieved
{
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
}- 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>');- nextLink: 'nextLink' retrieved from the previous query
{
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
}- 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');- title: Title of the target list
- ID: Item id which needs to be retrieved
{
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
}- 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);- title: Title of the target list
- Row Count: No. of items to be retrieved
{
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
}- 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' }]);- title: Title of the target list
- Item values: Array of Field name and Field Value
{
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
}- 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");- title: Internal name of the target document library
- Folder Name: Name of the folder
{
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
}- 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");- title: Internal name of the target list
- Folder Name: Name of the folder
{
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.
}- 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' }]);- 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
{
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.
}- 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' }]);- lstDetail: Details of the list that needs to be created
{
url: string, // url
body: string, // payload of the request
}- 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);- baseTemplate: Base Template of the list of type enum 'BaseTemplate'
{
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
}- 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");- listTitle: Title of the list
{
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
}- 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");- listTitle: Title of the list
{
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
}- 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");- listTitle: Title of the list
{
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
}