Category Archives: Client-Side Rendering (CSR)

SharePoint Fest Chicago – December 2018

Are you attending #SPFestChi SharePoint Fest – Chicago next week? I’ll be presenting a workshop on Monday and sessions Wednesday through Friday. Mostly talking about the intersection of SharePoint and PowerApps but also talking some legacy client-side rendering for SharePoint ‘classic’ as well.

  • WRK302 – Introduction to Building PowerApps for SharePoint Users
  • PWR104 – PowerApps: New Approaches to the Traditional SharePoint List View
  • PWR202 – From SharePoint to PowerApps: Evolution of the ‘Intake Form’ Solution
  • PWR303 – Client-Side Rendering (CSR) for SharePoint: from Intro to Practical Application

Let me know if you have any questions about my sessions and hope to see you there!

JS Link – Add Ellipsis to a Column

Ran across a random request the other day to see the ellipsis menu added to a column other than the Title column. Turns out it’s a pretty simple change similar to how we change the column header text in a SharePoint view.

listItemMenu = “TRUE”

See the full code HERE.

Note: To remove the ellipsis from the Title column in the view, change the column from “Title (linked to item with edit menu)” to “Title” field in the view Columns.

References

Note and Disclaimer: JS Link currently works in SharePoint 2013, 2016, and SharePoint Online. JS Link does NOT work with the ‘Modern Experience’ interface – only Classic Mode. See ‘Choose Your Path Forward’ for more information.

JS Link – No More HTML in Calculated Field Change

Didn’t see that coming.

More than a few times I’ve had folks bring up solutions using calculated fields to do some of the same things we do with Client-side rendering (CSR) and JS Link in SharePoint. That was fine at the time. Just another way to get to the solution using creative out of the box capabilities – until now.

Microsoft implemented a change yesterday (June 13, 2017) that HTML or script elements in calculated fields would be turned off. Apparently this was an ‘undocumented use’ of the feature. The system is now escaping special characters and replacing the column with a blank in the list view.

Now What?

The short answers – in no particular order:

  1. Client-side rendering via JS Link property of web parts. This only works for Classic mode. If you are using Classic mode, this is likely the ‘best’ short term answer to broken interfaces until a longer-term solution is implemented
  2. Third party products
  3. SPFx Extensions or customization
  4. PowerApps

CSR and JS Link: There are multiple ways to implement JS Link, some via the browser (how I use JS Link) and via deployed coded solutions (Dev only). As mentioned in other responses JS Link does NOT work in the modern experience. It will only work in the Classic mode (as long as it is around). There have been NO dates announced for getting rid of Classic mode at this time. If you choose this direction it’s worth noting that if you’re comfortable writing script in the calculated field you’ll likely be comfortable writing the code needed to get CSR to work. I’ve got plenty of examples in the link included here.

Third party products. I’ve seen folks mention something called Skybow but I’ve never seen or used it. Only mentioning because it was mentioned by someone in the community I trust.

SPFx Extensions and customizations: This is the ‘approved’ development path for deployed solutions. If you are a developer and/or are creating solutions that are used across a broader scope this is likely the path you should go down.

PowerApps: This is likely the long-term replacement for solutions you used HTML or other scripting in your calculated field for (hand slap from English teacher). Embedded integration with SharePoint isn’t there yet (well, it kind of is…), but has been announced. You can start building the views/solutions you need, but they won’t be ‘really’ embedded in SharePoint yet – web parts and/or other tools are coming soon (they’ve been announced).

Note: Much of the attention to PowerApps has been in the area of forms, but views will be a part of the capabilities as well. They’ve already made improvements with introduction of controls like the Data Table control. Check out my posts on PowerApps for SharePoint users, specifically things like ‘List View’ Layouts.

Update

Migration Note: Do not forget to take this into consideration now as something to look at when migrating from an environment where special characters (HTML and scripting) work into an environment (O365, etc.) where they do not. You’ll need a plan to replace or update your field customizations.

Summary

