Fun with PnP PowerShell

I had to do something with SharePoint lists today and had some fun with PnP PowerShell in the process. Here’s what I had to do. (What follows is a made up example).

I have a bunch of lists that contain the prices of an item per country. For instance, there’s 6 lists for 6 items.

Here’s what one of the lists looks like:

All 6 lists have the same contries, and the price of the item in that country.

What I wanted to do was have country specific lists that showed the price of the 6 items for that country. Initially I thought I might be able to do a lookup or something between lists, but that doesn’t work. Instead I decided to create new lists that are populated via PnP PowerShell.

To begin with, let’s get the names of the items and the countries. I do the first by finding all lists with “Item xx Prices” as the name. And for the latter I simply read all the Title field values from one of these lists.

Then I’d want to process each country. Check if there’s a list with that country name already, and if not create it. Then check if the list (new or existing) has a field called Price, and if not create it. And lastly go through each of the item lists, find the price of the item for that country, and add these as rows to the newly created list.

I did it this way (the snippet includes the two lookups I showed above too).

Now I can have this running periodically (I suppose I could look into triggering this whenever there’s a change to the item lists) and have the country lists up to date. Nice, huh! 😊

Here’s an example country list:

And here’s all the automatically created lists, with 6 items each: