Showing posts with label SharePoint Online. Show all posts
Showing posts with label SharePoint Online. Show all posts

Tuesday, 10 June 2025

Get SharePoint search results using the Rest API in PowerAutomate

In this blog, we will be learning about how to get SharePoint search results using the Rest API through PowerAutomate flow. The search results will be exported into a CSV file and saved into a SharePoint library. 

Create Flow

1). Sign in to Power Automate

2). Open the flow, click on the Create option, and select "Instant Cloud Flow".

Enter Flow name.
Choose the flow trigger as "Manually trigger a flow".
Click on the Create button.



3). Add an action "Send an HTTP request to SharePoint".


Site Address
https://yoursite.sharepoint.com/sites/VideoHub
Method
GET
Uri
_api/search/query
?querytext='Path:https://yoursite.sharepoint.com/sites/VideoHub*'
&trimduplicates=false&rowlimit=500
&selectproperties='Title,RefinableString00,RefinableInt00,
                   IsDiscontinued,RefinableString03'
&refiners='contentclass,SecondaryFileExtension,ListID'
&refinementfilters='and(
    contentclass:equals("STS_ListItem_DocumentLibrary"),
    SecondaryFileExtension:or("mp4","mkv"),
    ListID:equals("f7fb8ca6-aaaa-bbbb-cccc-7816d24bdf42")
)'
&sortlist='Created:descending'
Headers
{
  "accept": "application/json; odata=nometadata"
}


4). Add an action "Parse JSON" and enter output of previous step in Content.
In the Schema field use the below JSON content.


Parse JSON - Schema

		

Add additional type null - this will take care of the null values coming in search results.


5). Initialize variables one of type Object and Array.


		


6). Add "Apply to each" loop for rows and enter step 4 output (or enter below) as input value.

body('ParseJSONtoGetRows')['PrimaryQueryResult']['RelevantResults']['Table']['Rows']

Add another nested "Apply to each" loop for cells and enter current cells value (or enter below) as input value.

items('Apply_to_each-Rows')['Cells']


7). Add a "Switch" condition and enter current cell key value (or enter below) as On value.

items('Apply_to_each-Cells')?['Key']


8). Add a case statement to fetch field value from the search property.

Enter search managed property Title in equals field.

Add an action "Compose" and add below value in the inputs parameter.

setProperty(variables('varResponseJson'),'Title',items('Apply_to_each-Cells')?['Value'])

Here, setProperty(object, property, value) function is used; it returns an object with a property set to the provided value.

Add an action "Set variable" (defined in step 5) object variable and enter previous step output as input value.

9). Add more case statements; we are fetching five field values from the search properties. Therefore, five case statements are needed.
Repeat the step 8 for all the required search managed property.

Note: Create search managed properties for your custom fields and use them.




10). Append the array variable (defined in step 5) and enter object variable output as value after closing on inner "Apply to each" loop.


11). Add an action "Create CSV table" and enter array variable in From and Automatic in Columns.


12). Add an action "Create file" to create CSV file to export the search results.

Enter the Site Address and Folder Path as required.

File Name as concat('BlogData_', formatDateTime(utcNow(), 'yyyyMMdd_HHmmss'), '.csv')

File Content as output from previous step 12.



I hope this will help all of you! 🧲
Feel free to provide feedback.


🚀 "Happy Coding" 🚀

Wednesday, 20 December 2023

SharePoint site theme: Add a custom theme using PowerShell

In this blog, we will be learning how to create a custom theme for SharePoint Online.

Now SharePoint has a Site theming feature for applying custom styles and colors to the site. There are six light and two dark themes available by default. This feature provides the ability to define custom themes and make them available to site owners. Creating a custom theme in SharePoint is a quick and easy way to give your site a unique look and feel.

Currently, no theme is available under the "From your organization" option.


The steps to add a custom theme to SharePoint Online.

Step 1: Create a new color theme in SharePoint Online.

Themes are defined in a JSON schema that stores color settings and related metadata for each theme. Create a new custom theme for SharePoint Online using Microsoft Fluent UI Theme Designer, an online tool. Customize the theme as per your needs and click on the Export theme button. Copy and save the PowerShell output for later use.

Light Theme sample:


Dark Theme sample:


Step 2: Add a custom theme to SharePoint Online using PowerShell

First, connect to the SPO admin site using the Connect-SPOService cmdlet. The custom theme can be added to the SharePoint online tenant using the Add-SPOTheme cmdlet. This cmdlet creates a new theme or updates an existing theme. The color palette settings can be passed as either a hash table or a dictionary.

Execute the command for the light theme.



Now go to Site Settings, gear icon (⚙️) > Change the look > Theme and you'll find the newly added color theme listed under "From your organization".


Execute the same command for the dark theme.


