Sharepoint Framework Web Part filter list view

%3CLINGO-SUB%20id%3D%22lingo-sub-1715409%22%20slang%3D%22en-US%22%3ESharepoint%20Framework%20Web%20Part%20filter%20list%20view%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1715409%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20new%20SPFx%20webpart%20working%20on%20my%20SPO%20workbench.%20I%20have%20it%20reading%20an%20internal%20list%2C%20but%20I%20want%20to%20limit%20the%20number%20of%20items%20that%20show%20in%20the%20web%20part.%20Specifically%20only%20the%20latest%20item.%20I%20have%20a%20%22View%22%20built%20on%20the%20list%20that%20limits%20the%20number%20of%20items%20to%201%20and%20sorts%20it%20by%20created%20date%20so%20in%20my%20old%20(Classic)%20site%20it%20worked%20just%20fine%2C%20but%20now%20that%20I%20have%20to%20bulind%20a%20SPFx%20web%20part%20to%20get%20the%20design%20I%20want%20I%20can't%20get%20it%20to%20accept%20the%20views.%20I%20am%20trying%20this%20now%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fsp-dev-fx-property-controls%2Fcontrols%2FPropertyFieldViewPicker%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpnp.github.io%2Fsp-dev-fx-property-controls%2Fcontrols%2FPropertyFieldViewPicker%2F%3C%2FA%3E%26nbsp%3Band%20the%20filed%20works%2C%20but%20doesn't%20show%20my%20views.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThoughts%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1715498%22%20slang%3D%22en-US%22%3ERe%3A%20Sharepoint%20Framework%20Web%20Part%20filter%20list%20view%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1715498%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F809789%22%20target%3D%22_blank%22%3E%40Carchambault%3C%2FA%3E%26nbsp%3Bwhat%20do%20you%20mean%20by%20the%20field%20works.%20You%20mean%20the%20field%20is%20displaying%20without%20any%20views%20in%20the%20web%20part%20properties%20window%3F%20if%20that%20is%20the%20case%20make%20sure%20you%20pass%20the%20correct%20listid%20to%20the%20field.%20Also%2C%20if%20you%20can%20share%20some%20code%2C%20it%20would%20be%20helpful%20for%20the%20community%20to%20provide%20a%20best%20solution.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EHope%20it%20helps%2C%20please%20like%20it%20or%20mark%20it%20as%20a%20solution%20if%20it%20resolves%20your%20clarification%20or%20issue%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E-Sudharsan%20K...%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1722681%22%20slang%3D%22en-US%22%3ERe%3A%20Sharepoint%20Framework%20Web%20Part%20filter%20list%20view%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1722681%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F809789%22%20target%3D%22_blank%22%3E%40Carchambault%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EYou%20are%20using%20SPFx%20web%20part%20with%26nbsp%3B%3CSTRONG%3ENo%20Javascript%20Framework%3C%2FSTRONG%3E.%20The%20API%20URL%20you%20passed%20to%20retrieve%20the%20data%20is%20incorrect%20and%20please%20see%20the%20working%20copy%20of%20the%20code%20below.%20My%20list%20name%20is%26nbsp%3B%3CSTRONG%3EArea%3C%2FSTRONG%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eprivate%20_getListData()%3A%20Promise%3CANY%3E%20%7B%0A%20%20let%20apiUrl%3A%20string%20%3D%20this.context.pageContext.web.absoluteUrl%20%2B%20%60%2F_api%2Flists%2Fgetbytitle('Area')%2Fitems%60%3B%0A%20%20return%20new%20Promise((res%2C%20rej)%20%3D%26gt%3B%20%7B%0A%20%20%20this.context.spHttpClient.get(apiUrl%2C%20SPHttpClient.configurations.v1)%0A%20%20%20%20.then((respose%3A%20SPHttpClientResponse)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20res(respose.json())%3B%0A%20%20%20%20%7D)%3B%0A%20%20%7D)%3B%0A%20%7D%3C%2FANY%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3BInstead%20of%20using%20the%20default%26nbsp%3B%3CSTRONG%3ESPHttpClient%3C%2FSTRONG%3E%2C%20I%20recommend%20you%20to%20take%20a%20look%20at%20the%20%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpnpjs%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EPnP%20library%3C%2FA%3E.%20Below%20is%20the%20sample%20code%20using%20PnP%20library%20to%20fetch%20the%20same%20list%20data.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eprivate%20_getListDataUsingPnP()%20%7B%0A%20%20sp.web.lists.getByTitle('Area').items.select('Title').get()%0A%20%20%20.then(result%20%3D%26gt%3B%20%7B%0A%20%20%20%20console.log(%22Using%20PnPJs%3A%20%22%2C%20result)%3B%0A%20%20%20%7D)%3B%0A%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EUsing%20PnP%20library%2C%20the%20code%20would%20be%20clean%20and%20easy%20to%20read%20and%20understand%20by%20all%20the%20team%20members%20and%20there%20are%20more%20helpers%20available%20like%20select%2C%20filter%2C%20expand%20which%20I%20think%20is%20complicated%20implementing%20in%20the%20normal%26nbsp%3B%3CSTRONG%3ESPHttpClient%3C%2FSTRONG%3E%20option.%3C%2FP%3E%3CP%3EBoth%20the%20method%20returned%20the%20same%20results%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20371px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F222689i2FC3CAB9B820E81D%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3ETake%20a%20look%20at%20it%20and%20let%20me%20know%20if%20you%20have%20any%20clarifications.%3C%2FP%3E%3CP%3EHappy%20Coding...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EHope%20it%20helps%2C%20please%20like%20it%20or%20mark%20it%20as%20a%20solution%20if%20it%20resolves%20your%20clarification%20or%20issue%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E-Sudharsan%20K...%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I have a new SPFx webpart working on my SPO workbench. I have it reading an internal list, but I want to limit the number of items that show in the web part. Specifically only the latest item. I have a "View" built on the list that limits the number of items to 1 and sorts it by created date so in my old (Classic) site it worked just fine, but now that I have to bulind a SPFx web part to get the design I want I can't get it to accept the views. I am trying this now: https://pnp.github.io/sp-dev-fx-property-controls/controls/PropertyFieldViewPicker/ and the filed works, but doesn't show my views. 

 

