Library components in SPFX

So, you have a couple of SPFX solutions and they all share some code. You’d like to put that code together, build a library and make those SPFX solutions consume it. SPFX 1.9.1 now officially includes library components and that’s exactly what it’s for!

I’ll walk you through a basic example where I build a library that gets some information from the current SharePoint site using @pnp/sp. Then another solution, a webpart, that doesn’t have any reference to pnp and just consumes the function that the library exposes. Let’s get started!

To start with, ensure you have the latest version of the sharepoint generator:

Once you have installed version 1.9.1, just go ahead and create a new SPFX solution.

  • when it asks you to skip tenant deployment, say yes
  • when it asks you to create an isolated webpart, say no
  • and when it asks you for the type of component, here you’ll see the new option, library

You’ll have created your first SPFX library. Check the manifest file, you’ll see the component type:

Let’s focus on creating our reusable class/method. In this case, I just took the default library that the generator created for me, added a reference to @pnp/sp and created a method that retrieves the web title:

Our library is now ready to use. Let’s focus on the consumer webpart. Start by adding a reference to your library:

You can get your library name and version from your library package.json file:

Go ahead and import the library in your code:

Ups! If you compile, you’ll get an error. It can’t find your library:

What’s going on here is that npm doesn’t know about your library, you need to link it. So, go to your library and run npm link:

Now go to the consumer webpart and link it to the library, by typing npm link and the library name:

Now your import statement will be recognized as valid. So, go ahead and consume the library from within your webpart. In my case, I’m just showing the web title to the end user:

Now, both your library and webparts are ready. You have these options:

  • Debug your webparts using the workbench. In this case, just bundle them and gulp serve. Note: ensure both components are using a different local server port, otherwise, only one will run successfully. Compare both serve.json files.
  • Debug your webparts by adding them to any page. In this case, package them and upload them to the app catalog. Same as in the previous scenario, ensure gulp serve is running for both on different ports
  • Or just package them with ship and upload them to the catalog.

In any case, there you go:

If you’re running your webpart and library in debug mode, you’re able to debug both of them:

If you shipped your code, you can see the request to library file to the app catalog (or the CDN):

Hope you find it useful!

Search WebParts in SharePoint modern pages

Just came across some news saying that PnP Modern Search components got updated to version 3.6.3, among other things it includes a details list template. So, thought it would be useful to share my experience with these set of PnP components that I leveraged for a client solution a couple of weeks ago.

As most of you know, the list of components available to modern pages is still short and one of the components I missed the most are those related to search. Modern pages still lack components that you can use to show search results and let users interact with them, such as content search webpart, refiners, search box, etc

The PnP team released this set of components called “Modern Search” webparts, that are the key replacement for them. In less than an hour, I built this page that displays search results having the same content type from different locations:

This suite comes with a set of components that I’ve used in this page, ready to work together:

  • Refiners panel, the one on the left, that lets you refine your search the same way the classic refiners panel did
  • The searchbox, that mimics the classic searchbox functionality
  • The search results, that is the main webpart and displays the search results
  • The pager, that you can configure from the search results one
  • Search navigation
  • Search verticals

They are simple to use. The key is on the “Search Results” one. Just edit its properties and you can configure it through a wizard.

In the first page, you cann connect it to the search box, the search refiner and the pager
In the second page, you can configure the query (you can use {searchterms} to reference the searchbox contents), sorting, sortable fields, select properties, items per page, etc
In the last page, you configure the styling options.

The coolest part is on the last page. There, you can choose different layouts (the latest version also includes a details list), but the most useful one is the custom. There, you edit your template inline!

In this case, I added styles and updated the html to just look like a list

Just go ahead, clone the repo, build the webparts and use them in your projects!