SlideShare uma empresa Scribd logo
1 de 34
A jQuery Primer for SharePoint
Marc D Anderson
Who Is Marc?
• Co-Founder and President of Sympraxis Consulting LLC,
located in the Boston suburb of Newton, MA, USA.
Sympraxis focuses on enabling collaboration throughout the
enterprise using the SharePoint application platform.
• More than 30 years of experience in technology professional services and software
development. Over a wide-ranging career in consulting as well as line manager
positions, Marc has proven himself as a problem solver and leader who can solve
difficult technology problems for organizations across a wide variety of industries
and organization sizes.
• Three-time awardee of the Microsoft MVP award for SharePoint Server (2011,
2012, 2013).
What is jQuery?
is
Getting Started
• Add references to the jQuery library
• References can be in:
– Master page
– Page layout
– Individual aspx pages
• jQuery and other .js files can be stored in:
– Document Library
– _layouts folder
– Retrieved from a CDN
• Use “protocol-less” src and href
Script from CDNs
Referencing jQuery, jQueryUI, and SPServices from CDNs – Revisited
http://sympmarc.com/2013/02/07/referencing-jquery-jqueryui-and-spservices-from-cdns-revisited/
Note the protocol-less
references
HTML Elements
Powered by <a href="http://office365.com">Office365</a>.
Opening tag Closing tag
Attribute Value
<input id="my-cbox" class="foo bar" type="checkbox" checked />
Element Self-closing tag
Id Attribute PropertyValueClass(es)
What is the Document Object Model
(DOM)?
• The DOM starts as the page’s markup (HTML) as
delivered to the browser by the server: View Source
• Styled by the CSS which gives the page its look and feel
• The DOM is acted upon by any script in the page
• View Source is *not* the live DOM
What Can We Do With the DOM?
• Add or remove CSS classes
• Create new or remove existing HTML elements
• Change HTML element attributes
• Bind to events
• And so much more…
The DOM is HTML, which is XML, which is data!
The Basic Idea of jQuery
$(".article").hide();
Select something
Do something!
Remember this from CSS?
jQuery’s Document Ready
$(document).ready(function() {
// do something
});
$(function() {
// do something
});
jQuery(function($) {
// do something
});
jQuery Documentation:
Your Friend
• The jQuery documentation is used to be
arranged to help you
• What you need to know is was arranged
sequentially from top to bottom
jQuery Selectors
• Selectors are the most important
jQuery concept
• Selecting the right DOM object(s)
is half the battle
• Selectors return a jQuery object
containing a collection of DOM
objects: 1 to n matching elements
Selectors for SharePoint
$("div[id$='QuickLaunchNavigationManager']
li:first span.menu-item-text")
Selectors for SharePoint
$("td.ms-list-addnew a:eq(1)")
Useful jQuery Selector Tricks
$("[id='foo']"); // Equal to
$("[id!='foo']"); // Not equal to
$("[id^='foo']"); // Starts with
$("[id$='foo']"); // Ends with
$("[id*='foo']"); // Contains
$("[id~='foo']"); // Contains word
$("[id|='foo']"); // Contains prefix
$("[id]"); // Has attribute
$("[id][class][style]"); // Has all
•.NET Applications like
SharePoint generate some long
and ugly markup and IDs
id="ctl00$ctl41$g_26ee1140_76aa_4ec0_8
8c4_11e7e96480f4$ctl00$ctl02$ctl00$ctl
01$ctl00$ContentTypeChoice"
•These selector tricks really help
jQuery Attributes
• Once you’ve selected the right DOM element, you can
use jQuery to get or set its attributes
• As of jQuery 1.6:
– the .prop() method provides a way to explicitly get/set
property values (checked, selected, or disabled)
– .attr() gets/sets attribute values (class, style, etc.)
Example with SharePoint Attributes: Get
$("select[title='Region']").val();
$("select[title='Region'] option:selected").text();
Example with SharePoint Attributes: Set
$("select[title='Region']").val(5);
$("select[title='Region'] option:selected").text("boo");
Traversing
• Traversing lets you move around the
DOM based on your initial selection
• This is how you get at elements which
are difficult to select directly
• Ancestry matters in XML / HTML
Traversing Down:
Find an Element’s Specific Children
$("div[id$='QuickLaunchNavigationManager'] li:first")
.parent().find("li:eq(3) li:first .menu-item-text");
Traversal Example from SPServices
var possibleValues = $("select[ID$='SelectCandidate'][Title^='" +
opt.multiSelectColumn + " ']");
var selectedValues =
possibleValues.closest("span").find("select[ID$='SelectResult'][Title^='" +
opt.multiSelectColumn + " ']");
SelectCandidate SelectResult
<select
name="ctl00$m$g_e845e690_00da_428f_afbd_fbe804
787763$ctl00$ctl04$ctl07$ctl00$ctl00$ctl04$ctl
00$ctl00$SelectResult" title="City selected
values"
id="ctl00_m_g_e845e690_00da_428f_afbd_fbe80478
7763_ctl00_ctl04_ctl07_ctl00_ctl00_ctl04_ctl00
_ctl00_SelectResult" style="width: 162px;"
onkeydown="GipHandleHScroll(event)"
ondblclick="GipRemoveSelectedItems(ctl00_m_g_e
845e690_00da_428f_afbd_fbe804787763_ctl00_ctl0
4_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_MultiLoo
kupPicker_m); return false"
onchange="GipSelectResultItems(ctl00_m_g_e845e
690_00da_428f_afbd_fbe804787763_ctl00_ctl04_ct
l07_ctl00_ctl00_ctl04_ctl00_ctl00_MultiLookupP
icker_m);" size="20" multiple="">
<select
name="ctl00$m$g_e845e690_00da_428f_afbd_fbe8
04787763$ctl00$ctl04$ctl07$ctl00$ctl00$ctl04
$ctl00$ctl00$SelectCandidate" title="City
possible values"
id="ctl00_m_g_e845e690_00da_428f_afbd_fbe804
787763_ctl00_ctl04_ctl07_ctl00_ctl00_ctl04_c
tl00_ctl00_SelectCandidate" style="width:
162px;" onkeydown="GipHandleHScroll(event)"
ondblclick="GipAddSelectedItems(ctl00_m_g_e8
45e690_00da_428f_afbd_fbe804787763_ctl00_ctl
04_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_Multi
LookupPicker_m); return false"
onchange="GipSelectCandidateItems(ctl00_m_g_
e845e690_00da_428f_afbd_fbe804787763_ctl00_c
tl04_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_Mul
tiLookupPicker_m);" size="350" multiple="">
Manipulation
• Once you’ve gotten the right
element(s), you can:
– Manipulate their attributes
– Set properties
– Change their contents (e.g.,
innerHTML)
Events
• jQuery’s events enable you to work
with all of the standard JavaScript
events, plus create your own
• Used to create behaviors that take
effect when the user interacts with
the page in the browser, and to
further manipulate those behaviors
jQuery Events
$(".article").click(function(){
// do something
});
$(".article").mouseover(function(){
// do something
});
jQuery Events
$("h3.ms-WPTitle").click(function() {
alert("Go directly to the list.");
});
$("h3.ms-WPTitle").hover(function() {
$(this).css("background-color", "fuchsia");
$(this).data("title", $(this).html());
$(this).html("Click to visit");
},function() {
$(this).css("background-color", "white");
$(this).html($(this).data("title"));
});
Effects
• jQuery gives you a set of effects you can
use to change the elements in the page
• Effects can be:
– Visual: Change how the user sees existing
elements with animations
– Manipulative: Change where and how
elements are shown by moving them around
in the DOM
jQuery Effects Examples
$(".article").hide();
$(".article").slideUp();
$(".article").fadeOut("slow");
$(".article").animate({
"font-size": "24px",
"background-color": "red"
}, 5000);
Putting It Together:
Toggling Web Part Visibility
var wpTitles = $("h3.ms-WPTitle");
// Remove the links on the Web Part Titles
wpTitles.find("nobr").unwrap("<a></a>");
// Show the pointer on mouseover
wpTitles.css("cursor", "pointer");
// Add click behavior that toggles the visibility
wpTitles.click(function() {
$(this).closest("table").closest("tr").next().slideToggle();
});
Putting It Together:
Arranging Checkboxes
// Collect all of the choices
$(thisFormField).find("tr").each(function() {
columnOptions.push($(this).html());
});
out = "<TR>";
// Add all of the options to the out string
for(i=0; i < columnOptions.length; i++) {
out += columnOptions[i];
// If we've already got perRow columnOptions in the row,
// close off the row
if((i+1) % opt.perRow === 0) {
out += "</TR><TR>";
}
}
out += "</TR>";
// Remove the existing rows...
$(thisFormField).find("tr").remove();
// ...and append the out string
$(thisFormField).find("table").append(out);
jQueryUI Takes Effects Further
$(".article").tabs();
$("input").autocomplete();
$("#infoBox").dialog();
$("table.sortable").sortable();
…and many more
jQuery Plugins Abound!
• If you want to do something sophisticated,
look for an existing plugin
• Due diligence – some of the plugins aren’t
written very well
• Beware of “plugin sprawl”
More Useful Tools
• JSLint
– http://jslint.com/
– Checks your script against accepted standards
– “Warning: JSLint will hurt your feelings.”
• JSHint
– http://jshint.com/
– Like JSLint, but a little more forgiving
– More jQuery aware
• JavaScript Compressorator
– http://compressorrater.thruhere.net/
– Minifies script files using multiple methods
QUESTIONS?
Contact Information
eMail marc.anderson@sympraxisconsulting.com
Blog http://sympmarc.com
SPServices http://spservices.codeplex.com
SPXSLT http://spxslt.codeplex.com
Books http://sympmarc.com/books
The Middle Tier Manifesto http://bit.ly/middletier