Thoughts?

5 Replies

@Carchambault what do you mean by the field works. You mean the field is displaying without any views in the web part properties window? if that is the case make sure you pass the correct listid to the field. Also, if you can share some code, it would be helpful for the community to provide a best solution.

 

Hope it helps, please like it or mark it as a solution if it resolves your clarification or issue
-Sudharsan K...

@Sudharsan K Thank you for your reply!

 

Attached is my code and a screenshot of what is happening. Yes, I do believe I am missing the list ID, but have no idea where in the code it is not passing as the list information is there. 

 

import { Version } from '@microsoft/sp-core-library';
import { IPropertyPaneConfiguration } from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import { escape } from '@microsoft/sp-lodash-subset';
import styles from './CeoCornerWebPart.module.scss';
import * as strings from 'CeoCornerWebPartStrings';
import { SPHttpClientSPHttpClientResponse } from '@microsoft/sp-http';
import { EnvironmentEnvironmentType } from '@microsoft/sp-core-library';
import { PropertyFieldViewPickerPropertyFieldViewPickerOrderBy } from '@pnp/spfx-property-controls/lib/PropertyFieldViewPicker';

export interface ICeoCornerWebPartProps {
  liststring;
  viewsstring | string[];
}
export interface ISPLists 
{
  valueISPList[];
}

export interface ISPList 
{
  Titlestring;
}

export default class CeoCornerWebPart extends BaseClientSideWebPart<ICeoCornerWebPartProps> {

  private _getListData(): Promise<ISPLists>
  {
    return this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + "https://hcfhawaii.sharepoint.com/sites/Hookahi5/Lists/CEOCorner/Homepage.aspx",SPHttpClient.configurations.v1)
       .then((responseSPHttpClientResponse=> 
       {
       return response.json();
       });
   }
   private _renderListAsync(): void
   {
    if (Environment.type == EnvironmentType.SharePoint || 
             Environment.type == EnvironmentType.ClassicSharePoint) {
     this._getListData()
       .then((response=> {
         this._renderList(response.value);
       });
}

 }
   private _renderList(itemsISPList[]): void 
   {
    let htmlstring = '<div>';
    items.forEach((itemISPList=> {
      html += `
      <div>${item.Title}</div>
          `;
    });

    html += '</div>';

    const listContainerElement = this.domElement.querySelector('#spListContainer');
    listContainer.innerHTML = html;
  }

  public render(): void {
    this.domElement.innerHTML = `
      <div id="${ styles.ceocornerheader }">
       <div id="spListContainer" />
      </div>`;

      this._renderListAsync();
  }

  protected get dataVersion(): Version {
    return Version.parse('1.0');
  }

  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          groups: [
            {
              groupFields: [
                  PropertyFieldViewPicker('view', {
                  label: 'Select a view',
                  listId: this.properties.list,
                  selectedView: this.properties.views,
                  orderBy: PropertyFieldViewPickerOrderBy.Title,
                  disabled: false,
                  onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
                  properties: this.properties,
                  context: this.context,
                  onGetErrorMessage: null,
                  deferredValidationTime: 0,
                  key: 'viewPickerFieldId'
                })
              ]
            }
          ]
        }
      ]
    };
  }
}

 

 