Again, go to Site Settings gear icon (⚙️) > Change the look > Theme and you'll find the new added color theme listed under "From your organization".


When you choose a theme, color settings are instantly applied to the site so that you can see what the selected theme will look like. Select the desired theme, click Save to save your selection, or choose Cancel to revert to your current theme.

Our "Maroon - Light theme" output looks like




Our "Orange - Dark theme" output looks like




Update SP Theme
To update an existing theme (to modify some of its color settings, for example), add the -Overwrite flag to the Add-SPOTheme cmdlet.


I hope this will help all of you! 🧲
Feel free to provide feedback.


🚀 "Happy Coding" 🚀

Wednesday, 6 December 2023

Create List templates using Site template and Site script

In this blog, we will be learning how to create list templates under the tab From your organization.

When people in the organization create new lists, they need a template that can be used each time a new list is created.

Site Script

A site script is a collection of actions that SharePoint runs when creating a new site or list. Actions describe changes to apply to the new list, such as creating new columns, adding views, or content types. The actions are specified in a JSON script, which is a list of all actions to apply. When a script runs, SharePoint completes each action in the order listed.

Site Template (previously known as Site Design)

Site templates can be used each time a new site is created to apply a consistent set of actions. Site templates created using custom site scripts will display in the From your organization tab.

A site script is a JSON string that contains the actions to be taken on a site. A site template, on the other hand, is like a container for the site script. You can attach one or more site scripts to a site template, and when you apply the site template, you execute the actions defined in all of the site scripts. It's like the site template is the visible surface part that can group several site scripts together, and the site scripts are all the action underneath.

Currently, no list template is available From your organization tab.


  • A site template can run multiple scripts. The script IDs are passed in an array, and they run in the order listed.
  • The former term for site templates may still appear in certain cmdlets and script labels as "site design".
  • The schema is case-sensitive.

Create a Site script in JSON

Create the script in JSON that describes the action to be executed on the list. You can view and reference the latest JSON schema file here: https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json

In addition to constructing the JSON by hand, you can also use third-party tools with graphical user interfaces that generate the site scripts for you, such as sitedesigner.io by Mikko Punamäki or Site Template Studio by Yannick Plenevaux.

Exporting an existing list as a site script

In our scenario, we will be exporting an existing list configuration as a site script.
First, connect to the SPO admin site using the Connect-SPOService cmdlet. It can be achieved with the following script. The Get-SPOSiteScriptFromList cmdlet prints out the site script to the console. It can be saved into a variable, or you can copy and save it in a JSON file.


Deploy Site Script and Site Template

To use our site script, we need to deploy it to our tenant. We also need to create a site template where we can attach the deployed site script.

1) Add the site script

Each site script must be registered in SharePoint so that it is available to use. Add a new site script by using the Add-SPOSiteScript cmdlet.


After running the cmdlet, you get a result that lists the site script ID of the added script. Keep track of this ID, as it will be needed later when you create the site template.

2) Create the site template

Next, we will create the site template. Run the Add-SPOListDesign cmdlet to add a new site template. Use the script ID saved above when you added the site script. It can run one or more scripts that have already been added.


The JSON response displays the ID of the new site template. It can be used later on to update or modify the site template.

Get the existing Site Script and Site Template

After we have deployed our site script and site template to our tenant, we can see their properties and information about all other deployed site scripts and templates using the Get-SPOSiteScript and Get-SPOListDesign cmdlet.



Use the new site template

Now that we've added a site script and site template, we can use them to create a new list.



I hope this will help all of you! 🧲
Feel free to provide feedback.


🚀 "Happy Coding" 🚀

Friday, 14 July 2023

PowerApps: Set the default user value in the Canvas app.

I came across one of the tasks in PowerApps to set the default value of the Person Type column.

In this blog, we will be learning how to set the default value in the Canvas app.

Please follow the below steps:

1. Create a new SharePoint list.

2. Create a Person/User type column called "Requestor Name".


3. Create a blank Canvas App with Format Tablet.

4. Add an Edit form control on screen and add the above created list as the data source for this form control. Set form DefaultMode to New.

5. For "Requestor Name DataCard" set the below value for the Default property.

Switch(
    frm_FileTransferService.Mode,
    FormMode.New,
    {
        '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
        DisplayName:User().FullName,
        Claims:"i:0#.f|membership|" & Lower(User().Email),
        Email:User().Email,
        Picture:User().Image
    },
    FormMode.View,
    ThisItem.'Requestor Name'
)


6. No need to do any changes in the Update property value. Its shown for reference only.


7. Under "Requestor Name DataCard" for DataCardValue.

    a. Comment on the Items property value, as it's not required. The items property is for populating the data in the people picker.


    b. Set the below value for the DisplayMode property. This DisplayMode property is used to configure whether the control inside the card allows user input (Edit), only displays data (View), or is disabled (Disabled).