Mais conteúdo relacionado

Mais procurados

SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014
Mark Rackley
 
SPTechCon Dev Days - Third Party jQuery Libraries
SPTechCon Dev Days - Third Party jQuery LibrariesSPTechCon Dev Days - Third Party jQuery Libraries
SPTechCon Dev Days - Third Party jQuery Libraries
Mark Rackley
 
Intro to SharePoint Web Services
Intro to SharePoint Web ServicesIntro to SharePoint Web Services
Intro to SharePoint Web Services
Mark Rackley
 

Mais procurados (20)

Transform SharePoint default list forms with HTML, CSS and JavaScript
Transform SharePoint default list forms with HTML, CSS and JavaScriptTransform SharePoint default list forms with HTML, CSS and JavaScript
Transform SharePoint default list forms with HTML, CSS and JavaScript
 
SPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQuerySPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQuery
 
SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014
 
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
2/15/2012 - Wrapping Your Head Around the SharePoint Beast2/15/2012 - Wrapping Your Head Around the SharePoint Beast
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
 
SPSNH 2014 - The SharePoint & jQueryGuide
SPSNH 2014 - The SharePoint & jQueryGuideSPSNH 2014 - The SharePoint & jQueryGuide
SPSNH 2014 - The SharePoint & jQueryGuide
 
