4

I am trying to add a JSLink to my field through pnp so that it displays diffrently in the edit/new mode in the list that it is connected to, the problem i am having is that pnp throws an unknown error when i am running this command (Reference):

Set-PnPField -Identity KIT_sc_ProjectType -Values @{JSLink="~sitecollection/siteassets/jslink/groupcreation_grouptemplates.js";Group="KIT Fields"} -UpdateExistingLists

this the error i am getting back:

Set-PnPField : unknown error At line:1 char:1 + Set-PnPField -Identity KIT_sc_ProjectType -Values @{JSLink="~sitecoll ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Set-PnPField], ServerException + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Fields.SetField

Is it possible to add JSLink some another way to a field?

4 Answers 4

2

Set the JSLink to a field using JSOM:

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(setJslink, 'sp.js');
function setJslink()
{
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var field = web.get_availableFields().getByTitle("Description");
        context.load(field);
        context.executeQueryAsync(function(){
        field.set_jsLink("~sitecollection/SiteAssets/suggestion.js");
        field.updateAndPushChanges(true);
        context.executeQueryAsync(function(){
        var jsl = field.get_jsLink();
        console.log(jsl);
        });
        });
}
</script>

Reference:

Set JSLink on a Site Column With JSOM

4
  • Is it possible to execute this without deploying it as a add-in to the site collection?
    – SaadRH
    Commented Jun 24, 2019 at 8:36
  • Yes, no need to deploy as add-in. Just create a classic site page/web part page and then insert the code with Script Editor Web Part will be working. Please refer the video below to add JavaScript code with Script Editor Web Part. youtube.com/watch?v=yhKwWgILv_Y
    – Jerry
    Commented Jun 24, 2019 at 9:19
  • I don't see the "Script Editor Web Part" in my webpart catalog when page is in edit mode. Is it not an default webpart?
    – SaadRH
    Commented Jun 24, 2019 at 13:45
  • This is a deault web part in SharePoint , if you can't see the web part in your side, please check the blog below to enable it: enjoysharepoint.com/…
    – Jerry
    Commented Jun 25, 2019 at 3:09
1

There are several ways to add the JavaScript (JSLink) in Content Editor Web Part to the page: JS, CSOM note that the URL link should be added to ContentLink:

var siteUrl = '/sites/MySiteCollection';
var serverRelativeUrl = '/sites/MySiteCollection/Default.aspx';

function addWebPart() {
    var clientContext = new SP.ClientContext(siteUrl);
    var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);
    var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    var webPartXml = '<?xml version=\"1.0\" encoding=\"utf-8\"?>' + 
        '<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"' + 
        ' xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"' + 
        ' xmlns=\"http://schemas.microsoft.com/WebPart/v2\">' + 
        '<Title>My Web Part</Title><FrameType>Default</FrameType>' + 
        '<Description>Use for formatted text, tables, and images.</Description>' + 
        '<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</PartOrder>' + 
        '<FrameState>Normal</FrameState><Height /><Width /><AllowRemove>true</AllowRemove>' + 
        '<AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize>' + 
        '<AllowConnect>true</AllowConnect><AllowEdit>true</AllowEdit>' + 
        '<AllowHide>true</AllowHide><IsVisible>true</IsVisible><DetailLink /><HelpLink />' + 
        '<HelpMode>Modeless</HelpMode><Dir>Default</Dir><PartImageSmall />' + 
        '<MissingAssembly>Cannot import this Web Part.</MissingAssembly>' + 
        '<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter />' + 
        '<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, ' + 
        'PublicKeyToken=94de0004b6e3fcc5</Assembly>' + 
        '<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>' + 
        '<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">' + '/sites/SiteAssets/Test.js</ContentLink>' + 
        '<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">' + 
        '<![CDATA[This is a first paragraph!<DIV>&nbsp;</DIV>And this is a second paragraph.]]></Content>' + 
        '<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>';

    var oWebPartDefinition = limitedWebPartManager.importWebPart(webPartXml);
    this.oWebPart = oWebPartDefinition.get_webPart();
    limitedWebPartManager.addWebPart(oWebPart, 'Left', 1);
    clientContext.load(oWebPart);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
    alert('Web Part added: ' + oWebPart.get_title());
}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
1
  • Sorry, i forgot to mention that the field i am trying to add the JSLink to is connected to a list, and i want to display the edit/new mode of the field differently based on the code in the JSLink file. This is all happening in the classic mode of sharepoint.
    – SaadRH
    Commented Jun 24, 2019 at 8:49
1

Set JS Link using PNP Field, just remove tilt sign from jslink path. JSLink properties requires relative path without any placeholder. it will automatically add your current site url before '/'

Set-PnPField -Identity KIT_sc_ProjectType -Values @{JSLink="/siteassets/jslink/groupcreation_grouptemplates.js";Group="KIT Fields"} -UpdateExistingLists
5
  • Tried that, same error. I even tried CSOM using a console application. Getting a similar error. I checked https://<tenant>-admin.sharepoint.com/_layouts/15/online/TenantSettings.aspx and saw "Custom Scripts" was set to "Prevent", could this be it?
    – SaadRH
    Commented Jun 24, 2019 at 12:30
  • can you try to update any other property, just to see if it is updating. Commented Jun 24, 2019 at 12:31
  • i tried this: "Set-PnPField -Identity 'KIT_sc_ProjectType' -Values @{Required=$true}" and changed back and forth between true and false and it worked like a charm but not the JSLink property.
    – SaadRH
    Commented Jun 24, 2019 at 13:05
  • let me try tonight, will update you. seems to be bug.... Commented Jun 24, 2019 at 13:23
  • Thanks, will be waiting.
    – SaadRH
    Commented Jun 24, 2019 at 13:27
0

I needed to enable "Custom Script" in "https://tenantname-admin.sharepoint.com/_layouts/15/online/TenantSettings.aspx".

Everything works now, i was able to run it with pnp.

1
  • Thanks for sharing, you could accept your own reply as answer so that it could help others in the forum.
    – Jerry
    Commented Jun 26, 2019 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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