jQuery – What I Wish I Would Have Known When I Started… Mark Rackley – Solutions ArchitectEmail: mrackley@gmail.comBlog: http://www.sharepointhillbilly.comTwitter: http://www.twitter.com/mrackley
AgendajQuery OverviewjQuery & SharePoint – What’s the Point?Deployment, Maintenance, UpgradesSPServicesDevelopment & DebuggingExamples2
What is jQuery?JavaScript utility library - no cross browser issues if you are carefulQuickly write interactive and USABLE applicationsYou don’t have to deploy assemblies (perfect for tightly controlled environments)
What Skills do I need?JavaScript (duh)CSSA development background helps!It IS codeUses development constructsIf you can’t write code, your ability to do magic will be limited to what you can copy/pasteCAML, CAML, CAML… Sorry…Ability to think outside the boxUse all the pieces together
SharePoint & jQuery? Why?Resolves many SharePoint complaints without having to crack open Visual Studio“It looks like SharePoint” becomes “That’s SharePoint?”“SharePoint can’t do that out of the box” becomes “Sure, no problem”“That will take 3 weeks???” becomes “2 days? Awesome! I love you… here, please accept this bonus for being such a wonderful developer”
jQuery makes your SharePoint applications USABLE
Common MythsIt’s not secureBusted… It uses SharePoint security. All scripts run with privleges of current userIt doesn’t perform wellPlausible… It performs very well if you use it correctly in the right situationsI can’t elevate privileges if I need toConfirmed…
What about…I can’t interact with SharePoint Lists and LibrariesBusted… You can call Web Services with JavaScript (SPServices is a wonderful jQuery library that wraps SharePoints Web Service calls)It has no real use in my environment because of “x”Busted… Quickly prototype and tweak web parts before writing in Visual Studio. In fact, in some environments it is the only development option.
Yeah But… It’s not deployableCreate central script document library for jQuery scriptsUpload new versions as neededTurn on versioning and store multiple versionsUse metadata to keep track of script informationStore on file systemDeploy with solution packageIncreased performance of loading from file system
Okay… but… It’s not maintainableScript PlacementDon’t put your script source in the CEWPDon’t hardcode scripts in your MasterPagesAgain… deploy scripts to central script document library or to file system with solution packageIf stored in doc lib, turn on versioning to easily roll-back if needed.
Moving Between Environments and Upgrading to 2010It just works… (woo hoo)Uses list names and not GUIDs so no issues moving from dev to prodFor the most part works identical in 2007 and 2010 (I’m sure there’s the occasional issue but I’ve never run into it).Might want to tweak in 2010 to take advantage of 2010 features (ribbin, modal pop-ups)
Okay then…jQuery must be the SharePoint Silver BulletBusted…It does “expose” business logic to user if they dig aroundIt executes on the client side and can perform slow if manipulating larges amounts of dataBe extra careful for external facing applicationsYou can’t do EVERYTHING with jQuerylike…Timer JobsWorkflows (although it can eliminate the need for some)Event HandlersElevate PrivligesEasily interact with all business systems
jQuery is another tool for the SharePoint Developer’s toolkitUnderstand the limitationsUse it wisely
Development BasicsjQuery methods are executed using jQuery() or $()You don’t have to use $(document).ready() Use $() directly$(‘#elementID’) to interact with any elementDivs, tables, rows, cells, inputs, selects.. Etc.. Etc.. Etc… <table id=‘myTable’></table> $(‘#myTable’).append(‘<tr><td>add a row</td></tr>’);$(‘#elementID’).val() gets values of inputs<input type=‘text’ id=‘myTextBox’ >$(‘#myTextBox’).val();$(‘#elementID’).val([value]) sets the values of inputs$(‘#myTextBox’).val(‘Hello World’);
Development Basics$(‘#elementID’).html() gets the raw html within an element (great for divs, spans, tables, table cells, etc.)<div id=‘myDiv’>Hello World</div>$(‘#myDiv’).html() = ‘Hello World’$(‘#elementID’).html(“<html text>”) to set raw html$(‘#myDiv’).html(‘Hello World! Welcome to SPSSTL!’)$(‘#elementID’).hide(), $(‘#elementID’).show(), and $(‘#elementID’).toggle()$(‘#myDiv’).hide()$(‘#myDiv’).show()$(‘#myDiv’).toggle()
Development Basics$(‘#element’).addClass(“className”);$(‘#element’).removeClass(“className”);$(‘#element’).attr(“attributeName”);$(‘#element’).attr(“attributeName”,”attributeValue”);SharePoint Form Fields$(‘input[title=“Title”]’).val();$(‘select[title=“Title”]’).val();GET O’REILLY’S JQUERY POCKET REFERENCEhttp://oreilly.com/catalog/0636920016182
More Dev BasicsEach loops$('table[class="myTable"]').each(		function () {			$('#' + this.id).hide();		});CDATA is a life saverWrap values in CDATA tags:"<![CDATA[" + data + "]]>"
SPServicesjQuery library to execute SharePoint’s Web ServicesGet List ItemsAdd/Update List ItemsCreate lists, content types, site columns, etc.Create document library foldersCreate cascading drop down lists
Get user groups and permissions
Potentially call ANY SharePoint Web ServiceSPServicesWorks identical in SharePoint 2007 and SharePoint 2010Runs with permissions of current userCommunicate across sites
SPServicesGetListItems
SPServicesUpdateListItems
SPServices - FYIUse internal field names Returned values have “ows_” in front of field names$(this).attr("ows_Title");Lookup & People fieldsValue returned as “ID;#Value”  (1;#Field Value)Check for new item ID on item add to make sure item added correctlyvarnewID = $(xData.responseXML).find("[nodeName=z:row]").attr("ows_ID");
Development TipsDevelop for performanceLimit rows returned using CAMLAvoid screen scraping (Use SPServices)Don’t call web services until the data is needed.Use those ID’s to your advantage<td id=‘ListName-4’>Attributes are awesome<input type="text" title="list title 4" id="4">$('#4').attr("title");$(‘input[title=“list title 4”]’).val();
Writing jQueryPick whatever editor makes you happy…SharePoint DesignerNo need to upload scriptsVisual StudioI don’t use it, so can’t speak to itAptana (actual JavaScript IDE)Gives you some intellisenseNotePad++Good bracket matching which tends to bite you in the butt
Debugging… ugh.. It ain’t prettyAlerts.. Alerts.. Alerts.. Alerts…Developer ToolsSet breakpointsEvaluate expressions and variables inline (like REAL debugging!)Firebug for Firefox Considered to be best of the free tools out thereIE Developer Tools Comes installed on IE 8+
Debugging… Common ErrorsUsually it means your library didn’t get loadedObject ExpectedObject doesn’t support methodMake sure you don’t load scripts more than onceNull Object referenceCheck your bracesMake sure you end lines with “;”Check for missing quotesWhen all else fails, delete your script and rebuild it slowly in chunks, testing as you go.
DEMOS
How to get it…jQueryhttp://jquery.com/SPServiceshttp://spservices.codeplex.comSuper Awesome 3rd party libraries that integrate wellModal dialogs - http://www.ericmmartin.com/projects/simplemodal/Calendar - http://arshaw.com/fullcalendar/Charts - http://g.raphaeljs.com/
More jQuery ResourcesjQuery Pocket Referencehttp://oreilly.com/catalog/0636920016182CSS Pocket Referncehttp://oreilly.com/catalog/9780596515058/
HousekeepingPlease remember to submit your session evaluation forms after each session you attend to increase your chances at the raffleFollow SharePoint Saturday St. Louis on Twitter @spsstl and hashtag #spsstl
Thanks to Our Sponsors!Gold SponsorsSilver SponsorsRaffle Sponsors

SharePoint Saturday St. Louis - SharePoint & jQuery

  • 1.
    jQuery – WhatI Wish I Would Have Known When I Started… Mark Rackley – Solutions ArchitectEmail: mrackley@gmail.comBlog: http://www.sharepointhillbilly.comTwitter: http://www.twitter.com/mrackley
  • 2.
    AgendajQuery OverviewjQuery &SharePoint – What’s the Point?Deployment, Maintenance, UpgradesSPServicesDevelopment & DebuggingExamples2
  • 3.
    What is jQuery?JavaScriptutility library - no cross browser issues if you are carefulQuickly write interactive and USABLE applicationsYou don’t have to deploy assemblies (perfect for tightly controlled environments)
  • 4.
    What Skills doI need?JavaScript (duh)CSSA development background helps!It IS codeUses development constructsIf you can’t write code, your ability to do magic will be limited to what you can copy/pasteCAML, CAML, CAML… Sorry…Ability to think outside the boxUse all the pieces together
  • 5.
    SharePoint & jQuery?Why?Resolves many SharePoint complaints without having to crack open Visual Studio“It looks like SharePoint” becomes “That’s SharePoint?”“SharePoint can’t do that out of the box” becomes “Sure, no problem”“That will take 3 weeks???” becomes “2 days? Awesome! I love you… here, please accept this bonus for being such a wonderful developer”
  • 6.
    jQuery makes yourSharePoint applications USABLE
  • 7.
    Common MythsIt’s notsecureBusted… It uses SharePoint security. All scripts run with privleges of current userIt doesn’t perform wellPlausible… It performs very well if you use it correctly in the right situationsI can’t elevate privileges if I need toConfirmed…
  • 8.
    What about…I can’tinteract with SharePoint Lists and LibrariesBusted… You can call Web Services with JavaScript (SPServices is a wonderful jQuery library that wraps SharePoints Web Service calls)It has no real use in my environment because of “x”Busted… Quickly prototype and tweak web parts before writing in Visual Studio. In fact, in some environments it is the only development option.
  • 9.
    Yeah But… It’snot deployableCreate central script document library for jQuery scriptsUpload new versions as neededTurn on versioning and store multiple versionsUse metadata to keep track of script informationStore on file systemDeploy with solution packageIncreased performance of loading from file system
  • 10.
    Okay… but… It’snot maintainableScript PlacementDon’t put your script source in the CEWPDon’t hardcode scripts in your MasterPagesAgain… deploy scripts to central script document library or to file system with solution packageIf stored in doc lib, turn on versioning to easily roll-back if needed.
  • 11.
    Moving Between Environmentsand Upgrading to 2010It just works… (woo hoo)Uses list names and not GUIDs so no issues moving from dev to prodFor the most part works identical in 2007 and 2010 (I’m sure there’s the occasional issue but I’ve never run into it).Might want to tweak in 2010 to take advantage of 2010 features (ribbin, modal pop-ups)
  • 12.
    Okay then…jQuery mustbe the SharePoint Silver BulletBusted…It does “expose” business logic to user if they dig aroundIt executes on the client side and can perform slow if manipulating larges amounts of dataBe extra careful for external facing applicationsYou can’t do EVERYTHING with jQuerylike…Timer JobsWorkflows (although it can eliminate the need for some)Event HandlersElevate PrivligesEasily interact with all business systems
  • 13.
    jQuery is anothertool for the SharePoint Developer’s toolkitUnderstand the limitationsUse it wisely
  • 14.
    Development BasicsjQuery methodsare executed using jQuery() or $()You don’t have to use $(document).ready() Use $() directly$(‘#elementID’) to interact with any elementDivs, tables, rows, cells, inputs, selects.. Etc.. Etc.. Etc… <table id=‘myTable’></table> $(‘#myTable’).append(‘<tr><td>add a row</td></tr>’);$(‘#elementID’).val() gets values of inputs<input type=‘text’ id=‘myTextBox’ >$(‘#myTextBox’).val();$(‘#elementID’).val([value]) sets the values of inputs$(‘#myTextBox’).val(‘Hello World’);
  • 15.
    Development Basics$(‘#elementID’).html() getsthe raw html within an element (great for divs, spans, tables, table cells, etc.)<div id=‘myDiv’>Hello World</div>$(‘#myDiv’).html() = ‘Hello World’$(‘#elementID’).html(“<html text>”) to set raw html$(‘#myDiv’).html(‘Hello World! Welcome to SPSSTL!’)$(‘#elementID’).hide(), $(‘#elementID’).show(), and $(‘#elementID’).toggle()$(‘#myDiv’).hide()$(‘#myDiv’).show()$(‘#myDiv’).toggle()
  • 16.
    Development Basics$(‘#element’).addClass(“className”);$(‘#element’).removeClass(“className”);$(‘#element’).attr(“attributeName”);$(‘#element’).attr(“attributeName”,”attributeValue”);SharePoint FormFields$(‘input[title=“Title”]’).val();$(‘select[title=“Title”]’).val();GET O’REILLY’S JQUERY POCKET REFERENCEhttp://oreilly.com/catalog/0636920016182
  • 17.
    More Dev BasicsEachloops$('table[class="myTable"]').each( function () { $('#' + this.id).hide(); });CDATA is a life saverWrap values in CDATA tags:"<![CDATA[" + data + "]]>"
  • 18.
    SPServicesjQuery library toexecute SharePoint’s Web ServicesGet List ItemsAdd/Update List ItemsCreate lists, content types, site columns, etc.Create document library foldersCreate cascading drop down lists
  • 19.
    Get user groupsand permissions
  • 20.
    Potentially call ANYSharePoint Web ServiceSPServicesWorks identical in SharePoint 2007 and SharePoint 2010Runs with permissions of current userCommunicate across sites
  • 21.
  • 22.
  • 23.
    SPServices - FYIUseinternal field names Returned values have “ows_” in front of field names$(this).attr("ows_Title");Lookup & People fieldsValue returned as “ID;#Value” (1;#Field Value)Check for new item ID on item add to make sure item added correctlyvarnewID = $(xData.responseXML).find("[nodeName=z:row]").attr("ows_ID");
  • 24.
    Development TipsDevelop forperformanceLimit rows returned using CAMLAvoid screen scraping (Use SPServices)Don’t call web services until the data is needed.Use those ID’s to your advantage<td id=‘ListName-4’>Attributes are awesome<input type="text" title="list title 4" id="4">$('#4').attr("title");$(‘input[title=“list title 4”]’).val();
  • 25.
    Writing jQueryPick whatevereditor makes you happy…SharePoint DesignerNo need to upload scriptsVisual StudioI don’t use it, so can’t speak to itAptana (actual JavaScript IDE)Gives you some intellisenseNotePad++Good bracket matching which tends to bite you in the butt
  • 26.
    Debugging… ugh.. Itain’t prettyAlerts.. Alerts.. Alerts.. Alerts…Developer ToolsSet breakpointsEvaluate expressions and variables inline (like REAL debugging!)Firebug for Firefox Considered to be best of the free tools out thereIE Developer Tools Comes installed on IE 8+
  • 27.
    Debugging… Common ErrorsUsuallyit means your library didn’t get loadedObject ExpectedObject doesn’t support methodMake sure you don’t load scripts more than onceNull Object referenceCheck your bracesMake sure you end lines with “;”Check for missing quotesWhen all else fails, delete your script and rebuild it slowly in chunks, testing as you go.
  • 28.
  • 29.
    How to getit…jQueryhttp://jquery.com/SPServiceshttp://spservices.codeplex.comSuper Awesome 3rd party libraries that integrate wellModal dialogs - http://www.ericmmartin.com/projects/simplemodal/Calendar - http://arshaw.com/fullcalendar/Charts - http://g.raphaeljs.com/
  • 30.
    More jQuery ResourcesjQueryPocket Referencehttp://oreilly.com/catalog/0636920016182CSS Pocket Referncehttp://oreilly.com/catalog/9780596515058/
  • 31.
    HousekeepingPlease remember tosubmit your session evaluation forms after each session you attend to increase your chances at the raffleFollow SharePoint Saturday St. Louis on Twitter @spsstl and hashtag #spsstl
  • 32.
    Thanks to OurSponsors!Gold SponsorsSilver SponsorsRaffle Sponsors
  • 33.