Getting class name of variables in SharePoint’s Javascript client object model

Have you suffered from the pain of debugging SharePoint Client Object Model (COM) in Javascript? If you haven’t.. I think you soon will.

The SP COM introduces lots of classes in Javascript. Although you can add some basic intellisense for VS for the SP COM (see http://msdn.microsoft.com/en-us/library/ff798328.aspx), you will quickly find that it is no where near good enough.

I found that being able to get the class name of a variable in Javascript helps when debugging. In IE8, you can do this with: myObj.constructor.__typeName.

For the code below for example, doing alert(appointments.constructor.__typeName) would tell me that it is of type “SP.ListItemCollection”, and doing alert(appointmentEnumerator.constructor.__typeName) would tell me that it is of type “SP.ArrayListEnumerator”. This also works for simple types such as Number and string.

function getAppointmentOnSucceeded(sender, args)
{
	var appointmentEnumerator = appointments.getEnumerator();

	while (appointmentEnumerator.moveNext())
	{
		var appointment = appointmentEnumerator.get_current();
		$get("appointmentDetails").innerHTML += "</br><b>" + appointment.get_item('Title') + "</b><br/>" + appointment.get_item('Description');
	}
}
Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in Client Object Model, SharePoint. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s