TulsaTechFest - Maximize SharePoint UX with free jQuery libraries
TulsaTechFest - Maximize SharePoint UX with free jQuery librariesTulsaTechFest - Maximize SharePoint UX with free jQuery libraries
TulsaTechFest - Maximize SharePoint UX with free jQuery libraries
 
SPSDenver - SharePoint & jQuery - What I wish I would have known
SPSDenver - SharePoint & jQuery - What I wish I would have knownSPSDenver - SharePoint & jQuery - What I wish I would have known
SPSDenver - SharePoint & jQuery - What I wish I would have known
 
SPTechCon Boston 2015 - Utilizing jQuery in SharePoint
SPTechCon Boston 2015 - Utilizing jQuery in SharePointSPTechCon Boston 2015 - Utilizing jQuery in SharePoint
SPTechCon Boston 2015 - Utilizing jQuery in SharePoint
 
SPTechCon Dev Days - Third Party jQuery Libraries
SPTechCon Dev Days - Third Party jQuery LibrariesSPTechCon Dev Days - Third Party jQuery Libraries
SPTechCon Dev Days - Third Party jQuery Libraries
 
SharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQuerySharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQuery
 
Transform SharePoint List Forms with HTML and CSS
Transform SharePoint List Forms with HTML and CSSTransform SharePoint List Forms with HTML and CSS
Transform SharePoint List Forms with HTML and CSS
 
