Friday, August 15, 2014

SharePoint 2010: Execute custom code when scroll event is triggered

SharePoint 2010 does a fantastic job of handling scrolling. Even so, there are some instances where modifying this behaviour can be beneficial. This blog explores the problem of executing custom code when a scroll event is triggered and uses a function wrapper as a solution. I’ve written an article detailing how SharePoint 2010’s default scrolling behaviour works; I will be referring to ideas and components explained in that article in this blog entry. You can visit that article by clicking this link. It’s not necessary by any means to read it, but it might help. Without further adieu, let’s dive into the world of SharePoint scrolling behaviour.

Jump to Section



1. The Problem

We know that the FixRibbonAndWorkspaceDimensions function is called whenever an element or window is resized. That’s good to know because if we can add our code to the FixRibbonAndWorkspaceDimensions function, then SharePoint will call our code when a resize occurs. That sounds perfect! The problem is we don’t want to modify SharePoint’s JavaScript files in the hive. We want to keep the stock SharePoint files as close to their original release as possible. Why shouldn’t we modify these files? The SharePoint developers aren’t expecting our code in their files, so an update will undoubtedly replace old files with new ones. That would completely erase all our hard work! We can get around this by appending our own JavaScript functions onto the existing functions.

2. The Solution

JavaScript has this nifty ability to reference functions by storing them in objects. When SharePoint sets up its events it is using a reference to the function FixRibbonAndWorkSpaceDimensions as an event handler. This means that SharePoint will execute whatever function reference is in the FixRibbonAndWorkSpaceDimension object when an event fires. So, we can make FixRibbonAndWorkSpaceDimensions reference our custom code instead, right? Yes, we can do that. But you need to consider scenarios where a crucial fix was applied in FixRibbonAndWorkSpaceDimensions. If we replaced the original function with our own, then the crucial fix will never be executed. A better idea is to run SharePoint’s code followed by our own. We can do this by writing a wrapper function that executes both functions one after another. Then we have FixRibbonAndWorkSpaceDimensions reference the wrapper function. The result of this is our wrapper function is called whenever SharePoint fires an event that uses the FixRibbonAndWorkSpaceDimensions object.

Figure 1 provides code that you can use to implement this solution. The first thing the code does is store a reference to the original FixRibbonAndWorkSpaceDimensions function, so that we can call it later in our wrapper function. The next thing we do is set the FixRibbonAndWorkSpaceDimensions object to reference our wrapper function. Our wrapper function first calls the original FixRibbonAndWorkSpaceDimensionsFunction and then calls our custom code. This may seem odd or confusing, but all that’s really happening is we are changing which object points to which function (code). I’ve provided a link at the end of this article with further explanation of how function pointers work to help clear up any confusion.

FixRibbonAndWorkSpaceDimensionsFunction = FixRibbonAndWorkSpaceDimensions;
FixRibbonAndWorkSpaceDimensions = Wrapper;

function Wrapper() {
    FixRibbonAndWorkSpaceDimensionsFunction();
    CustomCode();
}

function CustomCode() {
    // write your own code to do whatever you want
    console.log(‘Our custom code is running!’);
}

Figure 1



3. The End

You can add whatever custom code you want and have it run after the FixRibbonAndWorkSpaceDimensions function without worrying about compatibility issues. The idea of a wrapper function that executes two functions can be applied to any JavaScript problem of this nature. I’m sure you can think of at least a couple more scenarios where you would like to override another default SharePoint behaviour. Good luck in your future SharePoint branding efforts.

4. Additional Resources