Dev Notes

Notes on Development with Microsoft Technologies

Quick Way to Load SPServices

2 Comments

In a lot of the projects I work on daily, I find myself using the SPServices library (by Marc Anderson) for a lot of different types of interaction. Sometimes, I wrap up all of the functionality within a content editor web part that can be exported and potentially placed in another location of the site. jQuery is pretty much always loaded on these sites, but the SPServices library may not be loaded. Here’s a quick way to make sure that it’s available for web parts that will make use of the library.

function insureSPServices(callbackFunction) {
	if($().SPServices == null) {
		// If SPServices has not been loaded, this will return as a null value and let us know we need to load the library. The jQuery
		// getScript method runs asynchronously, so we use the callback function for initial calls to the service
		jQuery.getScript("http://sharepoint/scriptcenter/jquery/jquery.SPServices-0.5.7.min.js", callbackFunction);
	} else {
		// SPServices is already loaded, so execute our callback function which contains the rest of the page initialization.
		callbackFunction.call(null, "Already Loaded");
	}
}

function InitializePage(data, textStatus) {
	// This is the callback function called by the insureSPServices function. 
	// Here is where I would place any web-service calls that happen on the loading
	// of the page. 
	// i.e. GetDocumentLibraryTotalCount(libraryName, countContainer);
}

$(document).ready( function () {
     // when we're ready, let's make sure that SPServices is loaded.
     insureSPServices(InitializePage);
});

This will make sure that the SPServices library has been loaded before attempting to make any calls to the library.

Advertisements

Author: Chris Quick

I have been a developer of web based solutions since early 2001 delivering solutions to a wide array of organizations using ASP, ASP.NET and SharePoint. I was introduced to SharePoint in 2003 when the consulting firm I worked for at the time introduced it into the workplace. I began working with MOSS 2007 as soon as Microsoft released the RTM version in November 2006. The platform was implemented at the organization I worked for in 2007 and went live in March of that year. I was tasked with the administration and ongoing development of the platform. I currently work as a SharePoint Architect with Artis Consulting, developing solutions for a wide variety of business problems. The goal of this blog is to share my discoveries developing solutions with SharePoint. I welcome your comments and feedback to any post -- and I welcome suggestions for future topics.

2 thoughts on “Quick Way to Load SPServices

  1. Pingback: IE9 RC Due this Month?; Windows Phone 7 3G Issues; Cloud Computing Predictions - SharePoint Daily - Bamboo Nation

  2. Pingback: SharePoint Daily » Blog Archive » IE9 RC Due this Month?; Windows Phone 7 3G Issues; Cloud Computing Predictions

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