Unfortunately this seems like a pretty sudden change. I certainly had no idea it was coming. For folks that were using this approach there’s a lot of scrambling going on right now dealing with blank columns that weren’t blank a few days ago. Depending on how complex the scripting was, some fixes might be as easy as stripping the HTML and script out and displaying a simple value. More complex solutions are going to take some effort to redo, either with a new approach, new code, or 3rd party software to get the business functionality back.

For simple HTML replacement, JS Link could be an easy switch. If you’ve never used it before check out the Hello World post and then the KPI post. Those will cover a lot and introduce you to the JS Link concept if you need to dig deeper for more complex solutions.

Good luck!

References

PowerApps – No Items to Show

In a previous JS Link / CSR post we covered how to handle what is displayed in a web part when there are no items to show. SharePoint’s default message is an ambiguous:
“There are no items to show in this view of the [listname] list”

Not an awesome user experience. Something a little more contextually relevant might be more helpful to users – either in a simple list view, or in a more complicated solution.

A PowerApps Gallery control isn’t any better – merely showing a blank record. The Data Table control, on the other hand, has a handy ‘NoDataText’ property that can be configured to display a message when the control is empty. 

So, what do we do when using Gallery controls – until a ‘NoDataText’ property is added and/or the Data Table control allows for more configuration options? As is the answer with many things in PowerApps: We use a formula.

Using an appropriate text field control within a Gallery control, we can replace the Text property with a formula like the following:

If(CountRows([_List Name_])=0,”[_Put your message here_]”, ThisItem.Title)

For a SharePoint list named: DefaultList you get the following:

If(CountRows(DefaultList)=0,”Nothing to see here”, ThisItem.Title)

The ‘CountRows’ function as you might guess returns the number of rows in the list.

The ‘If’ function tells the control to set the displayed value of the control to “Nothing to see here” if the condition (there are 0 rows) evaluates to ‘true’. If not, the value of the control is ThisItem.Title – the normal field value we would expect in the control. The text is only displayed when there are no items in the list. 

That’s it. Smile 

References

Top X Things Needed To Make PowerApps Awesome for SharePoint

While chatting about PowerApps recently, someone told me that I should always know my ‘top x’ things I want fixed, added, changed, etc. I can see where that might be handy to talk about with Microsoft, with other implementers, etc. It’s a quickly changing community and product and hey, they *are* listening. For PowerApps my current focus is on how it integrates with SharePoint. With that in mind, here’s my top 2. I’ll come up with more later. These are the ones I think are *really* important. 

  1. Embedded
  2. Embedded

PowerApps has great potential and already has a running start. But it has to nail the embedded story. If the integration of SharePoint and PowerApps is going to be *really* successful and gain the love of SharePoint users it needs to be easy and seamless. That means when a user selects a PowerApp in SharePoint it’s not going to jump to another application. It needs to run within the SharePoint interface.

So why do I list ‘embedded’ twice? Not because I really, really want it (I do) – but because there are (at least) two distinct embedded use cases:

Embedded Forms

EVERYONE has been talking about forms – which is well and good. They should be talking. They should (and have) been yelling. The gap is obvious and a solution is WAY overdue. With the scheduled end of InfoPath and the rudely unscheduled end of SharePoint Designer’s (SPD) visual designer, users have been left with one of the community’s longest and most obvious gaps. Thanks to third-party offerings (K2, Nintex, etc.) and community-efforts (Stratus Forms, etc.) certain needs have been met, but there are still gaps and it’s been far too long for a Microsoft-sponsored solution for business and power users.

Note: I don’t want this coming off as an anti-Microsoft rant. That is definitely not my intent. There were plenty of reasons for the delay. What they want to do and need to do is not trivial and the standards are extremely high. The integration we’re talking about also requires collaboration (see what I did there?) between two separate, complex, and rapidly changing products and teams. The PowerApps and SharePoint teams are also in different reporting structures within Microsoft. Fortunately both teams understand how important it is to get this particular integration done. The good thing is that there are many indications that this time around they’re going to make it – and maybe even exceed your expectations.