Hi @Carchambault,

You are using SPFx web part with No Javascript Framework. The API URL you passed to retrieve the data is incorrect and please see the working copy of the code below. My list name is Area

private _getListData(): Promise<any> {
		let apiUrl: string = this.context.pageContext.web.absoluteUrl + `/_api/lists/getbytitle('Area')/items`;
		return new Promise((res, rej) => {
			this.context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1)
				.then((respose: SPHttpClientResponse) => {
					res(respose.json());
				});
		});
	}

 Instead of using the default SPHttpClient, I recommend you to take a look at the PnP library. Below is the sample code using PnP library to fetch the same list data.

private _getListDataUsingPnP() {
		sp.web.lists.getByTitle('Area').items.select('Title').get()
			.then(result => {
				console.log("Using PnPJs: ", result);
			});
	}

Using PnP library, the code would be clean and easy to read and understand by all the team members and there are more helpers available like select, filter, expand which I think is complicated implementing in the normal SPHttpClient option.

Both the method returned the same results

image.png

Take a look at it and let me know if you have any clarifications.

Happy Coding...

 

Hope it helps, please like it or mark it as a solution if it resolves your clarification or issue
-Sudharsan K...

@Sudharsan K Thank you again. This is beginning to make more sense. Here is the copy of my current code. I think I am missing an authentication now? When I add your sample code I get an error on the 

 

import { Version } from '@microsoft/sp-core-library';
import { IPropertyPaneConfiguration } from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import { escape } from '@microsoft/sp-lodash-subset';
import { PropertyFieldViewPickerPropertyFieldViewPickerOrderBy } from '@pnp/spfx-property-controls/lib/PropertyFieldViewPicker';
import styles from './Ceo2WebPart.module.scss';
import * as strings from 'Ceo2WebPartStrings';

export interface ICeo2WebPartProps {
  descriptionstring;
  listsstring;
  viewsstring
}

export default class Ceo2WebPart extends BaseClientSideWebPart<ICeo2WebPartProps> {

  private _getListDataUsingPnP() {
    sp.web.lists.getByTitle('CEOCornera').items.select('Title').get()
      .then(result => {
        console.log("Using PnPJs: "result);
      });
  }

  public render(): void {
    this.domElement.innerHTML = `
      <div class="${ styles.ceo2 }">
        <div class="${ styles.container }">
          <div class="${ styles.row }">
            <div class="${ styles.column }">
              <span class="${ styles.title }">Welcome to SharePoint!</span>
              <p class="${ styles.subTitle }">Customize SharePoint experiences using Web Parts.</p>
              <p class="${ styles.description }">${escape(this.properties.description)}</p>
              <a href="https://aka.ms/spfx" class="${ styles.button }">
                <span class="${ styles.label }">Learn more</span>
              </a>
            </div>
          </div>
        </div>
      </div>`;
  }

  protected get dataVersion(): Version {
    return Version.parse('1.0');
  }

  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {

    return {
        pages: [
          {
            header: {
              description: strings.PropertyPaneDescription
            },
            groups: [
              {
                groupName: strings.BasicGroupName,
                groupFields: [
                PropertyFieldViewPicker('views', {
                  label: 'Select a view',
                  listId: this.properties.lists,
                  selectedView: this.properties.views,
                  orderBy: PropertyFieldViewPickerOrderBy.Title,
                  disabled: false,
                  onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
                  properties: this.properties,
                  context: this.context,
                  onGetErrorMessage: null,
                  deferredValidationTime: 0,
                  key: 'viewPickerFieldId'
                })
                ]
              }
            ]
          }
        ]
      };
    }
}

Hi @Carchambault 

I am so sorry that I missed the code that had to be added to the imports before that have you installed the PnP npm package? if not use the below command to install it and add the import statement mentioned below. After that, your code should work without any errors.

npm install @pnp/sp --save
import { sp } from "@pnp/sp";