tavikukko / Chrome-SP-Editor

Extension for creating and updating files (js, css) in SharePoint Online from Developer Tools
122 stars 39 forks source link

How can we use Modern Scriptlinks (not an issue but question)? #98

Closed Gennady-G closed 2 years ago

Gennady-G commented 2 years ago

Hi @tavikukko !

Occasionally noticed that Edge version has "Modern Scriptlink App", that installs *.sppkg in App Catalog.

How can I use it? What code should be in js file?

For example earlier CSR was registered like this (this is example of code generated by Cisar, https://github.com/andrei-markeev/cisar):

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function () {
    function getBaseHtml(ctx) {
        return SPClientTemplates["_defaultTemplates"].Fields.default.all.all[ctx.CurrentFieldSchema.FieldType][ctx.BaseViewID](ctx);
    }
    function init() {
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
            // OnPreRender: function(ctx) { },
            Templates: {
                //     View: function(ctx) { return ""; },
                //     Header: function(ctx) { return ""; },
                //     Body: function(ctx) { return ""; },
                //     Group: function(ctx) { return ""; },
                //     Item: function(ctx) { return ""; },
                //     Fields: {
                //         "<field internal name>": {
                //             View: function(ctx) { return ""; },
                //             EditForm: function(ctx) { return ""; },
                //             DisplayForm: function(ctx) { return ""; },
                //             NewForm: function(ctx) { return ""; }
                //         }
                //     },
                //     Footer: function(ctx) { return ""; }
            },

            // OnPostRender: function(ctx) { },
            ListTemplateType: 171
        });
    }
    RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens("~siteCollection/Style Library/delme.js"), init);
    init();
});

What type of js can we use in Modern Scriptlink? Maybe You have some example?

Best regards, Gennady

tavikukko commented 2 years ago

Hi @Gennady-G !

Just create javascript file and drop it to a folder in SP. Then inject that file using SP Editor's ScriptLinks functionality. Now, if you have installed the Modern ScriptLink App, the file you injected will be loaded when opening page in the injected files scope.

The source code of Modern ScriptLinks App can be found here https://github.com/tavikukko/sp-scriptlinks

I hope this helps, Tomi

Gennady-G commented 2 years ago

The source code of Modern ScriptLinks App can be found here https://github.com/tavikukko/sp-scriptlinks

So as I've understand - it is something like 'Dynamic Application Customizer', that can append different js files to page header

But what should be inside js file?

I tried to add App to site-collection, uploaded file with "console.log('Hi!')" but don't see if it is loaded.. Or I do something wrong.. My url is: https://server.sharepoint.com/sites/dev/SiteAssets/scriptlink.js

11-2022-01-13_164733

s2-2022-01-13_163900

s3-2022-01-13_163916

s4-2022-01-13_163928

Gennady-G commented 2 years ago

App is deployed successfully:

app-2022-01-13_165237

tavikukko commented 2 years ago

Ok, I need to check if that App still works as it should. But it just loads that file what you have injected and execute it, that's all. So the console.log just print something.

Gennady-G commented 2 years ago

Ah, ok! so I don't see it in Network tab.. But it prints nothing in console, too..

tavikukko commented 2 years ago

Change the scope to sitecollection in the ScriptLink.

tavikukko commented 2 years ago

Seems like the App only reads sitecollection links. But anyways, I don't think people should use sub webs with Modern SharePoint.

Gennady-G commented 2 years ago

Wow! Thank You much @tavikukko , as usual! :) Now it works perfect!)

hi-2022-01-13_174128