PowerApps needs to be able to replace existing SharePoint forms – the standard New, Edit, and Display forms – as well as add additional forms to a list or library. Form editing needs to be easy and intuitive in terms of which fields are displayed, how fields are laid out and formatted. Beyond that, there are plenty of other features we’d love to see, but the ones listed here are the core. Talk to anyone that’s used InfoPath or SPD and you’ll quickly get a list of wanted features.

We have every indication that Forms and the embedded experience will be addressed. Microsoft has gone as far as announcing that PowerApps IS the replacement for InfoPath. It is important to remember however that the features are coming iteratively, little by little, but continuously. So be patient.

Embedded Views

The embedded story that folks aren’t talking about as much is for Views. SharePoint views have historically been a powerful tool for business users. And while they are powerful out of the box, power users continue to find that they’d like to extend views beyond the out of box capabilities, and extend without involving developers when they are able.

Again, power users were once able to do some limited, yet still extremely useful, view customizations with SPD, but lost that power with the deprecation of the designer view. For the last few years, some customizations were again available using Client-Side Rendering (CSR) and the JS Link property of web parts. While extremely flexible, this approach was beyond most typical business users as it crept into a grey area between out of the box and ‘real’ customization and development. The approach never gained mainstream support or adoption. Now, as O365 continues to mature and lock down features that have the capability to jeopardize platform stability, CSR and JS Link are also going away from fringe power users and exclusively back into the hands of developers (good for the platform, unfortunate for those that were using it). 

Users need a way to get the benefit of SharePoint views, specifically choosing a list of fields, the order of the fields in a grid or spreadsheet format, the filter for the list of items, and how they are sorted. Once those core features are available they’re going to want the ability to customize that view using PowerApps’ ability to change field formats, apply conditional formatting, and other rules.

PowerApps today are surfaced in O365 SharePoint Online via the view dropdown.

image

Selecting one of them initiates the loading of a PowerApp, but only give the user a button to open the PowerApp – opening the client application.

image

Embedding the PowerApp would (hopefully) spin up a PowerApp right in the SharePoint window – just like any other view – rather than having to launch a client application – which is a fairly jarring experience for users.

I think there’s some interest in this use-case in the community, but it’s definitely less discussed than the forms example. I’m not sure what the interest level is, but I imagine anyone that was doing CSR and JS Link work would be interested in it. We’ll just have to wait and see.

#3 – A View Template

OK. So I did think of a third item on my list.

The current SharePoint template for PowerApps starts with a SharePoint list and creates a series of three forms in PowerApps. It would be great is there was a PowerApps template that took the selection a step further – into the current views for a list (or library) – and had a default layout that looks more like a traditional SharePoint view – a table/grid layout. This feature alone would allow SharePoint users to leverage their existing investment in views straight into extending them in the PowerApps interface. 

Summary

PowerApps is a powerful addition to the suite of tools Microsoft is making available for power users – many of which are currently using SharePoint. The PowerApps team wants to see user adoption grow and has a large group of potential users in the existing SharePoint user base – and SharePoint folks are an eager bunch. We’re already intrigued by the potential PowerApps brings to the table. If Microsoft is able to smoothly embed PowerApps into SharePoint (and Teams!) pages, users will be chomping at the bit to use PowerApps (and Flow) even faster than they already are.

JSLink – Filter on a Lookup Column with CSR

Lookup columns are both useful and a little odd in how they’re implemented in SharePoint list views. I wrote a bit about this in a previous post and showed one example on how to deal with Lookup column content. This post shows an alternative – and possibly more useful approach.

We’ll change the default link in the lookup field to a link that filters the current list by the lookup field value.  Yes, users can also use the filter built into the column header – this is just another way to implement it. I also think this is a bit more intuitive for users.

At the end of the day, this is just another example of custom link ‘building’ with CSR.
Sample File: CSR_LookupSelfFilter.js

Key lines:
image

With ‘Theme’ being the internal name of the Lookup field that we’re filtering on. Fairly straightforward.

The full list:
image

After clicking ‘Technics’:
image

