My SharePoint "Soylent Green" Moment

When you first start working with SharePoint, like any other new (to you) technology, you turn on the fire hose and start drinking down all the books patterns, samples and articles you can find. The SharePoint learning curve, which forms roughly a 90 degree angle to your former path of progress, is bigger than most. Perhaps because of that, or perhaps because it provides a convenient excuse, you tend to swallow down whole patterns and ways of thinking about web development that may be contrary to how you would approach normal ASP.NET development.

One case in point: SharePoint web parts. To get your first web part up and running you will likely look to samples and documentation on line. MSDN has lots of good samples to choose from like this one. In this sample you will be shown how to add controls to your web part by overriding the CreateChildControls() method like so:

   1: protected override void CreateChildControls()
   2: {
   3:      //Create text box
   4:      newTitle = new TextBox();
   5:      newTitle.Text = "";
   6:      Controls.Add(newTitle);
   7:  
   8:      //Create button
   9:      saveTitle = new Button();
  10:      saveTitle.Text = "Set Web Part Title";
  11:      saveTitle.Click += new EventHandler(saveTitle_click);
  12:      Controls.Add(saveTitle);
  13: }

Kind of feels strange at first, not like adding web controls to page markup in ASP.NET, or dragging one on to the form using the visual designer if that's the way you roll. But, then, you suck it up and soldier on, "this is SharePoint development men, it's just different". After all, the options presented at this point like creating custom user controls which then you have to deploy to SharePoint don't make your life, or the learning curve, any easier so you accept and move on.

Then, at some, not-too-distant, future point you get a bug report that goes something like this: "The grid sorting is flaky, sometimes it works and sometimes it loses its sort and goes back to default." Or: "when I select an item in the grid to delete, an item gets deleted, but it's not the same one I clicked on". I tripped over these bugs myself as I was preparing to sign off on dev and hand off to UAT. As I traced the problem, stepped thru the code I realized what was happening. I was storing my current sort order and expression in ViewState to persist the data between calls, familiar approach from ASP.NET projects of the past. ViewState, however was not consistently available to the code that needed it to re-set the sort on post back. That is when it all hit me, in a true Soylent Green moment of stupid realization of the obvious. Only, instead of "It's made of people!" my shocked moment of clarity was: "It's made of dynamic web controls!"

I clearly remember occasions on previous projects where fellow devs came to me with odd problems with ViewState, either not returning what it should or the page post failing validation, or controls not persisting, or posting back, the values that were expected. I would ask if they were dynamically loading controls and, when they replied they were, I would say: "don't do that", or something "clever" like "just because it's possible doesn't make it a good idea". Now here I was being bitten by the same stupid bugs. Truth was, in 95% of the cases we didn't really need to load controls dynamically, it was far easier to solve the problem another way then to take on the challenges of managing dynamic controls such as making sure the same exact controls were populated on each post back in the Page.Init() method, or else ViewState and Postback data wouldn't get populated correctly etc. In the rare occasions we deemed it necessary to load controls dynamically, we were, at least, fully aware of the ramifications and dealt with them as needed.

So, I don't know if it's just me, or if others have experienced during the SharePoint learning "wall", that you almost forget that it's all ASP.NET under the hood, and the "best practices" and lessons you have learned thru the years just get chucked out the window because this is SharePoint.

 

© Copyright 2009 - Andreas Zenker

No Comments