SharePoint and the User Interface with JavaScript
SharePoint and the User Interface with JavaScriptSharePoint and the User Interface with JavaScript
SharePoint and the User Interface with JavaScript
 
SPTechCon Boston 2015 - Overcoming SharePoint Limitations
SPTechCon Boston 2015 - Overcoming SharePoint LimitationsSPTechCon Boston 2015 - Overcoming SharePoint Limitations
SPTechCon Boston 2015 - Overcoming SharePoint Limitations
 
Introduction to StratusForms #SayNoToInfoPath
Introduction to StratusForms #SayNoToInfoPathIntroduction to StratusForms #SayNoToInfoPath
Introduction to StratusForms #SayNoToInfoPath
 
Intro to SharePoint Web Services
Intro to SharePoint Web ServicesIntro to SharePoint Web Services
Intro to SharePoint Web Services
 
(Updated) SharePoint & jQuery Guide
(Updated) SharePoint & jQuery Guide(Updated) SharePoint & jQuery Guide
(Updated) SharePoint & jQuery Guide
 
Using jQuery to Maximize Form Usability
Using jQuery to Maximize Form UsabilityUsing jQuery to Maximize Form Usability
Using jQuery to Maximize Form Usability
 
SPTechCon 2014 How to develop and debug client side code in SharePoint
SPTechCon 2014 How to develop and debug client side code in SharePointSPTechCon 2014 How to develop and debug client side code in SharePoint
SPTechCon 2014 How to develop and debug client side code in SharePoint
 
A Power User's Intro to jQuery Awesomeness in SharePoint
A Power User's Intro to jQuery Awesomeness in SharePointA Power User's Intro to jQuery Awesomeness in SharePoint
A Power User's Intro to jQuery Awesomeness in SharePoint
 
Content query web part – get it all in one place and style it!
Content query web part – get it all in one place and style it!Content query web part – get it all in one place and style it!
Content query web part – get it all in one place and style it!
 

Destaque

Destaque (8)

Using Web Services and the Data View Web Part to combine lists from multiple ...
Using Web Services and the Data View Web Part to combine lists from multiple ...Using Web Services and the Data View Web Part to combine lists from multiple ...
Using Web Services and the Data View Web Part to combine lists from multiple ...
 
SharePoint Tech Fest Houston 2015 - Creating a Great User Experience in Share...
SharePoint Tech Fest Houston 2015 - Creating a Great User Experience in Share...SharePoint Tech Fest Houston 2015 - Creating a Great User Experience in Share...
SharePoint Tech Fest Houston 2015 - Creating a Great User Experience in Share...
 
SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery Essentials
 
Oslo SP User Group - Content Types - Love Them or Lose It
Oslo SP User Group - Content Types - Love Them or Lose ItOslo SP User Group - Content Types - Love Them or Lose It
Oslo SP User Group - Content Types - Love Them or Lose It
 
SharePoint Evolutions 2015 - Moving from SOAP to REST
SharePoint Evolutions 2015 - Moving from SOAP to RESTSharePoint Evolutions 2015 - Moving from SOAP to REST
SharePoint Evolutions 2015 - Moving from SOAP to REST
 
Unity Connect Haarlem 2016 - The Lay of the Land of Client-Side Development c...
Unity Connect Haarlem 2016 - The Lay of the Land of Client-Side Development c...Unity Connect Haarlem 2016 - The Lay of the Land of Client-Side Development c...
Unity Connect Haarlem 2016 - The Lay of the Land of Client-Side Development c...
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 

Semelhante a SEF2013 - A jQuery Primer for SharePoint

SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
Mark Rackley
 
Django introduction @ UGent
Django introduction @ UGentDjango introduction @ UGent
Django introduction @ UGent
kevinvw
 
J query presentation
J query presentationJ query presentation
J query presentation
akanksha17
 
J query presentation
J query presentationJ query presentation
J query presentation
sawarkar17
 

Semelhante a SEF2013 - A jQuery Primer for SharePoint (20)

