Modifying NewDocSet.aspx without dirty hacks

Typically when you browse the interwebs, you’ll get two answers to the question “how can I add JS to the new document set page?”:

  1. Edit the master page
  2. Edit the _layouts/15/NewDocSet.aspx

The first is a bad idea, because this adds extra weight to every page load in the site collection. And editing the master page is generally not recommended.

The second is an even worse idea, and cannot be deployed to SharePoint Online. It will also most probably break your code when you migrate to the next version, or migrate to Office 365.

So what can we do?

The Problem

The main issue here is that the NewDocSet.aspx is not a page in the virtual file system (i.e. in the content database), but in the physical file system (in the server hive), i.e. you can’t change it in the UI, nor in SharePoint Designer or other tools that have access to the WebDAV mounts of SharePoint.

So there’s no way to edit the web part on that page and add a JS Link to it.

The Solution

Using JSLink on a List field

This one is one of my favorites. I’ve recently discovered that this can be done.

Since SharePoint 2013, you can attach JS Link to a few levels, including View, Form, Web part, Content Type and Field. The Content Type is the most useful one, but unfortunately doesn’t work (a bug I guess).

But you can use the list field to accomplish this, for example (JavaScript Object Model, but could be C# CSOM as well):

// Get the list
ctx = SP.ClientContext.get_current();
list = ctx.get_web().getList("/server relative path!!/")
ctx.load(list);
ctx.executeQueryAsync();

// Get the field
fields = list.get_fields();
ctx.load(fields);
ctx.executeQueryAsync();
field = fields.getByTitle("field name you want to set CSR for");
ctx.load(field);
ctx.executeQueryAsync();

// Set the JS Link
field.set_jsLink("~siteCollection/path/to.js");
field.update();
ctx.executeQueryAsync();

 Using a User Custom Action

Downside: this is deployed to the entire site (collection).
And I haven’t gotten it to work yet, as deploying user custom actions can be done on lists as well, but generally seem to offer less functionality and hooks than the site collection or site level.

See this MSDN article for more information on how to create a user custom action.

Conclusion

Deploy your CSR to the NewDocSet.aspx using JSLink on a Field in the content type.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s