1

I have 300 Subsites and want to add my custom SPFx Extension to all those subsites. Manually I know how to achieve that. I just go to site contents and add my app. But I want to solve this programatically.

Best way would be via REST, but I could not find anything how to do this. CSOM would be another option, but REST is definitely preferred.

Any suggestions or helpful links?

2

An easier way for you to add an extension would be to register it as the site collection user custom action.

Once you do that, all the existing subsites and subsites which will be created afterwards will have this extension automatically loaded. This way you dont have to manually add it for every subsite individually.

Have added below sample code to do that:

using (ClientContext clientContext = new ClientContext("https://site-collection-url"))
{
    var userName = "admin@tenant.onmicrosoft.com"
    var password = "password";
    SecureString securePassword = new SecureString();
    foreach (char c in password.ToCharArray())
    {
        securePassword.AppendChar(c);
    }

    clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
    clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);                

    // you can get the Guid of the extension
    // from the [Extension].manifest.json file's id property

    Guid spfxExtension_GlobalHeaderID = new Guid("<Guid of the extension>");
    string spfxExtName = "HelloWorldExtension";
    string spfxExtTitle = "HelloWorld Application Customizer Extension";

    string spfxExtDescription = "Adds HelloWorldExtension to the site";
    string spfxExtLocation = "ClientSideExtension.ApplicationCustomizer";
    string spfxExtProps = "";  // add properties if any, else remove this

    UserCustomAction userCustomAction = clientContext.Site.UserCustomActions.Add();
    userCustomAction.Name = spfxExtName;
    userCustomAction.Title = spfxExtTitle;
    userCustomAction.Description = spfxExtDescription;
    userCustomAction.Location = spfxExtLocation;
    userCustomAction.ClientSideComponentId = spfxExtension_GlobalHeaderID;
    userCustomAction.ClientSideComponentProperties = spfxExtProps;

    clientContext.Site.Context.ExecuteQuery();
}

Just add the above code in a console application and use the CSOM Nuget package.

Code modified and Referenced from - Manage tenant-scoped SPFx extensions across your SharePoint sites

Also, if you want to do it via REST API only, you can checkout the below links which use the ALM APIs.

Reference - Application Lifecycle Management (ALM) APIs for SPO

Using PnP JS ALM APIs - Using ALM APIs with PnP JS library

0

using (ClientContext ctx = getContext(newSiteUrl, configurationValues)) { var appManager = new AppManager(ctx); var apps = appManager.GetAvailable(); var chartsApp = apps.Where(a => a.Title == "title of your app").FirstOrDefault(); var installApp = appManager.Install(chartsApp);
if (installApp) { Guid spfxExtension_GlobalHeaderID = new Guid("xxxx-xxxx-xxxxxx"); string spfxExtName = "App Name"; string spfxExtTitle = "App Title"; string spfxExtGroup = ""; string spfxExtDescription = "Adds a global header/footer to the site"; string spfxExtLocation = "ClientSideExtension.ApplicationCustomizer";

                CustomActionEntity ca = new CustomActionEntity
                {
                    Name = spfxExtName,
                    Title = spfxExtTitle,
                    Group = spfxExtGroup,
                    Description = spfxExtDescription,
                    Location = spfxExtLocation,
                    ClientSideComponentId = spfxExtension_GlobalHeaderID
                };

                ctx.Web.AddCustomAction(ca);
                ctx.ExecuteQueryRetry();
            }                
        }
  • Please improve the formatting of code. So that others can understand it. – Ganesh Sanap Nov 18 '18 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.