Geeks With Blogs

News





The SharePoint Hillbilly Fewer Big Words... More Pretty Pictures...

Yes… another one of these posts. What can I say? I’m a pot stirrer.. a rabble rouser *rabble rabble*

jQuery in SharePoint seems to be a fairly polarizing issue with one side thinking it is the most awesome thing since Princess Leia as the slave girl in Return of the Jedi and the other half thinking it is the worst idea since Mannequin 2: On the Move. The correct answer is OF COURSE “it depends”.

But what are those deciding factors that make jQuery an awesome fit or leave a bad taste in your mouth? Let’s see if I can drive the discussion here with some polarizing comments of my own… I know some of you are getting ready to leave your comments even now before reading the rest of the blog, which is great! Iron sharpens iron… These discussions hopefully open us up to understanding the entire process better and think about things in a different way.

You should not be writing jQuery in SharePoint if you are not a developer…

Let’s start off with my most polarizing and rant filled portion of the blog post. If you don’t know what you are doing or you don’t have a background that helps you understand the implications of what you are writing then you should not be writing jQuery in SharePoint!

I truly believe that one of the biggest reasons for the jQuery haters is because of all the bad jQuery out there. If you don’t know what you are doing you can do some NASTY things! One of the best stories I’ve heard about this is from my good friend John Ferringer (@ferringer). John tells this story during our Mythbusters session we do together. One of his clients was undergoing a Denial of Service attack and they couldn’t figure out what was going on! After much searching they found that some genius jQuery developer wrote some code for an image rotator, but did not take into account what happens when there are no images to load! The code just kept hitting the servers over and over and over again which prevented anything else from getting done! Now, I’m NOT saying that I have not done the same sort of thing in the past or am immune from such mistakes. My point is that if you don’t know what you are doing, there are very REAL consequences that can have a major impact on your organization AND they will be hard to track down.  Think how happy your boss will be after you copy and pasted some jQuery from a blog without understanding what it does, it brings down the farm, AND it takes them 3 days to track it back to you.  :/ Good times will not be had.

Like it or not JavaScript/jQuery is a programming language. While you .NET people sit on your high horses because your code is compiled and “runs faster” (also debatable), the rest of us will be actually getting work done and delivering solutions while you are trying to figure out why your widget won’t deploy. I can pick at that scab because I write .NET code too and speak from experience. I can do both, and do both well. So, I am not speaking from ignorance here.

In JavaScript/jQuery you have variables, loops, conditionals, functions, arrays, events, and built in methods. If you are not a developer you just aren’t going to take advantage of all of that and use it correctly.

Ahhh.. but there is hope! There is a lot of jQuery resources out there to help you learn and learn well! There are many experts on the subject that will gladly tell you when you are smoking crack.

I just this minute saw a tweet from @cquick with a link to: “jQuery Fundamentals”. I just glanced through it and this may be a great primer for you aspiring jQuery devs.

Take advantage of all the resources and become a developer! Hey, it will look awesome on your resume right?

You should not be writing jQuery in SharePoint if it depends too much on client resources for a good user experience

I’ve said it once and I’ll say it over and over until you understand. jQuery is executed on the client’s computer. Got it? If you are looping through hundreds of rows of data, searching through an enormous DOM, or performing many calculations it is going to take some time! AND if your user happens to be sitting on some old PC somewhere that they picked up at a garage sale their experience will be that much worse! If you can’t give the user a good experience they will not use the site. So, if jQuery is causing the user to have a bad experience, don’t use it. I sometimes go as far to say that you should NOT go to jQuery as a first option for external facing web sites because you have ZERO control over what the end user’s computer will be. You just can’t guarantee an awesome user experience all of the time.

Ahhh… but you have no choice? (where have I heard that before?). Well… if you really have no choice, here are some tips to help improve the experience:

Avoid screen scraping

This is not 1999 and SharePoint is not an old green screen from a mainframe… so why are you treating it like it is? Screen scraping is time consuming and client intensive. Take advantage of tools like SPServices to do your data retrieval when possible.

Fine tune your DOM searches

