Blog

SharePoint JavaScript Context Development – Part 1 – SP.ScriptUtility

4 min read
4 min read

The hidden gem of SharePoint context JavaScript development – SP.ScriptUtility

This blog is part of the blog series SharePoint JavaScript Context Development

JavaScript in SharePoint

Something I have been rather passionate about for some time has been a lack of knowledge in SharePoint developers in JavaScript. The lack of knowledge became apparent several years ago when I worked on a small project that required some nice looking user interaction. This project was the first time I had moved from being a contractor to being a full-time developer for Microsoft products.

What I saw was developers not understanding the type system in JavaScript, not understanding closures, and more often than not turning to internet search for generic solutions to methods that SharePoint already has available in the context.

There are 161 JavaScript files in SharePoint 2013 that have debug versions, there is almost every kind of helper method you can imagine, and there are even duplicate methods that do the same thing in these files.

So I’ve decided to write this series to start covering off the most useful and most used of these helper methods. I will start this with the SP.ScriptUtility JavaScript class, which is described on the MSDN at SP.ScriptUtility JavaScript Class MSDN.

SP.ScriptUtility resides in the SP.Init.js JavaScript library.

This class is the home to six very useful helper methods and one helper field.

SP.ScriptUtility Fields

  • emptyString Field

SP.ScriptUtility Methods

  • isNullOrEmptyString(str) Method
  • isNullOrUndefined(obj) Method
  • isUndefined(obj) Method
  • truncateToInt(n) Method
  • ScriptUtility() Constructor

Being able to infer the meaning from most of these is quite simple, however for clarification I will break down each one.

emptyString
This field returned an empty string in JavaScript “”. Semantically this can improve readability in code in which you mean to use strings. Remembering JavaScript isn’t a strongly typed language using helper methods such as this helps reinforce usage to other developers in the team.

Bool isNullOrEmptyString(str)
This method expects a string and returns a Boolean value. This method presumes the type is a string and checks against null, undefined and empty string.

Bool isNullOrUndefined(obj)
This is a more generic version of the method above, and any type in JavaScript can be checked with this method. The value is checked vs. null and undefined only, returning a Boolean value.

Important note: In JavaScript null and undefined are different objects, and you cannot presume something is one or the other especially in the SharePoint context.

Bool isUndefined(obj)
This method accepts any object and uses type checking to see if the value has been defined, returning a Boolean value if there was a match.

Number(Inferred as Int) truncateToInt number
This method detects if the value is positive or not, returning the correctly rounded value. This method can only take numbers whether they are in a string, without any other characters or as a number value. It is important to note JavaScript treats all numbers in the same way.

ScriptUtility Constructor
This method does nothing. It merely acts as a placeholder for the class name SP.ScriptUtility to hold the above methods.

Conclusion

SP.ScriptUtility houses several useful methods for use in your code; however this functionality is duplicated, so next time I will go into the first place this is duplicated. Again in SP.Init.js I will look at the undocumented SP.ScriptHelpers class.

Please feel free to comment, or ask questions. Also please subscribe for instant updates to our blog, where you can find out the latest and more unanswered SharePoint development questions from the SPCAF Team.

Read on Part 2 – SP.ScriptHelpers

Subscribe to our newsletter