SPSTC - SharePoint & jQuery Essentials
SPSTC - SharePoint & jQuery EssentialsSPSTC - SharePoint & jQuery Essentials
SPSTC - SharePoint & jQuery Essentials
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationLotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
Django introduction @ UGent
Django introduction @ UGentDjango introduction @ UGent
Django introduction @ UGent
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 
The SharePoint and jQuery Guide by Mark Rackley - SPTechCon
The SharePoint and jQuery Guide by Mark Rackley - SPTechConThe SharePoint and jQuery Guide by Mark Rackley - SPTechCon
The SharePoint and jQuery Guide by Mark Rackley - SPTechCon
 
Jquery library
Jquery libraryJquery library
Jquery library
 
J query presentation
J query presentationJ query presentation
J query presentation
 
J query presentation
J query presentationJ query presentation
J query presentation
 
J query module1
J query module1J query module1
J query module1
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
How to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQueryHow to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQuery
 
jQuery besic
jQuery besicjQuery besic
jQuery besic
 
jQuery Presentasion
jQuery PresentasionjQuery Presentasion
jQuery Presentasion
 

Mais de Marc D Anderson

Mais de Marc D Anderson (20)

SPC2019 - Managing Content Types in the Modern World
SPC2019 - Managing Content Types in the Modern WorldSPC2019 - Managing Content Types in the Modern World
SPC2019 - Managing Content Types in the Modern World
 
ECS2019 - Managing Content Types in the Modern World
ECS2019 - Managing Content Types in the Modern WorldECS2019 - Managing Content Types in the Modern World
ECS2019 - Managing Content Types in the Modern World
 
Rencontre Groupe d'usagers SharePoint Montreal - The Next Great Migration - C...
Rencontre Groupe d'usagers SharePoint Montreal - The Next Great Migration - C...Rencontre Groupe d'usagers SharePoint Montreal - The Next Great Migration - C...
Rencontre Groupe d'usagers SharePoint Montreal - The Next Great Migration - C...
 
RISPUG - Top Form - Using PowerApps to Replace List Forms
RISPUG - Top Form - Using PowerApps to Replace List FormsRISPUG - Top Form - Using PowerApps to Replace List Forms
RISPUG - Top Form - Using PowerApps to Replace List Forms
 
SPCNA 2018 - Top Form - Using PowerApps to Replace List Forms
SPCNA 2018 - Top Form - Using PowerApps to Replace List FormsSPCNA 2018 - Top Form - Using PowerApps to Replace List Forms
SPCNA 2018 - Top Form - Using PowerApps to Replace List Forms
 
SPCNA 2018 - The Next Great Migration - Classic to Modern
SPCNA 2018 - The Next Great Migration - Classic to ModernSPCNA 2018 - The Next Great Migration - Classic to Modern
SPCNA 2018 - The Next Great Migration - Classic to Modern
 
SPS New York City 2017 - The Lay of the Land of Client-Side Development circa...
SPS New York City 2017 - The Lay of the Land of Client-Side Development circa...SPS New York City 2017 - The Lay of the Land of Client-Side Development circa...
SPS New York City 2017 - The Lay of the Land of Client-Side Development circa...
 
ECS Zagreb 2017 - Content Types - Love Them or Lose It
ECS Zagreb 2017 - Content Types - Love Them or Lose ItECS Zagreb 2017 - Content Types - Love Them or Lose It
ECS Zagreb 2017 - Content Types - Love Them or Lose It
 
SPS Monaco 2017 - The Lay of the Land of Client-Side Development circa 2017
SPS Monaco 2017 - The Lay of the Land of Client-Side Development circa 2017SPS Monaco 2017 - The Lay of the Land of Client-Side Development circa 2017
SPS Monaco 2017 - The Lay of the Land of Client-Side Development circa 2017
 
Lions Tigers Teams - SPTechCon Austin 2017
Lions Tigers Teams - SPTechCon Austin 2017Lions Tigers Teams - SPTechCon Austin 2017
Lions Tigers Teams - SPTechCon Austin 2017
 
