Showing posts with label PnP Commands. Show all posts
Showing posts with label PnP Commands. Show all posts

August 14, 2024

Harnessing SharePoint OOTB Events with PnP Commands for Automated Permission Updates & Efficient Operations

Introduction

In SharePoint Online, automating operations in response to internal events — such as the creation of new site groups or granting permissions — can significantly streamline administrative tasks. This is particularly vital in environments with multiple associated sites where permissions need to be synchronized regularly. Here's how we can use SharePoint's OOTB events to improve operational efficiency and ensure timely updates across sites.

Challenges in Synchronizing Permissions

Consider a typical scenario in a SharePoint environment with one hub site and several associate sites. When a new user is added to a group in the hub site, the same user must be added across all associated sites. 

If we use the Logic App or Power Automate to sync the permission across all these sites it would require daily SharePoint group checks, leading to permissions synchronization delays.

Innovative Solution Using PnP Commands

To address this delay, we leveraged SharePoint PnP (Patterns and Practices) commands to trigger Flows or Azure Functions directly via HTTP when specific OOTB events occur. This method provides immediate event details such as usernames and group names, which can then be used to synchronize user permissions across sites instantly.

Setup and Configuration

We set up event receivers in SharePoint that respond to specific user group changes. Here are the PnP PowerShell commands used to configure these event receivers:

1. When a User is Added:

 Add-PnPEventReceiver -Name "GroupUserAdded" -Url $functionUrlToAddUser -EventReceiverType GroupUserAdded -Synchronization Asynchronous  

2. When a User is Removed:

 Add-PnPEventReceiver -Name "GroupUserDeleted" -Url $functionUrlToRemoveUser -EventReceiverType GroupUserDeleted -Synchronization Asynchronous  

These commands integrate two Azure Functions: the first triggers when a user is added to any SharePoint group on the Hub site, and the second when a user is removed. This setup ensures that any modifications to user groups are immediately reflected across all associated sites without the need for manual intervention.

Benefits and Capabilities

By leveraging OOTB events, we can perform a variety of operations that respond dynamically to changes within the SharePoint environment. These include but are not limited to:

  • Adding, updating, or deleting items, lists, and sites
  • Managing group and role definitions and assignments
  • Handling file movements and attachment operations

Conclusion

Utilizing SharePoint's OOTB events with PnP commands to trigger automated actions eliminates delays inherent in daily synchronization tasks. This approach not only enhances operational efficiency but also improves user experience by ensuring that permissions and other configurations are consistently up-to-date. This strategy ensures more efficient management of SharePoint systems, minimizing delays and boosting overall operational effectiveness.

 If you have any questions you can reach out our SharePoint Consulting team here.

December 2, 2021

[Issue Resolved]: "Specified argument was out of the range of valid values" while executing Add-PnPSiteScript and Add-PnPSiteDesign PowerShell commands

Introduction

Recently, we implemented a SharePoint site design using a PowerShell script for a Postal and Parcel company based out of Melbourne, Victoria, Australia. We encountered an issue while applying the site script and site design during the execution of  Add-PnPSiteScript and Add-PnPSiteDesign commands.


In this blog, we will learn about the issue and its solution that we can implement while creating a new site design template using the PowerShell script. 

Issue

    • We normally use the Add-PnPSiteScript and Add-PnPSiteDesign commands to add a new Site Script and Site Design on a SharePoint tenant. 
    • When we execute these commands frequently on the same tenant, a new Site Script is being created every time. 
    • And after some time, we encounter these below issues during the execution of the PowerShell script. 

    Image 1


    • It says “Specified argument was out of the range of valid values”. 

    Solution

      This issue is occurring because there is a tenant level limitation in SharePoint that we can create max of 100 Site Scripts and 100 Site Designs templates.

      • To resolve these issues, first of all, we need to remove some of the unused Site Scripts and Site Designs so that we can add our new Site Scripts and Site Designs. 
      • To remove a Site Script and Site Design, we will be using the below commands. 

       Remove-PnPSiteScript -Identity $scriptID -Force  
       Remove-PnPSiteDesign -Identity $designID -Force  
      
      • Here the $scriptID & the $designID is the ID of the existing Site Script and Site Design that is been available at the SharePoint tenant level.
      • The –Force in the Remove command is used to confirm the execution of this command without asking for confirmation to remove for its execution.
      • We cannot view these IDs directly from the SharePoint tenant, so we will be using the below commands to retrieve the details of these IDs.

       Get-PnPSiteScript  
       Get-PnPSiteDesign   
      

      • The Get-PnPSiteScript & the Get-PnPSiteDesign commands will retrieve the available Site Scripts and Site Design details and we will extract these details, identify unused Site Script/Site Design and will be using them for removing the existing unused Site Design and Site Scripts.
      • And then, it should allow creating your new Site Design and Site Script by executing the Add-PnPSiteScript and Add-PnPSiteDesign commands.

      Conclusion

      This is how we can resolve the "Specified argument was out of the range of valid values" error in the PowerShell script. Hope this helps, good day! 

      If you have any questions you can reach out our SharePoint Consulting team here.