A lot of time can be eaten up just searching the DOM and ignoring table rows that you don’t need. Write better jQuery to only loop through tables rows that you need, or only access specific elements you need. Take advantage of Element ID’s to return the one element you are looking for instead of looping through all the DOM over and over again.

Write better jQuery

Remember this is development. Think about how you can write cleaner, faster jQuery. This directly relates to the previous point of improving your DOM searches, but also when using arrays, variables and loops. Do you REALLY need to loop through that array 3 times? How can you knock it down to 2 times or even 1? When you have lots of calculations and data that you are manipulating every operation adds up. Think about how you can streamline it. Back in the old days before RAM was abundant, Cores were plentiful and dinosaurs roamed the earth, us developers had to take performance into account in everything we did. It’s a lost art that really needs to be used here.

You should not be writing jQuery in SharePoint if you are sending a lot of data over the wire…

Developer:  “Awesome… you can easily call SharePoint’s web services to retrieve and write data using SPServices!”

Administrator: “Crap! you can easily call SharePoint’s web services to retrieve and write data using SPServices!”

SPServices may indeed be the best thing that happened to SharePoint since the invention of SharePoint Saturdays by Godfather Lotter… BUT you HAVE to use it wisely! (I REFUSE to make the Spiderman reference). If you do not know what you are doing your code will bring back EVERY field and EVERY row from a list and push that over the internet with all that lovely XML wrapped around it. That can be a HUGE amount of data and will GREATLY impact performance! Calling several web service methods at the same time can cause the same problem and can negatively impact your SharePoint servers.

These problems, thankfully, are not difficult to rectify if you are careful:

Limit list data retrieved

Use CAML to reduce the number of rows returned and limit the fields returned using ViewFields.  You should definitely be doing this regardless. If you aren’t I hope your admin thumps you upside the head.

Batch large list updates

You may or may not have noticed that if you try to do large updates (hundreds of rows) that the performance is either completely abysmal or it fails over half the time. You can greatly improve performance and avoid timeouts by breaking up your updates into several smaller updates. I don’t know if there is a magic number for best performance, it really depends on how much data you are sending back more than the number of rows. However, I have found that 200 rows generally works well.  Play around and find the right number for your situation.

Delay Web Service calls when possible

One of the cool things about jQuery and SPServices is that you can delay queries to the server until they are actually needed instead of doing them all at once. This can lead to performance improvements over DataViewWebParts and even .NET code in the right situations. So, don’t load the data until it’s needed. In some instances you may not need to retrieve the data at all, so why retrieve it ALL the time?

You should not be writing jQuery in SharePoint if there is a better solution…

jQuery is NOT the silver bullet in SharePoint, it is not the answer to every question, it is just another tool in the developers toolkit. I urge all developers to know what options exist out there and choose the right one! Sometimes it will be jQuery, sometimes it will be .NET,  sometimes it will be XSL, and sometimes it will be some other choice…

So, when is there a better solution to jQuery?

When you can’t get away from performance problems

Sometimes jQuery will just give you horrible performance regardless of what you do because of unavoidable obstacles. In these situations you are going to have to figure out an alternative. Can I do it with a DVWP or do I have to crack open Visual Studio?

When you need to do something that jQuery can’t do

There are lots of things you can’t do in jQuery like elevate privileges, event handlers, workflows, or interact with back end systems that have no web service interface. It just can’t do everything.

When it can be done faster and more efficiently another way

Why are you spending time to write jQuery to do a DataViewWebPart that would take 5 minutes? Or why are you trying to implement complicated logic that would be simple to do in .NET? If your answer is that you don’t have the option, okay. BUT if you do have the option don’t reinvent the wheel! Take advantage of the other tools.

The answer is not always jQuery… sorry… the kool-aid tastes good, but sweet tea is pretty awesome too.

You should not be using jQuery in SharePoint if you are a moron…

Let’s finish up the blog on a high note… Yes.. it’s true, I sometimes type things just to get a reaction… guess this section title might be a good example, but it feels good sometimes just to type the words that a lot of us think… So.. don’t be that guy!