If(
    frm_FileTransferService.Mode = FormMode.New,
    Parent.DisplayMode.Disabled,
    Parent.DisplayMode
)


9. Save and Publish the app.

10. Play/Run the app. In the New Form, the logged-in user is shown as default, and editing is disabled.



🚀 "Happy Coding" 🚀

Sunday, 2 January 2022

Power Automate: Update file metadata and preserve version history.

In this blog, we will be updating file properties/metadata whenever a new file is uploaded into the library.

In this approach, we will use the REST API and wrap it into a Power Automate flow.

Create Flow

Complete flow with all actions


Flow URL - https://flow.microsoft.com/

1. Open the flow, click on the Create option, and select "Automated Cloud Flow".


Enter Flow name.
Choose the flow trigger as "When an item is created".
Click the Create button.



2. Enter the "Site Address" SharePoint site URL and the "List Name" library title where flow needs to be associated.

Add a new action step, "Compose" and rename it "Compose-user".

In input, add an object as shown below. Created By Claims is value; here we will get the claims value of the user who is uploading the file.



3. Add a new action step again, "Compose" and rename it as "Compose-update".

In input, add an object as shown below.

  {

    "FieldName": "Internal column name",

    "FieldValue": "desired value"

  },

  {

    ............

  },

  .....

]

The input value will be an array of objects, and each object defines the configuration of the column to be updated. The FieldName is the internal column name, and the FieldValue is the desired value that you want to set in that column.

In my example, the Editor (Modified by) and Title columns are updated.

In the Editor FieldValue output of Compose-user will be set.

Note: Flow is running under the service account. If the Editor field is updated, then we can also preserve the Modified By column. Otherwise, the service account value will be updated in the Modified By column.



4. Add a new action step: Send an HTTP request to SharePoint.

Here we will compose a REST API request call.

  • Site Address: SharePoint site URL where the library is present
  • Method: POST
  • Uri: _api/web/lists/getbytitle('<List Title>')/items(<ID>)/validateUpdateListItem
  • Body: 

{
   "formValues": outputs('Compose_-_update'),
   "bNewDocumentUpdate":true
}


        validateUpdateListItem: REST API ValidateUpdateListItem() function to update listitem metadata

        formValues: The value must be the output of the second Compose action.

        bNewDocumentUpdate: true (Not to create any version on update) or false (Create a version on the update)

        For details refer: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-visio/jj246412(v=office.15)



        Upload the file to the Documents library and test the flow.


        🚀 "Happy Coding" 🚀

        Saturday, 24 July 2021

        SharePoint Home site.

         In this blog, we will be discussing the SharePoint Home site.


        What is SharePoint Home site?

        SharePoint Home site is a Communication site. It is the main landing/home site for your modern SharePoint intranet portal. It brings together news, documents, events, content, conversations, feeds, videos, and other resources.


        How to set the site as Home site?

        1. Create a new site or use an existing Communication site.

        2. Open SharePoint Online Management Shell in administrative mode and connect to SharePoint as a global admin or SharePoint admin.

        Here I have installed Microsoft.Online.SharePoint.PowerShell modules.

        SharePoint Online Management Shell: Getting Started

        3. Run the below command:

        Set-SPOHomeSite -HomeSiteUrl https://developer5.sharepoint.com/sites/TheLanding

        It will take some time to reflect on the change. In my case, it took 20 minutes. Run the below command to check which site is currently set as the Home site.

        Get-SPOHomeSite

        Home site can also be set easily through the SharePoint Online admin center.


        Go to SharePoint Online admin center at https://YourDomain-admin.sharepoint.com

        1. Click on the Settings option.

        2. Click on the "Home site" link.

        3. Enter the URL of the SharePoint Online communication site that you would like to set as a home site.

        4. Press the Save button.


        ~~~ Things to know ~~~

        1. Only PowerShell is available to set a site as the Home site. Currently, it cannot be done through UI as of now.

        Update: Home site can be set easily through the SharePoint Online admin center.

        2. Only one site can be set as the Home site for a Tenant.

        Update: In the latest release, users will be able to set up multiple home sites by using multiple Viva Connections experiences. The feature is currently in private preview and is expected to start rolling out broadly by the end of July 2023.

        3. Home site (Communication site) search scope will now be tenant-wide. The default search scope is at the site level only.



        4. Home site can be a Hub site also.

        5. News coming from the Home site will have a distinguishing highlighted Home site name banner.



        The same is available in the SharePoint mobile app.

        6. Enables quick access (Home icon) to the Home site from the SharePoint mobile app.


        7. The Global Navigation option will be enabled for the Home site now.


        Remove site as Home site

        Run the below command to remove the site as your Home site.

        Remove-SPOHomeSite
        Update: Home site can be removed easily through the SharePoint admin center.

        🚀 "Happy Coding" 🚀