question

AniruddhaAditya-3867 avatar image
0 Votes"
AniruddhaAditya-3867 asked MichaelHan-MSFT commented

spfx webpart get List Items by specrific View

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

office-sharepoint-online
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AniruddhaAditya-3867 ,

Is there anything update? If my answer helps you, please click "Accept Answer" and upvote it :)

0 Votes 0 ·

1 Answer

MichaelHan-MSFT avatar image
1 Vote"
MichaelHan-MSFT answered

Hi @AniruddhaAditya-3867 ,

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.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.