SPTechCon Boston 2016 - Creating a Great User Experience in SharePoint
SPTechCon Boston 2016 - Creating a Great User Experience in SharePointSPTechCon Boston 2016 - Creating a Great User Experience in SharePoint
SPTechCon Boston 2016 - Creating a Great User Experience in SharePoint
 
SPTechCon Boston 2016 - Content Types - Love Them or Lose It
SPTechCon Boston 2016 - Content Types - Love Them or Lose ItSPTechCon Boston 2016 - Content Types - Love Them or Lose It
SPTechCon Boston 2016 - Content Types - Love Them or Lose It
 
SPC Adriatics 2016 - Creating a Great User Experience in SharePoint
SPC Adriatics 2016 - Creating a Great User Experience in SharePointSPC Adriatics 2016 - Creating a Great User Experience in SharePoint
SPC Adriatics 2016 - Creating a Great User Experience in SharePoint
 
SPC Adriatics 2016 - Alternative Approaches to Solution Development in Office...
SPC Adriatics 2016 - Alternative Approaches to Solution Development in Office...SPC Adriatics 2016 - Alternative Approaches to Solution Development in Office...
SPC Adriatics 2016 - Alternative Approaches to Solution Development in Office...
 
SharePointFest Konferenz 2016 - Creating a Great User Experience in SharePoint
SharePointFest Konferenz 2016 - Creating a Great User Experience in SharePointSharePointFest Konferenz 2016 - Creating a Great User Experience in SharePoint
SharePointFest Konferenz 2016 - Creating a Great User Experience in SharePoint
 
SharePointFest Konferenz 2016 - Alternative Approaches to Solution Developmen...
SharePointFest Konferenz 2016 - Alternative Approaches to Solution Developmen...SharePointFest Konferenz 2016 - Alternative Approaches to Solution Developmen...
SharePointFest Konferenz 2016 - Alternative Approaches to Solution Developmen...
 
SPTechCon Austin 2016 - Content Types-Love Them or Lose It
SPTechCon Austin 2016 - Content Types-Love Them or Lose ItSPTechCon Austin 2016 - Content Types-Love Them or Lose It
SPTechCon Austin 2016 - Content Types-Love Them or Lose It
 
SPTechCon Austin 2016 - Creating a Great User Experience in SharePoint
SPTechCon Austin 2016 - Creating a Great User Experience in SharePointSPTechCon Austin 2016 - Creating a Great User Experience in SharePoint
SPTechCon Austin 2016 - Creating a Great User Experience in SharePoint
 
SharePoint Tech Fest Houston 2015 - Moving from SOAP to REST
SharePoint Tech Fest Houston 2015 - Moving from SOAP to RESTSharePoint Tech Fest Houston 2015 - Moving from SOAP to REST
SharePoint Tech Fest Houston 2015 - Moving from SOAP to REST
 
SharePoint Saturday CT 2015 - Content Types: Love Them or Lose It
SharePoint Saturday CT 2015 - Content Types: Love Them or Lose ItSharePoint Saturday CT 2015 - Content Types: Love Them or Lose It
SharePoint Saturday CT 2015 - Content Types: Love Them or Lose It
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

