DEV Community

Kinga
Kinga

Posted on • Edited on

4

Set properties for/from SPFx Application Customizer

When developing SPFx solutions, you may want to provide additional properties to configure your extension.

You may define them in serve.json for testing:

{
"url": "https://yourtenant.sharepoint.com/sites/yoursite/?loadSPFX=true&debugManifestsFile=https://localhost:4321/temp/manifests.js&customActions={\"xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\":{\"location\":\"ClientSideExtension.ApplicationCustomizer\",\"properties\":{\"isConfigured\":false}}}",
  //...
}
Enter fullscreen mode Exit fullscreen mode

or in elements.xml file using ClientSideComponentProperties property:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
        Title="Breadcrumbs"
        Location="ClientSideExtension.ApplicationCustomizer"
        ClientSideComponentId="xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
        HostProperties="{&quot;preAllocatedApplicationCustomizerTopHeight&quot;:&quot;47&quot;}"
    >
    </CustomAction>
</Elements>
Enter fullscreen mode Exit fullscreen mode

or even in Site Design using associateExtension verb with clientSideComponentProperties:

{
    "verb": "associateExtension",
    "title": "ETHZ Corporate Design: add Site Logo and Footer",
    "location": "ClientSideExtension.ApplicationCustomizer",
    "clientSideComponentId": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "clientSideComponentProperties": "{\"isConfigured\":\"false\"}",
    "scope": "Site"
}
Enter fullscreen mode Exit fullscreen mode

You may also update them later using PnP PowerShell

Set properties from Application Customizer

But what if you want to update a property from the Application Customizer itself?
You may need it, if the application customizer is provisioning resources and this action should be executed only once. Or maybe user is prompted for initial configuration, which should be saved and used from now on.

It's not as easy as when updating WebPart properties, but doable neverthless :)

import "@pnp/sp/sites";
import "@pnp/sp/user-custom-actions";
import { IUserCustomActionInfo } from '@pnp/sp/user-custom-actions';

interface IAppCustomizerInfo extends IUserCustomActionInfo{
    "ClientSideComponentId":string;
    "ClientSideComponentProperties":string;
}

//...

try{
    const siteUserCustomActions = await this.spfiContext.site.userCustomActions()
    if(siteUserCustomActions.length === undefined || siteUserCustomActions.length === 0){
        return {
            result: false,
            error: "Site Design resource not found"
        }
    }
    const appCustomizers = siteUserCustomActions
        .filter(uca => uca.Location === "ClientSideExtension.ApplicationCustomizer")
        .filter((uca : IAppCustomizerInfo) => uca.ClientSideComponentId === _applicationCustomizerID);

    if (appCustomizers.length>0){
        const uca= appCustomizers[0] as IAppCustomizerInfo;


        const props: { [key: string]: string } = {
            "ClientSideComponentProperties" : JSON.stringify({
                    ...JSON.parse(uca.ClientSideComponentProperties),
                    isConfigured: "true"
                })
            }
        const userCustomActions = this.spfiContext.site.userCustomActions.getById(uca.Id);
        await userCustomActions.update(props);

        return { result: true };
    }
}
catch (error) {
    return  {
        result: false,
        error: error
    }
}
Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay