2

Im trying to register a client side rendering script to a webpart using JS to do so. Im having a hard time finding examples on how to do this, is anyone able to help me out? Below is what i got allready, where im fetching the webpart manager, really all i need is a pointer to as if how to find the right webpart in the Collection, and register the JavaScript file on it.

    this.getWebPartManager = function (list) {
    ctx = new SP.ClientContext(siteUrl);
    var oFile = ctx.get_web().getFileByServerRelativeUrl(serverRelativeUrl);

    var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    this.collWebPart = limitedWebPartManager.get_webParts();

    ctx.load(collWebPart);
    ctx.executeQueryAsync(Function.createDelegate(this, this.addCSRScriptToWebPart), Function.createDelegate(this, this.onError));


}

this.addCSRScriptToWebPart = function(){



}
2

How to set JSLink property using JSOM:

function setupJsLink(pageUrl, wpId,jsLinkUrl,success,error)
{
    var context = SP.ClientContext.get_current();
    var page = context.get_web().getFileByServerRelativeUrl(pageUrl);
    var wpm = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    var webpartDef = wpm.get_webParts().getById(new SP.Guid(wpId));
    var webpart = webpartDef.get_webPart();
    var properties = webpart.get_properties();
    properties.set_item("JSLink", jsLinkUrl);
    webpartDef.saveWebPartChanges();
    context.executeQueryAsync(
        function () {
          success();                  
        }, 
        error);
}

Usage

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {

    var pageUrl = _spPageContextInfo.serverRequestPath;
    var webPartId = 'f6989c4d-1387-4657-a8f6-30dc0a4f967f';   //web part id
    var jsLinkUrl = '~sitecollection/Style Library/tasks.js';  //jslink url
    setupJsLink(pageUrl, webPartId,jsLinkUrl,
       function(){
          console.log('Saved.');
       },
       function(sender, args) {
          console.log(args.get_message());
       });       
});
0

From here: Update/set web part property JSLink using JavaScript CSOM

UpdateWebPartProperty();

function UpdateWebPartProperty()
{
    var listName = "list1";
    var jslinkURL = "~sitecollection/SiteAssets/testjslink.js";
    var pageUrl = "/Lists/" + listName + "/NewForm.aspx";

    var clientContext = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
    console.log(_spPageContextInfo.webServerRelativeUrl);
    var oFile = clientContext.get_web().getFileByServerRelativeUrl(_spPageContextInfo.webServerRelativeUrl + pageUrl); 
    var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    var collWebPart = limitedWebPartManager.get_webParts();

    clientContext.load(collWebPart);
    clientContext.executeQueryAsync(function () 
    {
        var webPartDef = null;
        for (var x = 0; x < collWebPart.get_count() && !webPartDef; x++) {
            var temp = collWebPart.get_item(x);
            console.log(temp.get_id().toString());
        }
        webPartDef = collWebPart.get_item(0);
        if (!webPartDef) {
            console.log("Web Part: " + wpId + " not found on page: " + _spPageContextInfo.webServerRelativeUrl);
            return;
        }
        var webPartProperties = webPartDef.get_webPart().get_properties();
        clientContext.load(webPartProperties);
        clientContext.executeQueryAsync(
            function () {
                var webpartprops = webPartProperties;
                console.log(webpartprops.get_item('JSLink'));
                webpartprops.set_item("JSLink", jslinkURL);
                webPartDef.saveWebPartChanges();
                clientContext.load(webPartDef);
                clientContext.executeQueryAsync(
                function () {
                    console.log("Web part properties saved.");
                },
                function() 
                {
                    console.log("Failed save Web part Properties"); 
                });
            }, 
            function () 
            { 
                console.log("Failed to load web part properties"); 
            });
    }, 
    function () { 
        console.log("Failed to load web part collection"); 
    });
}

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.