spfx webpart get List Items by specrific View

Aniruddha Aditya 316 Reputation points
2021-03-20T12:49:20.14+00:00

Hi,

Though a simple question, I am not able to achieve it yet. I want to have an spfx web part triggers a CAML Query from a Specific view. So that later if I add or remove columns, I should not be changing the code. So basically I don't want to hard code any field names, just dynamically pull all the items if a filter condition is met and bind it to a data table.

Didn't find any way to filter by a specific View for example: All Items.

Thanks
Ani

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,682 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-03-22T06:56:05.447+00:00

    Hi @Aniruddha Aditya ,

    There is no direct way to get the list items by specific view, you need to get the ViewQuery for this view. Then use the ViewQuery in CAML Query to get the listitems.

    Below is my demo code for you:

    import { sp } from "@pnp/sp";  
    import "@pnp/sp/webs";  
    import "@pnp/sp/lists";  
    import "@pnp/sp/views";  
    import "@pnp/sp/items";  
      
     private async getListViewQuery(){  
      let list=sp.web.lists.getByTitle("Test1");  
      let view=await list.views.getByTitle("View1").select("ViewQuery")();  
      console.log(view.ViewQuery);  
      let xml = '<View><Query>' + view.ViewQuery + '</Query></View>';   
      let items= await list.getItemsByCAMLQuery({'ViewXml':xml});  
      console.log(items);  
     }  
    

    Reference: https://sharepoint.stackexchange.com/questions/264455/how-get-sharepoint-list-items-by-views-in-spfx


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments