Jason Specland: Consultant, Comedian

Making it up as I go along. Always.

Tag: Programming

Exporting XSLTListView Web Parts to Other Webs + In Place Records Management Enabled = Null Reference Exception

Here’s the scenario: In SharePoint, you want to view one or more lists from one web in a site collection in a different web on the same site collection. What do you do? That’s easy: You go to SharePoint Designer, go to the original list view, export the XSLTListView web part that represents that view, load it into your other web and place it in whatever web part page you’d like. Boom. Dashboard.

Unless, of course, you’ve got the In Place Records Management feature enabled. In that case, you’re screwed. Everything will look fine for a while. But eventually, all of the views of your original list will fail. You will see the dreaded:


Server Error in '/' Application.
________________________________________
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.Office.RecordsManagement.RecordsRepository.Records.GetCachedAllowManualRecordDeclarationSetting(CachedList list, Hashtable siteProperties) +3
Microsoft.Office.RecordsManagement.Controls.<>c__DisplayClass1.b__0() +164
Microsoft.Office.Server.Utilities.MonitoredScopeWrapper.RunWithMonitoredScope(Action code) +39
Microsoft.Office.RecordsManagement.Controls.DeclareRecordAction.GetDeclareRecordInfo() +158
Microsoft.Office.RecordsManagement.Controls.InPlaceRecordsRibbon.Page_PreRenderComplete(Object sender, EventArgs e) +74
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +11058669
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3671

Why, SharePoint!? WHY!? (Seriously, if I had a nickel for every time I shouted that…)

Well, with the help of our good friend .NET Reflector, let’s take a closer look at that stack trace, shall we?

Microsoft.Office.RecordsManagement lives in Microsoft.Office.Policy.dll. I open it up and take a look at GetCachedAllowManualRecordDeclarationSetting(CachedList list, Hashtable siteProperties). Not much there…

[sourcecode language=”csharp”]
internal static bool GetCachedAllowManualRecordDeclarationSetting(CachedList list, Hashtable siteProperties)
{
if (list.RecordsUseListSetting)
{
return list.RecordsAllowManualDeclareSetting;
}
return GetCachedAllowManualDeclarationSiteSetting(siteProperties);
}
[/sourcecode]

Nothing that’s going to throw a NullReferenceException unless the cached list itself is null. So let’s travel up the stack a bit to Microsoft.Office.RecordsManagement.Controls.<>c__DisplayClass1.b__0().