Another good buddy of mine that works for Microsoft told me. “I loved jQuery in SharePoint…. until I had to support it.”. He went on to explain that some user was making several web service calls on a page using jQuery and then was calling Microsoft and COMPLAINING because the page took so long to load… DUH! What do you expect to happen when you are pushing that much data over the wire and are making that many web service calls at once!! It’s one thing to write that kind of code and accept it’s just going to take a while, it’s COMPLETELY another issue to do that and then complain when it’s not lightning fast!  Someone’s gene pool needs some chlorine.

So, I think this is a nice summary of the blog… DON’T be that guy… don’t be a moron. How can you stop yourself from being a moron? Ah.. glad you asked, here are some tips:

Think

Is jQuery the right solution to my problem? Is there a better approach? What are the implications and pitfalls of using jQuery in this situation?

Search

What are others doing? Does someone have a better solution? Is there a third party library that does the same thing I need?

Plan

Write good jQuery. Limit calculations and data sent over the wire and don’t reinvent the wheel when possible.

Test

Okay, it works well on your machine. Try it on others ESPECIALLY if this is for an external site. Test with empty data. Test with hundreds of rows of data. Test as many scenarios as possible. Monitor those server resources to see the impact there as well.

Ask the experts

As smart as you are, there are people smarter than you. Even the experts talk to each other to make sure they aren't doing something stupid. And for the MOST part they are pretty nice guys. Marc Anderson and Christophe Humbert are two guys who regularly keep me in line. Make sure you aren’t doing something stupid.

Repeat

So, when you think you have the best solution possible, repeat the steps above just to be safe.  Smile

Conclusion

jQuery is an awesome tool and has come in handy on many occasions. I’m even teaching a 1/2 day SharePoint & jQuery workshop at the upcoming SPTechCon in Boston if you want to berate me in person. However, it’s only as awesome as the developer behind the keyboard. It IS development and has its pitfalls. Knowledge and experience are invaluable to giving the user the best experience possible.  Let’s face it, in the end, no matter our opinions, prejudices, or ego providing our clients, customers, and users with the best solution possible is what counts. Period… end of sentence…

Posted on Tuesday, February 22, 2011 12:14 PM | Back to top


Comments on this post: You should NOT be writing jQuery in SharePoint if…

# Know when you don't know something
Requesting Gravatar...
Mark, Great post. I have a CS degree, but I am not a developer. I can usually spot bad code or at the very least I know when to say that I don't know. I like this post because for someone like me, it helps me have a conversation when I have to tell a developer, its not SharePoint, I promise.

Thanks,
Andrew
Left by Andrew Alaniz on Feb 22, 2011 12:39 PM

# Mr
Requesting Gravatar...
Mark:

As I just said to you in a DM: "This is scary: I think I agree with everything you say. Except about me being a 'pretty nice guy'."

There are some self-serving observations that I could make, but that might be seen as tacky. I would, however, recommend to anyone who enjoys your post to go back and read The Middle Tier Manifesto: An Alternative Approach to Development with Microsoft SharePoint.

M.
Left by Marc D Anderson on Feb 22, 2011 12:40 PM

# re: You should NOT be writing jQuery in SharePoint if…
Requesting Gravatar...
Damn! I was going to write a comment in my usual "glass is half empty" style, and now I read that I am a nice guy... (or maybe I just misinterpreted the meaning of "most").
I would add a common pitfall. You should not write 5 lines of jQuery if you can do the same thing with 20 lines of plain JavaScript (think maintenance).
Also, I am an adept of screen scraping, and I think it can be seen as both an old and a modern technique. SharePoint uses it for standard features, jQuery has it built-in, and it is a core tool for progressive enhancement (cf. the Visualize plugin for example). The final choice is a trade off between performance and convenience.
Left by Christophe on Feb 25, 2011 7:59 AM

# re: You should NOT be writing jQuery in SharePoint if…
Requesting Gravatar...
What I find the most interesting about this post is, you can abstract the word jQuery and the word SharePoint from this whole post and insert any language and platform in it's place.

From my experience with Batch, VBScript, VBA, PowerShell, these all seem to fit as well.

Left by Matt Bramer on Mar 18, 2011 11:39 PM

Your comment:
 (will show your gravatar)


Copyright © Mark Rackley | Powered by: GeeksWithBlogs.net