SEF2013 - A jQuery Primer for SharePoint

  • 1. A jQuery Primer for SharePoint Marc D Anderson
  • 2. Who Is Marc? • Co-Founder and President of Sympraxis Consulting LLC, located in the Boston suburb of Newton, MA, USA. Sympraxis focuses on enabling collaboration throughout the enterprise using the SharePoint application platform. • More than 30 years of experience in technology professional services and software development. Over a wide-ranging career in consulting as well as line manager positions, Marc has proven himself as a problem solver and leader who can solve difficult technology problems for organizations across a wide variety of industries and organization sizes. • Three-time awardee of the Microsoft MVP award for SharePoint Server (2011, 2012, 2013).
  • 4. Getting Started • Add references to the jQuery library • References can be in: – Master page – Page layout – Individual aspx pages • jQuery and other .js files can be stored in: – Document Library – _layouts folder – Retrieved from a CDN • Use “protocol-less” src and href
  • 5. Script from CDNs Referencing jQuery, jQueryUI, and SPServices from CDNs – Revisited http://sympmarc.com/2013/02/07/referencing-jquery-jqueryui-and-spservices-from-cdns-revisited/ Note the protocol-less references
  • 6. HTML Elements Powered by <a href="http://office365.com">Office365</a>. Opening tag Closing tag Attribute Value <input id="my-cbox" class="foo bar" type="checkbox" checked /> Element Self-closing tag Id Attribute PropertyValueClass(es)
  • 7. What is the Document Object Model (DOM)? • The DOM starts as the page’s markup (HTML) as delivered to the browser by the server: View Source • Styled by the CSS which gives the page its look and feel • The DOM is acted upon by any script in the page • View Source is *not* the live DOM
  • 8. What Can We Do With the DOM? • Add or remove CSS classes • Create new or remove existing HTML elements • Change HTML element attributes • Bind to events • And so much more… The DOM is HTML, which is XML, which is data!
  • 9. The Basic Idea of jQuery $(".article").hide(); Select something Do something! Remember this from CSS?
  • 10. jQuery’s Document Ready $(document).ready(function() { // do something }); $(function() { // do something }); jQuery(function($) { // do something });
  • 11. jQuery Documentation: Your Friend • The jQuery documentation is used to be arranged to help you • What you need to know is was arranged sequentially from top to bottom
  • 12. jQuery Selectors • Selectors are the most important jQuery concept • Selecting the right DOM object(s) is half the battle • Selectors return a jQuery object containing a collection of DOM objects: 1 to n matching elements
  • 15. Useful jQuery Selector Tricks $("[id='foo']"); // Equal to $("[id!='foo']"); // Not equal to $("[id^='foo']"); // Starts with $("[id$='foo']"); // Ends with $("[id*='foo']"); // Contains $("[id~='foo']"); // Contains word $("[id|='foo']"); // Contains prefix $("[id]"); // Has attribute $("[id][class][style]"); // Has all •.NET Applications like SharePoint generate some long and ugly markup and IDs id="ctl00$ctl41$g_26ee1140_76aa_4ec0_8 8c4_11e7e96480f4$ctl00$ctl02$ctl00$ctl 01$ctl00$ContentTypeChoice" •These selector tricks really help
  • 16. jQuery Attributes • Once you’ve selected the right DOM element, you can use jQuery to get or set its attributes • As of jQuery 1.6: – the .prop() method provides a way to explicitly get/set property values (checked, selected, or disabled) – .attr() gets/sets attribute values (class, style, etc.)
  • 17. Example with SharePoint Attributes: Get $("select[title='Region']").val(); $("select[title='Region'] option:selected").text();
  • 18. Example with SharePoint Attributes: Set $("select[title='Region']").val(5); $("select[title='Region'] option:selected").text("boo");
  • 19. Traversing • Traversing lets you move around the DOM based on your initial selection • This is how you get at elements which are difficult to select directly • Ancestry matters in XML / HTML
  • 20. Traversing Down: Find an Element’s Specific Children $("div[id$='QuickLaunchNavigationManager'] li:first") .parent().find("li:eq(3) li:first .menu-item-text");
  • 21. Traversal Example from SPServices var possibleValues = $("select[ID$='SelectCandidate'][Title^='" + opt.multiSelectColumn + " ']"); var selectedValues = possibleValues.closest("span").find("select[ID$='SelectResult'][Title^='" + opt.multiSelectColumn + " ']"); SelectCandidate SelectResult <select name="ctl00$m$g_e845e690_00da_428f_afbd_fbe804 787763$ctl00$ctl04$ctl07$ctl00$ctl00$ctl04$ctl 00$ctl00$SelectResult" title="City selected values" id="ctl00_m_g_e845e690_00da_428f_afbd_fbe80478 7763_ctl00_ctl04_ctl07_ctl00_ctl00_ctl04_ctl00 _ctl00_SelectResult" style="width: 162px;" onkeydown="GipHandleHScroll(event)" ondblclick="GipRemoveSelectedItems(ctl00_m_g_e 845e690_00da_428f_afbd_fbe804787763_ctl00_ctl0 4_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_MultiLoo kupPicker_m); return false" onchange="GipSelectResultItems(ctl00_m_g_e845e 690_00da_428f_afbd_fbe804787763_ctl00_ctl04_ct l07_ctl00_ctl00_ctl04_ctl00_ctl00_MultiLookupP icker_m);" size="20" multiple=""> <select name="ctl00$m$g_e845e690_00da_428f_afbd_fbe8 04787763$ctl00$ctl04$ctl07$ctl00$ctl00$ctl04 $ctl00$ctl00$SelectCandidate" title="City possible values" id="ctl00_m_g_e845e690_00da_428f_afbd_fbe804 787763_ctl00_ctl04_ctl07_ctl00_ctl00_ctl04_c tl00_ctl00_SelectCandidate" style="width: 162px;" onkeydown="GipHandleHScroll(event)" ondblclick="GipAddSelectedItems(ctl00_m_g_e8 45e690_00da_428f_afbd_fbe804787763_ctl00_ctl 04_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_Multi LookupPicker_m); return false" onchange="GipSelectCandidateItems(ctl00_m_g_ e845e690_00da_428f_afbd_fbe804787763_ctl00_c tl04_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_Mul tiLookupPicker_m);" size="350" multiple="">
  • 22. Manipulation • Once you’ve gotten the right element(s), you can: – Manipulate their attributes – Set properties – Change their contents (e.g., innerHTML)
  • 23. Events • jQuery’s events enable you to work with all of the standard JavaScript events, plus create your own • Used to create behaviors that take effect when the user interacts with the page in the browser, and to further manipulate those behaviors
  • 24. jQuery Events $(".article").click(function(){ // do something }); $(".article").mouseover(function(){ // do something });
  • 25. jQuery Events $("h3.ms-WPTitle").click(function() { alert("Go directly to the list."); }); $("h3.ms-WPTitle").hover(function() { $(this).css("background-color", "fuchsia"); $(this).data("title", $(this).html()); $(this).html("Click to visit"); },function() { $(this).css("background-color", "white"); $(this).html($(this).data("title")); });
  • 26. Effects • jQuery gives you a set of effects you can use to change the elements in the page • Effects can be: – Visual: Change how the user sees existing elements with animations – Manipulative: Change where and how elements are shown by moving them around in the DOM
  • 28. Putting It Together: Toggling Web Part Visibility var wpTitles = $("h3.ms-WPTitle"); // Remove the links on the Web Part Titles wpTitles.find("nobr").unwrap("<a></a>"); // Show the pointer on mouseover wpTitles.css("cursor", "pointer"); // Add click behavior that toggles the visibility wpTitles.click(function() { $(this).closest("table").closest("tr").next().slideToggle(); });
  • 29. Putting It Together: Arranging Checkboxes // Collect all of the choices $(thisFormField).find("tr").each(function() { columnOptions.push($(this).html()); }); out = "<TR>"; // Add all of the options to the out string for(i=0; i < columnOptions.length; i++) { out += columnOptions[i]; // If we've already got perRow columnOptions in the row, // close off the row if((i+1) % opt.perRow === 0) { out += "</TR><TR>"; } } out += "</TR>"; // Remove the existing rows... $(thisFormField).find("tr").remove(); // ...and append the out string $(thisFormField).find("table").append(out);
  • 30. jQueryUI Takes Effects Further $(".article").tabs(); $("input").autocomplete(); $("#infoBox").dialog(); $("table.sortable").sortable(); …and many more
  • 31. jQuery Plugins Abound! • If you want to do something sophisticated, look for an existing plugin • Due diligence – some of the plugins aren’t written very well • Beware of “plugin sprawl”
  • 32. More Useful Tools • JSLint – http://jslint.com/ – Checks your script against accepted standards – “Warning: JSLint will hurt your feelings.” • JSHint – http://jshint.com/ – Like JSLint, but a little more forgiving – More jQuery aware • JavaScript Compressorator – http://compressorrater.thruhere.net/ – Minifies script files using multiple methods
  • 34. Contact Information eMail marc.anderson@sympraxisconsulting.com Blog http://sympmarc.com SPServices http://spservices.codeplex.com SPXSLT http://spxslt.codeplex.com Books http://sympmarc.com/books The Middle Tier Manifesto http://bit.ly/middletier