[sourcecode language=”csharp”]
public void b__0()
{
SPList list = SPContext.Current.List;
if (list != null)
{
CachedArea rootArea = CacheManager.GetManagerForContextSite().ObjectFactory.RootArea;
CachedList cachedList = CommonUtilities.GetCachedList(SPContext.Current.Web, SPContext.Current.ListId);
if (list != null) // <- WTF!? (This comment added by me.) { this.declareRecord.iprList = Records.GetCachedAllowManualRecordDeclarationSetting(cachedList, rootArea.Properties); if (this.declareRecord.iprList) { this.declareRecord.userHasRights = Records.CachedDoesUserHavePermissionsToDeclare(list, rootArea.Properties); } } } } [/sourcecode] Ah HA! The cached list is null! The inner null check is clearly meant to check the null-ness of cachedList and not list! So, thanks to a clear, obvious bug in SharePoint, you can’t use XSLTListView Web Parts on other webs in your site collection if In Place Records Management is turned on. Unfortunately, the only solution is to turn it off.

Note: I upgraded my development SharePoint instance to the February 2012 CU. This bug still exists in assembly file version 14.0.6116.5000.

My Upgrade Actions Can’t Have a Comment? Really, SharePoint?

So I’m doing new work on a SharePoint feature that’s already deployed, so naturally, I have to do some upgrade actions to deploy some new fields to a content type. There are a few fields to add, and since the AddContentType field doesn’t have any naming elements, I decide to throw in some comments before each entry, so I know which field is which and I make sure I haven’t forgotten any. Everything compiles fine and Update-SPSolution works fine. Then, I try to run my upgrade and I get the following error:

Really, SharePoint? You can clearly see that it’s a comment, and yet you still just barf on it rather than just ignoring it. WHY!?

Adventures in Uninformative SharePoint Messages, or The Sandbox is Too Damn Busy for Your List Receiver

Still here? Great. So, here’s the situation. Tell me if it seems crazy. There are things that, to me as a programmer, don’t seem crazy at all, while more seasoned SharePoint developers would look at what I was doing and scream, “Are you mad?” Like iterating through SPList.Items() in a foreach loop without first putting them into an SPListItemCollection, for example.

Anyway, I have the following situation: A project tracking list, which has a “Primary Technical Lead” field and a “Secondary Technical Lead” field, both of which are single User-type. I need to create a “Team Members” field, which is a multi-User type. Upon creation of that field, I need to populate it with both the value from the Primary and Secondary tech lead fields. Naturally, I chose to implement this one-time setup in the FeatureActivated event receiver.

Additionally, I want to make sure that whenever a tech lead is changed, that that change is reflected in the Team Members field. I also want to make sure that you can’t delete someone from Team Members if they’re still a tech lead. Naturally, I chose to implement this as an ItemUpdating event receiver.

Every time I tried to activate the solution, however, I always got the following error:

Unhandled exception was thrown by the sandboxed code wrapper’s Execute method in the partial trust app domain: The sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request.

Well, excuuuuuuuuse me!

Now, I wasn’t entirely un-clever about this. Of course my ItemUpdating event was firing when I was trying to populate the TeamMembers field. (Okay, I’m kind of lying. There was a lot of blank “WTF?” going on in my head before I realized that the ItemUpdating event was firing. Then I went through the feature activation in the debugger, and noticed that the exception was thrown when I called SPListItem.Update() or SPListItem.SystemUpdate(), which I tried in desperation.) But my ItemUpdating method should have just quickly exited upon not seeing the fields that it was worried about in the AfterProperties. Still, worried about my logic, I commented out my routine line by line. Until finally, my entire ItemUpdating receiver looked like:

[sourcecode language=’c#’]
public override void ItemUpdating(SPItemEventProperties properties) {
// Lots of commented-out lines
base.ItemUpdating(properties);
}
[/sourcecode]

And still, Sandbox is Too Damn Busy.

Then I went to the List Receiver’s Elements.xml file, and commented out the <Receiver…><Type>ItemUpdating</Type>…</Receiver> part. And lo and behold, the Sandbox would suddenly take my calls again.

I then created the simplest test case. One list. One ItemUpdating event receiver which does nothing. One FeatureActivated event receiver that updates a list item. Activation == game over.

Now I understand why updating a ListItem that has an ItemUpdating receiver from a FeatureActivated receiver might be a problem. But, once again, SharePoint has vomited forth one of the least useful error messages possible.

Steve Jobs Made Me a Better Developer

“It seems that perfection is reached not when there is nothing left to add, but when there is nothing left to take away”. — Antoine de Saint Exupéry

Steve Jobs has passed away. Everyone is talking about him in terms of what he did for the world. His contributions have long since passed “significant” and can without reservation be called legendary. But that will be talked about elsewhere, extensively, and by people far more eloquent than myself.

I’m just going to talk about what Steve Jobs did for me. His philosophy made me a better developer. His relentless pursuit of simplicity and ease of use for the end user changed the way I looked at IT.

I used to be a unix administrator. I used to revel in my knowledge of the arcane. I read the BOFH with glee. I used Linux, but I was always struggling with little things. Editing config files. Building from source. But smart people like me can figure it out, right? And for the most part, I did.

Then I got my first Mac, with OS X. It had the comforting unix command line which got me in the door, but I ended up using Terminal less and less. It just worked. I could still mess around with my computer, but now I didn’t have to deal with the mess if I didn’t want to.

Then the iPhone came. It’s hard to remember the world of cell phones before the iPhone. It seems like we’ve always lived in a world of touch screens. But before iPhone were dark days. I had a Blackberry for work, and I hated it with a burning hot hate. iPhone cut through the tyranny of the cell carriers and showed what was possible if someone, anyone, would just spend a few minutes giving a damn about their users.

Gradually, my entire perspective changed. Steve Jobs showed me that making computers work in an intuitive way is not only possible, but imperative. He showed me that programs that work in simple, expected ways can not only eliminate frustration, but can even be delightful. “User Error” isn’t dumb people. It’s software that doesn’t know how to work with people because no one gave enough of a damn to make it work the way ordinary, non-computer people do.

I work in IT, but I don’t work for IT. I work for some of the most brilliant physicians and researchers in the world. I work for an army of dedicated nurses and medical staff. These people are smart, sophisticated, and know what they’re doing. But they are not “computer people” and any system that requires them to be one in order to get it to work is a failure. There is a PEBKAC error, but the “P” in this case is the programmer.

I’ve been fighting a fight at work recently. No need for specifics, but I was given something from the higher-ups to implement. Implementing it as given would have been easy. But I knew it would be a huge and frustrating roadblock for users. It wasn’t my place but I fought for the users, and while I haven’t won a complete victory, I did get some concessions, and I got some people to really think about how what we do affects these users. It was the spirit of Steve Jobs that compelled me to make that stand.

I’m not a great programmer. I have neither the time nor the resources to dedicate to the loving craftsmanship that’s necessary to achieve perfect Jobsian simplicity. But I care. I fight. I work harder. And I do so because of Steve Jobs and his vision.

Thank you, Steve.

Sharepoint: Updating Workflow Task Causes NullReferenceException (a fix)

I’ve got a workflow where a project manager approves a project (that’s one kind of task), or can send it out for technical review (another kind of task), or can request more information from the user (yet another task). Each of these tasks have their own Content Type, and their own custom task edit form in ASP.NET. (Because I hate InfoPath with a burning white hot hate.)

Whenever someone uses one of these custom edit forms, however, I get a NullReferenceException. The stack trace always puts it in the non-public SPListItem.PrepeareItemForUpdate method. It doesn’t matter how I change the task list item. Using SPWorkflowTask.AlterTask, or just SPContext.Current.ListItem followed by Update all threw the NullReferenceException.

To Google I go! However, most of the results said that it had to do with not having a definition in the Content Types. That wasn’t my problem, however, since not only did my Content Types have FieldRef’s, but they were very much full.

At my wits end, I broke out the .NET Reflector and decompiled the Microsoft.SharePoint assembly. Here’s what I found:

As you can see (well, kind of see… click on it to see more clearly), this would only happen in a WorkflowTask. If the list item’s “Completed” field is null, the cast to bool throws a NullReferenceException. This is probably the kind of thing that can’t happen in SharePoint Designer, but only would occur when someone is mucking about in Visual Studio.

To fix it, make sure you set the “Completed” field. I do this:

[sourcecode language=’c#’]
private void SetTaskStatus(string status, bool taskIsComplete) {
SPListItem taskItem = SPContext.Current.ListItem;

Hashtable taskData = new Hashtable();
taskData[SPBuiltInFieldId.TaskStatus] = status;
taskData[SPBuiltInFieldId.Completed] = taskIsComplete;
if (taskIsComplete) {
taskData[SPBuiltInFieldId.PercentComplete] = 1;
}

SPWorkflowTask.AlterTask(taskItem, taskData, true);
}
[/sourcecode]

Or, if you work for Microsoft, you could do us a favor and make sure that you null check that line and default to false. (It’s entirely possible that this has been fixed by SP1, but my production server doesn’t run that yet, so I can’t either.)

Unit Testing: Rhino Mocks, Ordered Mocks, and Events

Tired of all that improv show talk? Well, now it’s time for me to get my geek on. Non-computer geeks feel free to tune out. This is actually more along the lines of “put it out there for the sake of future Googlers” than a serious discussion.

I’ve had test-first religion for some time now, but deadlines have made me something of a lapsed tester. After finding a pretty bad-ass bug in my code, I got religion again, admitted I was a sinner, and went forward in Testing. I am redeeming myself by writing a few hundred unit tests of already existing code.

My preferred mock framework is Rhino Mocks. Now I’m all about the AAA (Arrange, Act, Assert) syntax. 99.9% of my tests (seriously, I’ve got over a thousand of them) are AAA. Most of my tests are unordered. I just want to test that something was called, and I don’t care what was called before or after it.

But you can’t do AAA syntax when you’re doing ordered tests. It hasn’t been a problem thus far, but I ran into a problem when I started testing objects that hooked up events in the constructor: So my code looked like this:

[sourcecode language=”c#”]
namespace RhinoMocksOrdredWithEvents {
public interface IOrderedThing {
void FirstThing();
void SecondThing();
void SomethingElse();
event EventHandler OnThingHappened;
}

public class MyTestClass {

private IOrderedThing MyOrderedThing { get; set; }

public MyTestClass(IOrderedThing orderedThing) {
MyOrderedThing = orderedThing;
MyOrderedThing.OnThingHappened += new EventHandler(MyOrderedThing_OnThingHappened);
MyOrderedThing.SomethingElse();
}

public void DoThingsInOrder() {

MyOrderedThing.SomethingElse();

MyOrderedThing.FirstThing();
MyOrderedThing.SecondThing();
}

private void MyOrderedThing_OnThingHappened(object sender, EventArgs e) {
return;
}

}
}
[/sourcecode]

And here’s the test to make sure that FirstThing() and SecondThing() are called in order:

[sourcecode language=”c#”]
namespace RhinoMocksOrdredWithEvents {
[TestClass]
public class OrderedMockTests {

[TestMethod]
public void TestThingsAreRunInOrder() {
// Arrange
MockRepository mocks = new MockRepository();

IOrderedThing mockOrderedThing = mocks.DynamicMock();

MyTestClass classUnderTest = new MyTestClass(mockOrderedThing);

using (mocks.Record()) {
using (mocks.Ordered()) {
mockOrderedThing.Expect(thing => thing.FirstThing());
mockOrderedThing.Expect(thing => thing.SecondThing());
}
}

mockOrderedThing.Replay();

// Act
classUnderTest.DoThingsInOrder();

// Assert
mockOrderedThing.VerifyAllExpectations();
}
}
}
[/sourcecode]

Be not fooled by my spurious use of the //Arange, //Act, and //Assert comments. This is most definitely not the AAA syntax.

So when I tried to run this test, I got the following error:


Test method
RhinoMocksOrdredWithEvents.OrderedMockTests.TestThingsAreRunInOrder
threw exception:
Rhino.Mocks.Exceptions.ExpectationViolationException:
IOrderedThing.add_OnThingHappened(System.EventHandler`1[System.EventArgs]);
Expected #1, Actual #0..

What what what!? I never told Rhino Mocks that I expected the event to be wired! And anyway, it is wired, right there in the constructor, see? Then I comment out the event hookup, and the test works! So off I sauntered to the Rhino Mocks mailing list all like, “Hey guys, I don’t mean to be all smug and stuff, but all y’all got a bug.”

Thankfully, Tim Barcz on the mailing list set me straight.

Yes, Virginia, you can do the AAA syntax in an ordered mock, and this is how you do it:

[sourcecode language=”c#”]
namespace RhinoMocksOrdredWithEvents {
[TestClass]
public class OrderedMockTests {

[TestMethod]
public void TestThingsAreRunInOrder() {
// Arrange
//MockRepository mocks = new MockRepository();

IOrderedThing mockOrderedThing = MockRepository.GenerateMock();

MyTestClass classUnderTest = new MyTestClass(mockOrderedThing);

mockOrderedThing.GetMockRepository().Ordered();

mockOrderedThing.Expect(thing => thing.FirstThing());
mockOrderedThing.Expect(thing => thing.SecondThing());

// Act
classUnderTest.DoThingsInOrder();

// Assert
mockOrderedThing.VerifyAllExpectations();

}
}
}
[/sourcecode]

Which works no matter what strange stuff you want to do in the constructor.

Much thanks to Tim Barcz for setting me straight. Here is the original thread on the Rhino Mocks mailing list.

Debugging WCF Services In Visual Studio 2008 in 32-bit Mode on a 64-bit Architecture

This is for my own reference, (and for the sake of anyone Googling) since this has been driving me crazy for months. Feel free to ignore if you’re not a geek. Feel free to correct it if you are.

When building a WCF project for the x86 architecture on an x64 machine/OS (like, say, Windows 7), you will get a BadFormatException when you try to debug it. This is because WcfSvcHost.exe will run in the mode defined by your architecture, regardless of your particular build environment. Microsoft says that this is working as intended. I say boloney, but I’m a VS version behind now. (I haven’t tested VS 2010 yet.)

Here is a link to part of the solution.


corflags /32BIT+ WcfSvcHost.exe

However, the signature of this assembly is now broken, and won’t load. So to bypass that:


sn -Vr WcfSvcHost.exe

to remove strong name signature verification.

DANGER:
Don’t do that unless you know what you’re doing! That opens up a huge security risk. Do not do that in a production environment! Only do it on your dev box!

Why not just build the app in x64 or (even better) Any CPU? Because I can’t get Oracle.DataAccess in 64-bit.

Programming Praxis: Goldbach’s Conjecture

Since I mentioned in my last post that I live in fear of the tech interview, I decided to do something about it rather than live in fear. To that end, I subscribed to a website called Programming Praxis, which publishes short programming problems to sharpen your saw on. (Well, I’d actually been a subscriber for a while, but it’s just now that I’ve actually took the time to do my own saw sharpening.)

Geek Content Ahead. Feel Free to Ignore.

Today’s Praxis is on the Goldbach Conjecture which states that any even number greater than 2 can be expressed as the sum of two primes. The challenge is to write a program that will take in an even number, and spit out the two primes that can be added together to make it.

I realize my solution is extremely naive, but it’s the first time in a long time that I’ve actually worked on programming problems other than “get data from database a, transform it somehow, and put it in database b.” Also, for extra difficulty (and speed, although my algorithm is the real speed hangup in this case) I wrote it in C.

For the one or two people still reading, here is my extremely naive solution, cobbled together while watching West Side Story on TCM.

[sourcecode language=”cpp”]
#include
#include

void show_usage();
unsigned long *create_sieve_to_number(unsigned long number);

int main (int argc, const char * argv[]) {

if (argc != 2) {
show_usage();
return -1;
}

unsigned long number = atol(argv[1]);

if ((number % 2) != 0) {
show_usage();
return -1;
}

unsigned long *sieve = create_sieve_to_number(number);

for (unsigned long i = 2; i < number; i++) { if (sieve[i] == 1) { for (unsigned long j = i; j < number; j++) { if (sieve[j] == 1) { if (i + j == number) { printf("Solution found: %d + %d\n", i, j); return 0; } } } } } printf("no solution found! pick up your Fields Medal!\n"); return 0; } void show_usage(void) { printf("usage: goldbach [even number]\n"); } unsigned long *create_sieve_to_number(unsigned long number) { unsigned long *sieve; sieve = (unsigned long *)malloc(sizeof(unsigned long) * (number + 1)); for (int i = 0; i < number; i++) { sieve[i] = 1; } for (unsigned long i = 2; i < number; i++) { if (sieve[i] == 1) { for (unsigned long j = i * i; j < number; j = j + i) { sieve[j] = 0; } } } return sieve; } [/sourcecode] Edit: See what happens when you’re watching TV when you’re doing your homework? The flags marking numbers as prime can be a bool (or even a bit) and most certainly does not have to be an unsigned long. Yay for massive, massive wastes of memory! (But hey, I was watching West Side Story…)

The Dreaded Technology Test

A recent post on Coding Horror about “Non-Programmer Programmers” kind of terrified me. Not because I think that my job is going away, and not because I think my skills aren’t up to snuff, but because the technical interview terrifies me like nothing else.

Jason, with performance anxiety? Yes, it’s true!

As a performer, I come from the world of improv, where the mantra is “Just make it up! What you say will always be correct!” Naturally, this level of bullshittery, while it might get you through a real interview, will never do in a technical interview where you will be eviscerated if you don’t know the systems you claim to know. (Or even some detail of a system you really do know, but had never previously used.) Now I’ve never put something on my résumé that I did not actually use, but in some cases, it’s just been a while and I need a few minutes with my friend Google to get back up to speed.

Part of the problem is that the tech interview is like flying an airplane. Once you stall once, it’s really hard not to crash and burn. You just get more and more flustered until you can’t even answer questions about things that you do know extensively.

I had an interview like that just before I interviewed for my current job. It was October/November 2001, and as you could imagine, New York City was on edge and not in a hiring mood, especially in the “dot-com” world which had gone bust. I interviewed downtown with a famous phone company for a unix administrator job. I can’t remember what question started the stall… I think it was something about rpc, or what port something listened on… But after that, I was dead in the water. They asked me a bog-simple RAID question that I totally blew. As they escorted me out of the building, one of the managers sypathetically said to me, “It must be hard getting a job in this economy…” Shellshocked, I agreed.

Reading that article on Coding Horror, and remembering that interview, kept me awake far too late last night. Again, I’m reasonably confident that my job’s not going anywhere, and I’m confident in my abilities, but one has to plan for these things.

Then today, sitting in the office that I’ve occupied for almost nine years now, I remembered a tech interview that was tough, but I knocked out of the park. (In particular, I remember totally killing a question about DNS.) Naturally, its the one that got me the job I have today. And they seem to like me after all these years, to the point where they actually let me write production code.

Scary though the tech interview may be, if I keep my skills up, keep my cool, and remember how to pull out of a stall, I should be okay. Besides, the “weed out” questions on that Coding Horror article are laughably easy. And if your tech interview feels like the Spanish Inquisition, did I really want to work for you in the first place?