Showing posts with label Custom Site Template. Show all posts
Showing posts with label Custom Site Template. Show all posts

February 21, 2025

Automate SharePoint Site Creation with PowerShell and Power Automate

Introduction:

Creating and managing SharePoint sites quickly and easily is important for any organization. By using PowerShell and Power Automate, you can automate site creation and include useful workflows in your site templates. This guide will show you how to create a custom SharePoint site template that makes settings up new sites simple and consistent.


Key Features of This Approach:

  • Power Automate Integration: Automatically run workflows whenever a new site is created.

  • Custom Site Scripts: Use simple scripts to define how your sites look and work.

  • Easy site Management: Set up themes, time zones, and site logos with just a script.

  • Workflow Automation: Automatically send notifications or track site creation details using workflows.


Benefits:

  • Save Times: Automates repetitive work.
  • Keeps Things Consistent: Make sure every new site follows the same setup and rules.
  • Scalable: Handles creating lots of sites without manual work.
  • Easy to Customize: You can change templates anytime to match your needs.

Steps to Implement

1. Setup a Power Automate Flow:

     Power Automate will handle workflows triggered during the site creation process.

     Steps to create the flow: 

        1. Go to Power Automate.

        2. Click "New Flow" > "Automated Cloud Flow".
         

        3. Name the flow (e.g., "PowerShell through calling a Power Automate") and click "Skip".
        

        4. Add the "When an HTTP request is received" trigger.
        

        5. Configure the trigger settings.

                a. Under "Who can trigger the flow?" select "Anyone".
                

                b. Add the following Request Body JSON Schema.

    {
      "type": "object",
      "properties": {
           "type": {
                "type": "string"
            },
            "properties": {
                "type": "object",
                "properties": {
                    "webUrl": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        }
                    },
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string"
                            },
                            "properties": {
                                "type": "object",
                                "properties": {
                                    "siteName": {
                                        "type": "object",
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        }
                                    },
                                    "product": {
                                        "type": "object",
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "webDescription": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string"
                                }
                            }
                        },
                        "creatorName": {
                            "type": "object",
                            "properties": {
                                "type": {
                                    "type": "string"
                                }
                            }
                        },
                        "creatorEmail": {
                            "type": "object",
                            "properties": {
                                "type": {
                                    "type": "array",
                                    "items": {
                                        "type": "string"
                                    }
                                }
                            }
                        },
                        "createdTimeUTC": {
                            "type": "object",
                            "properties": {
                                "type": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            }
        }

        6. Add any additional actions, such as sending an email, creating a list or library, or creating a log.

        7. Save the flow to automatically generate the HTTP POST URL. Copy this URL for use later.
         

2. Write a Site Script:

      A site script defines the actions SharePoint will execute when the template is applied.

      Steps to create the script:

            1. Open a text editor (e.g. Notepad++ or Visual Studio Code).

            2. Copy and customize the following JSON structure.

       $JSONScript = @"
                   {
                          "$schema": "schema.json",
                           "actions": [
                              {
                                  "verb": "applyTheme",
                                  "themeName": "Custom Theme"
                               },
                              {
                                  "verb": "setRegionalSettings",
                                  "timeZone": 24,
                                  "sortOrder": 25,
                                  "hourFormat": "12"
                              },
                             {
                                  "verb": "setSiteLogo",
                                  "url": "/Style Library/logo.jpg"
                              },
                             {
                                  "verb": "createSPList",
                                  "listName": "Work",
                                  "templateType": 101,
                                  "subactions": [
                                        {
                                              "verb": "setTitle",
                                              "title": "Work"
                                        }
                                    ]
                              },
                             {
                                  "verb": "triggerFlow",
                                  "url": "PASTE_YOUR_FLOW_URL_HERE",
                                  "name": "PowerShell through calling Flow",
                                  "parameters": {
                                          "siteName": "Demo site",
                                          "product": "SharePoint Online"
                                   }
                             }
                         ],
                    "bindata": {},
                   "version": 1
                  }
                "@

            3. Replace PAST_YOUR_FLOW_URL_HERE with the URL copied from your Power Automate flow.      


3. Create a Site Template using PowerShell:

    PowerShell allows you to upload and apply the site script to create a template.

    Steps to run the script:

        1. Open PowerShell as an administrator.

        2. Connect to SharePoint Online.

        Connect-SPOService –Url https://your-tenant-name-admin.sharepoint.com

        3. Add the site script

        $JSONScript = Get-Content -Raw -Path “PathTo\SiteScript.json”

        $SiteScript = $JSONScript | Add-SPOSiteScript -Title “Demo Site Script”

        4. Create the site design.

        Add-SPOSiteDesign -Title “Site Template1” -WebTemplate 68 -SiteScripts $SiteScript.ID -Description “Site Template1”

        5. Web Template: User 68 for Communication Sites, 64 for Team Sites, or 1 for Team Site without
             Group
.
        

        6. Disconnect from SharePoint Online   

                    Disconnect-SPOService

4. Create a New Site:

        Use the SharePoint Admin Center to create a new site with the custom template.

        Steps to create the site:

            1. Navigation to "https://your-tenant-name-admin.sharepoint.com"

            2. Select Active Sites > Create.
              

            3. Choose the site type (e.g. Communication site).
              

            4. Under Template, select Your Organization > Your custom template and click on  "Use Template".
               

               

            5. Enter the required site details.
                    a. Site Name: Provide a site name (e.g. "Demo Communication Site").
                    b. Site Description: Add a brief description.
                    c. Site Owner: Specify the owner.
               

            6. Click Create Site and wait for provisioning to complete.


5. Test the Automation:

        1. Open the newly created site.

        2. Verify the following:

                a. The applied theme and regional settings.
                b. The uploaded site logo.
                c. The created SharePoint list.
                d. The triggered Power Automate workflow.          


6. Manage Templates:

    To remove an existing template:

        1. Reconnect to SharePoint Online in PowerShell.                
        Connect-SPOService -Url https://your-tenant-name-admin.sharepoint.com

        2. Get the list of site designs.              
        Get-SPOSiteDesign
        

        3. Delete the template by ID.     
        Remove-SPOSiteDesign -Identity “TemplateID
                  


Additional Tips for Beginners:

      1. Test Before Deployment: Always test in a development environment.

      2. Error Handling: Enable logging in Power Automate for debugging issues.

      3. Template Naming: Use meaningful full names for scripts and templates to avoid confusion.

      4. Keep Scripts Modular: Break down complex actions into smaller scripts for easier management.


Advantages:

    1. Automates complex workflows during site creation.

    2. Centralizes configurations, making maintenance easier.

    3. Enhances productivity by reducing administrative overhead.


Pros and Cons

Pros

    1. Consistent site creation process.

    2. Fully customizable based on organization needs.

    3. Supports integration with other services.


Cons

    1. Initial setup requires scripting expertise.

    2. Debugging errors may require deeper knowledge.

    3. Depends on Power Automate flow reliability.


Additional Tips

    a. Test the JSON Schema and PowerShell Scripts in a development environment before deploying.

    b. Use descriptive names for site templates to avoid confusion.

    c. Regularly review and update site scripts to align with organizational changes.


Conclusion

Automating SharePoint site creation using PowerShell and Power Automate transforms how you manage site provisioning. This approach enhances efficiency, consistency, and scalability, allowing IT administrators o focus on strategic initiatives. By following this guide, you can create Powerful site templates integrated with workflows that meet your organization's needs.

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.