In a way this is part 2 on how to using PnP Modern Search to display days of celebration. In Using PnP Modern Search to display today's birthdays I showed how to display birthdays, so a natural progression is to show how to display work anniversaries.
I was really surprised how difficult this proved to be. I even discussed the topic with Mikael Svenson and Marc Anderson and I realized that I had to cheat in order for it to work.
The problem is that date manipulation in KQL is hard at best and sometimes impossible. In order to find a work anniversary I must compare Today and the Hiredate managed property, but ONLY the day and month part.
Calculating how many years the employee has been with the company is also required and I have NO idea how to get that using KQL.
Objective
- Employees having a work anniversary today, and also how many years they have been with the company
- Employees having a work anniversary within the next 7 days + number of years.
How to cheat
- The account
- The hiredate, but with the year segment being the current year
- The number of years the employee has been with company at the next anniversary
RefinableDate12=Today
RefinableDate12<{Today+7} AND RefinableDate12>{Today}
Implementation
Find the Content Type ID of the Content type created by the script.
Add a Results web part, name it "Todays work anniversaries"
Set the Query template to:
Contenttypeid:0x01009290F0FA40E7CB42B55D6D96F897262B* RefinableDate12=Today
(replace 0x01009290F0FA40E7CB42B55D6D96F897262B with the value for the your content type)
Add a Results web part, name it "Upcoming anniversaries (7 days)"
Set the Query template to:
Contenttypeid:0x01009290F0FA40E7CB42B55D6D96F897262B* RefinableDate12<{Today+7} AND RefinableDate12>{Today}
(replace 0x01009290F0FA40E7CB42B55D6D96F897262B with the value for your content type)
Add the managed properties birthdayhiredateAccountOWSUSER, nextWorkAnniversaryInYearsOWSTNMBR, RefinableDate10, RefinableDate12 to the Selected Properties in both web parts.
Enter Layout slots
Change UserEmail to use the birthdayhiredateAccountOWSUSER property
Change Date from Created to RefinableDate12
For both web parts you can select the People layout on "page 2" in the web part configuration.
Set Secondary text to:
{{{Title}}} has been with us {{{nextWorkAnniversaryInYearsOWSTNMBR}}} years
(remember to click Use Handlebars expression)
Set Tertiary text to:
{{getDate (slot item @root.slots.Date) "MMMM-D"}}
(remember to click Use Handlebars expression)
Set the sorting on the Upcoming Anniversaries to RefinableDate12 asc
Run the Add-UserDataToList function in a runbook or similar once a month and you should be done๐