Error: 'b' is null or not an object

This error has caused me so much pain when trying to use SharePoint’s JavaScript client-side object model (CSOM), so, in case I have it again:

  1. Check that the function exists.
  2. Check that the function is named correctly.
  3. Make sure that you’re not using “this” in the call to createDelegate, as detailed here.
    • Right: Function.createDelegate(this, onSuccessMethod)
    • Wrong: Function.createDelegate(this, this.onSuccessMethod)

    I don’t know why this difference should cause this error, but I’ve proved it true.

If you know of other causes of this error, let me know, I’ll add it to the list. I love the experience of debugging JavaScript…

Error: 'b' is null or not an object

Use jQuery to populate and hide fields in an EditForm.aspx

We’ve got an interesting requirement. We’re using a SharePoint list to store documents which are related to K2 SmartObjects. One of my colleagues is building a custom user control that’s going to show information from this SmartObjects, and associated documents. We want to allow users to

  • click to add a document to our smart object entity
  • upload a file to a library
  • fill in some details
  • automatically associate that file by a GUID

Sounds complicated? Actually, it’s not so hard! Continue reading “Use jQuery to populate and hide fields in an EditForm.aspx”

Use jQuery to populate and hide fields in an EditForm.aspx

Get query string parameters with Regular Expressions (in JavaScript)!

I’ve said it before, but not quite so explicitly – it’s easiest to get query string parameters with a regular expression. I keep seeing big JavaScript functions looping through and splitting the parameters up, and that might be fine if you want to store those and use them repeatedly – but if not, regexes are your friend.

This function will return the value of a parameter. If the parameter requested isn’t in the query string, it returns null.If the parameter doesn’t have a value (e.g. debug in ...&debug&...) it returns an empty string.

function GetQueryParam(parameter){
var p = escape(unescape(parameter));
var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
var match = regex.exec(window.location.search);
var value = null;
if( match != null ){
value = match[1];
}
return value;
}

Get query string parameters with Regular Expressions (in JavaScript)!

What is MSOLayouts_MakeInvisibleIfEmpty()

I was looking through the code of SharePoint’s Blank Web Part page layout, and I kept finding elements called _invisibleIfEmpty. Now, I’d noticed that some page layouts, such as the Splash page layout had borders on their web part zones which were only shown if the zone contained a web part. I was interested in how this might work. People keep asking for web parts with borders, preferrably ones with rounded corners. Continue reading “What is MSOLayouts_MakeInvisibleIfEmpty()”

What is MSOLayouts_MakeInvisibleIfEmpty()

Corrupt Breadcrumbs in the in PageAreaTitleFrame

I was tempted to call this “The incredible case of the bust rendering of SharePoint pages”, but it seemed a bit long, to be honest. This is a bit of a detective story (and if I’d the developer who built this in the first place, there’d be a murder) so stick with me… Continue reading “Corrupt Breadcrumbs in the in PageAreaTitleFrame”

Corrupt Breadcrumbs in the in PageAreaTitleFrame

Showing Query String parameters in a page in SharePoint

I came across an interesting little problem with putting query string parameters for an HTTP request into a SharePoint page.

I was using a Dataview web part, which accepts query string parameters into the filter that it runs over the data that it’s going to display. That’s pretty cool – I had a list of items with a status column, and I wanted to be able to filter the items based on that status, and that the status would come from the query string. However, on the page, I also wanted to show a title with what was being filtered by in it. So, for example, my url might read:

http://server/site/page.aspx?status=Ready

And I wanted the title in the page to read “Filtering by ‘Ready‘”.

What I didn’t appear to be able to do was to just use the request object . Something like

<%= Request.QueryString.ToString() %>

won’t work as code blocks aren’t, apparently, allowed in the file – the error message is “code blocks are not allowed in this file”. Okay, given that SharePoint designer is supposed to open these pages up to ‘Power Users’ I do get why code blocks aren’t allowed. But I really wanted to write code, and there had to be a way to do that. Well, Kirk Allen Evans has figured out how – you have to enable them in the PageParserPaths section of the web.config file. (It’s strange how easy it is to forget the web.config file that controls SharePoint sometimes). Just to repeat his example (in case blue and red on black is difficult to read):

<PageParserPaths>
<PageParserPath VirtualPath="/pages/*" CompilationMode="Always" AllowServerSideScript="true" />
</PageParserPaths>

This example allows code blocks for all files under the /pages directory of my web application – it’s worth noting that this is for the web application in IIS, not a SharePoint site collection or the like.

Okay, so that works – great. However, there are security implications, and this is probably best only used for development environments. I’ve written a bit about under what conditions our inline code will run.

In our instance our customer is kind of reluctant to make changes on their server. Changing the web.config file would be a bit of a big deal. (As a side note, this is a bit restrictive – I think a blog posting on that is due sometime). So what could we do without a server footprint?

Well, without code in the page, we would need a web control to call in that page – that would work also. It’d be trivial to write one. However, that requires deployment, marking as safe in the web.config file, etc., so really that just pushes the problem into another file.

Thus, I ended up getting a bit ‘old skool’ – JavaScript in the page to write the value from the query string on the client. I came up with:

<script type="text/javascript">
var query = window.location;
var regex = /letter=([^&=]+)/i;
var match = regex.exec( query );
if( match != null ) {
document.write( "Filtering by '" + match[1] + "'" );
}
</script>

Made a function, this became:

function getQueryParam(key) {
var regex = new RegExp(key+"=([^&=]+)","i");
var match = regex.exec(
window.location );
if( match != null ) {
return match[1];
} else {
return null;
}
}

What this code does is it gets the page’s url (window.location), runs a regular expression looking for the ‘letter’ parameter, and if it find one, it outputs some text to the document. A bit noddy, but simple and it works – with no server footprint. Of course, you do need JavaScript enabled…

Showing Query String parameters in a page in SharePoint

Add an event handler

I keep using this snippet of code:

if( window.addEventListener ) {
window.addEventListener("load", fn, false );
} else if( window.attachEvent ) {
window.attachEvent("onload", fn );
}

It’s just a tidier way of attaching a function ‘fn’ to an event. It also allows multiple handlers per event. There could be another ‘else’ where we just assign the function to an event (e.g. window.onload = fn ) but that would only support one handler per event.

Add an event handler