360 likes | 467 Vues
Discover how to effectively use JavaScript in SharePoint to enhance functionality and user experience. This guide explores the essentials of integrating JavaScript, including adding scripts to the SharePoint master page, utilizing JSOM for data manipulation, and implementing Client-Side Rendering for customizable displays. Learn about OData, REST APIs, and common patterns in JavaScript use, with practical examples and tips for successful implementation. Unlock the potential of SharePoint with JavaScript for a more dynamic web experience.
E N D
JS in SP – Why, What & How Nick Hadlee and Gary Payne
In the Masterpage <SharePoint:ScriptLinklanguage="javascript"name="menu.js"OnDemand="false"runat="server"Localizable="false"/> true" <scripttype="text/javascript"> // <![CDATA[ document.write(‘<script type="text/javascript"src="/_layouts/menu.js"></’ + ‘script>’); // ]]> </script> <scripttype="text/javascript">RegisterSod("menu.js", “\u002f_layouts\u002f15\u002fmenu.js?rev=pcr83s11QGFA2kLt5rDQ1g\u00253D\u00253D");</script>
Control Delegate <SharePoint:DelegateControlrunat="server" ControlId="AdditionalPageHead"AllowMultipleControls="true" /> <?xml version="1.0" encoding="utf-8"?> <Elementsxmlns="http://schemas.microsoft.com/sharepoint/"> <Control Id="AdditionalPageHead" Sequence="50" ControlSrc="~/_CONTROLTEMPLATES/SPC/jQueryControl.ascx" /> </Control> </Elements>
A Custom Action <?xml version="1.0" encoding="utf-8"?> <Elementsxmlns="http://schemas.microsoft.com/sharepoint/"> <CustomAction ScriptSrc="~SiteCollection/SiteAssets/jquery-1.8.3.min.js" Location="ScriptLink" Sequence="10"> </CustomAction> </Elements>
OData demo – Get some data http://[site]/_api/web/lists/ GetByTitle('Towns')/items?$select=Title
TIP!!! Getting internal name of list fields http://[site]/_api/web/lists/ getbytitle('Events')/Fields ?$select=Title,InternalName,ID
Typical JSOM Pattern varclientContext = SP.ClientContext.get_current(); varweb = clientContext.get_web(); varlist = web.get_lists().getByTitle("Rooms"); varfloorChoiceField = clientContext.castTo( list.get_fields().getByInternalNameOrTitle("Floor"), SP.FieldChoice ); clientContext.load(floorChoiceField); clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed) );
Typical JSOM Pattern varclientContext = SP.ClientContext.get_current(); varweb = clientContext.get_web(); varlist = web.get_lists().getByTitle("Rooms"); varfloorChoiceField = clientContext.castTo( list.get_fields().getByInternalNameOrTitle("Floor"), SP.FieldChoice ); clientContext.load(floorChoiceField); clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed) );
Typical JSOM Pattern varclientContext = SP.ClientContext.get_current(); varweb = clientContext.get_web(); varlist = web.get_lists().getByTitle("Rooms"); varfloorChoiceField = clientContext.castTo( list.get_fields().getByInternalNameOrTitle("Floor"), SP.FieldChoice ); clientContext.load(floorChoiceField); clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed) );
Typical JSOM Pattern varclientContext = SP.ClientContext.get_current(); varweb = clientContext.get_web(); varlist = web.get_lists().getByTitle("Rooms"); varfloorChoiceField = clientContext.castTo( list.get_fields().getByInternalNameOrTitle("Floor"), SP.FieldChoice ); clientContext.load(floorChoiceField); clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed) );
TIP!!! You can only work with files up to 1.5 MB by using the JavaScript object model. To upload larger files, use REST.
Client Side Rendering • Method of changing how areas of SharePoint’s UI via JS micro templates “Display Templates” • JSLink or JS Link • Search
JSLink • Fields • View(s) templates "View" • Forms templates "DisplayForm" "EditForm" "NewForm" • Views • The entire view via an “Item” template or “Body” template • A field in a view via a “View” template • Can also limit to a specific view and/or list template • Xslt List View Web Parts • Forms
JSLink The JavaScript template pattern: (function(){ // Initialize the variable that store the objects. varoverrideCtx = {}; overrideCtx.Templates = {}; // Do the template bits here // <-- Overidden rendering is added here --> // Register the template overrides. SPClientTemplates.TemplateManager .RegisterTemplateOverrides(overrideCtx); })();
JSLink (function(){ // Initialize the variable that store the objects. varoverrideCtx = {}; overrideCtx.Templates = {}; // A field template. Can be static text or a function overrideCtx.Fields = { LinkTitle = { "View" : "No problem here!" } } // Register the template overrides. SPClientTemplates.TemplateManager .RegisterTemplateOverrides(overrideCtx); })();
JSLink (function(){ // Initialize the variable that store the objects. varoverrideCtx = {}; overrideCtx.Templates = {}; // A field template. Can be static text or a function overrideCtx.Fields = { LinkTitle = { "View" : titleFieldFunction} } // Register the template overrides. SPClientTemplates.TemplateManager .RegisterTemplateOverrides(overrideCtx); })();
JSLink function titleFieldFunction(ctx){ // Craft the html to be returned by the function varreturnHtml = ""; returnHtml += "<b>"; returnHtml += ctx.CurrentItem.Title; returnHtml += "</b>"; returnreturnHtml; }
TIP!!! _spPageContextInfo is a JS object on every SP page _spPageContextInfo.webServerRelativeUrl • http://blog.tedpattison.net/Lists/Posts/Post.aspx?ID=9 • http://johnliu.net/blog/2012/2/3/sharepoint-javascript-current-page-context-info.html
Contact Details Gary Payne http://www.smallsteps.co.nz Gary.payne@smallsteps.co.nz Nick Hadlee @nickhadlee http://nickhadlee.wordpress.com nicholas.hadlee@intergen.co.nz