Now we also need to add a ‘reset’ link to the web part so that users can get back to the default view. Otherwise, they could get stuck in a dead end after selecting a filter value. There are all kinds of ways to implement this, for simplicity’s sake we just updated the title text and link. 
Before:

image

After:
image

Obviously you don’t *actually* have to change the text of the web part header – just the URL. Smile But again, it seems like better UX.

Hope this is useful.

References:
JS Link and CSR Page

JSLink – Using the Lookup Column with CSR

This post covers another column type for use with CSR overrides. Lookup columns have what I consider an irritating default display characteristic in that they are displayed as a hyperlink to the lookup list item.

image

Where the link goes to:

image

The majority of the times I’ve seen a Lookup used, they are simply a list of items to select in a dropdown or radio buttons – like a category. In this case the hyperlink and displaying the form are useless (to me). Every so often the lookup list has a few other fields, like Description, etc. in that case the link can be a bit more useful, but it still seems to be more of a distraction from a user experience (UX) perspective.

What I’d like to see is the Lookup value – the same text – displayed without the hyperlink. I can get this value using the following:

ctx.CurrentItem.Theme[0].lookupValue

Or, more generically:

ctx.CurrentItem.[internalName][0].lookupValue

You can also access two other properties of the item. The properties are: isSecretFieldValue, lookupId, and lookupValue. And no, right now I have no idea what the heck ‘isSecretFieldValue’ is other than a Boolean value. Smile 

Note: OK. I looked it up. There’s one post under ‘isSecretFieldValue’ here – and if set to ‘True’ it apparently hides the values of the Created By and Modified By values in the form. Huh.

Back to our Lookup column.

As with several other column types, the Lookup column can contain more than one item and is stored as an array. As with those other column types you can also check how many items are contained in the column value by checking the length of the array:

ctx.CurrentItem.[internalName].length

Once an override is put into place using this approach, the list view now looks something like this:

image

Definitely cleaner.

Sample code for this example can be found here.

JSLink – Using the Person Column with CSR

When starting with JSLink and CSR (Client-Side Rendering) in SharePoint 2013 one usually starts with text fields. They tend to be the most intuitive and simple. Referring to a text field looks like this:

ctx.currentItem.[internalName] like “ctx.CurrentItem.Title”

Pretty straightforward. Other fields take a little bit more poking around to figure out the correct syntax. Person fields, like a few other field types, allow for one or more values to be stored – meaning the data will be stored in an array. Because of the array, overriding Person fields looks something like this:

The name:
ctx.CurrentItem.[internalName][0].title

The email address:
ctx.CurrentItem.[internalName][0].email

For some reason, the complete list of properties that are available for my ‘Created By’ field (internal name ‘Author’ – using Name with Presence) and ‘Modified By’ field (internal name ‘Editor’) are: email, id, picture, sip, and title.

However, when I have a Person field that I manually add to the list the properties I get are: department, email, id, jobTitle, picture, sip, title. When a People field allows multiple values it also has a ‘value’ property.

I’m not sure why you get the extra properties (department,  jobTitle, and value)  in addition to the original fields. The manually added field also has the picture property populated by the data from the user profile while the Created By and Modified By fields do not. More to dig into at another time.

If the Person column does contain more than one item, you can check ctx.CurrentItem.[internalName].length and then iterate through the array for all the names, email addresses, etc.

Definitely more nitty gritty details on how these fields work. But this should get you started for now.

Display Links to SharePoint Item Attachments using CSR

I had an inquiry this week regarding whether or not CSR could be used within a SharePoint list view to display links directly to an item’s attachments. It took a bit of tweaking code around a bit, but Raymond Mitchell (@iwkid) and I were able to get it working.

Background: SharePoint lists may allow for attachments to list items. SharePoint also has a default column that can be used in a view for displaying whether or not an item *has* one or more attachments. (Is that Clippy in disguise?)

image

A user experience (UX) stumbling block however is that you have to open up an item view in order to see the list of items and links to them. If you’re familiar with SharePoint, not a huge UX issue, but still somewhat irritating compared to just seeing the list of items in the view directly. If you’re *not* SharePoint-savvy, then you might not know at all whether an attachment exists or how to see attachments.

We’re aiming for something like this:

image

The CSR JavaScript file can be found here: CSR_ViewAttach.js

Note: I’m sure the JavaScript gurus out there will find more code cleanup that can be done and methods that can be used to improve it – this should get you started. We’ll add new versions as improvements are made.

Instructions for use:

  1. If you don’t already have one, create a ‘scripts’ document library on the Office365 / SharePoint site you’ll be using the CSR file on.
  2. Download the JavaScript file from here
  3. Edit the JavaScript file to work with your column, list and site URLs (see more in the sample file or below)
  4. Upload the edited JavaScript file to your ‘scripts’ library.
  5. jQuery is required for this CSR to work. If you don’t have jQuery already, upload it to the same scripts library. (See this post for more information on using jQuery with CSR)
  6. Add the Script Editor web part to your page or use the ‘Embed Code’ option to add a reference to jQuery.
    Ex. “<script language=”javascript” type=”text/javascript” src=”../Scripts/jquery-2.1.3.min.js”></script>”
  7. Your list: If you don’t have a ‘spare’ text column you can use to override in a view, create a new text column (ex. ‘ViewAttach’).
  8. Create a view for your list that includes your new column. You can still use all the same sorting, filtering, and other other view configuration settings for this view.
  9. When using your list view, edit the web part and add the link to your JavaScript file to the JS Link property of the web part. It will look something like: ~site/Scripts/CSR_ViewAttach.js

If everything is in place, attachments in your list should now be listed in the column you identified. (If not, check the common troubleshooting items for CSR… hmm, that sounds like another post…)

Items to configure in the JavaScript/CSR file before it will work in your environment (these are also noted in the file itself):

  • The (internal) name of field being overridden. In the example code below we’re using the ‘Body’ column (which is the internal name for the ‘Description’ field) of a task list. You can override any text field and/or create your own.
  • The siteURL variable.  — See example in the CSR file itself
  • The listName variable.  — The list that includes the attachments you’re displaying links to (example in file)
  • The attachPath variable.  — Link to where attachments are stored (example in file)

Thanks, Notes, and References:

Please let me know how this works for you!

JSLink – CSR to Override “There are no items to show in this view…”

The default message displayed in a view when that view has no items to display is:

“There are no items to show in this view of the [listname] list”.

In most cases this is just fine. When users are working directly in a list this message is consistent with the user experience.

If for whatever reason this text needs to be overridden there isn’t an obvious or simple approach to do this through the UI.

The default text is set within each list behind the scenes. If you were to change the property of the list directly using some custom approaches it would change the message for the list and be seen in all views. Using CSR, we’re going to change what is displayed without changing the core data. This give a few advantages:

  1. We generally don’t want to make permanent customizations if we don’t have to.
  2. Using CSR we can update the message for *each* view as necessary – which might be more appropriate for each use case.  

The bonus here is that it’s REALLY easy using CSR once you’ve figured it out. Smile 

So, here’s how it works:

Note: Normally I don’t add CSR to actual view pages – *especially* the All Items view – but technically you can. I generally use a separate page with a web/app part dropped on the page and apply CSR using JS Link at that point. But for this simple example we use the straight-up view page.

Our view starts out like this:

image

Now, you can take a shortcut by using the Footer override, but this will override both the default message when there are no items in the list *and* be displayed when there are items displayed. Fine if the override is blank, but not useful if you’re actually displaying a message.

overrideCtx.Templates.Footer = “ “;

The more elegant method is to override the default text in the list property SharePoint uses while leaving the logic SharePoint uses to decide when to display the message (No items in the view –> display the text). Within the list schema the property is ‘NoListItem’ and fortunately for us this property is available to us in our CSR JavaScript. In this example we use the OnPreRender override to make the change.

image

Applying this code as JS Link file gives us the following:

image

Pretty slick now that we’ve figured it out.

Thanks again to Raymond Mitchel (@iwkid) for helping with digging through the background details.