{"id":1629,"date":"2016-02-26T07:31:28","date_gmt":"2016-02-25T20:31:28","guid":{"rendered":"https:\/\/itgroove.net\/mmman\/?p=1629"},"modified":"2023-02-24T21:39:36","modified_gmt":"2023-02-24T21:39:36","slug":"javascript-that-waits-for-the-sharepoint-apis-to-be-ready","status":"publish","type":"post","link":"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/","title":{"rendered":"JavaScript that Waits for the SharePoint API\u2019s to be Ready"},"content":{"rendered":"<p>I&#8217;ve run into this countless times in SharePoint where I need the browser DOM to be ready, but also at the same time, SharePoint isn&#8217;t yet ready to perform operations against its APIs.  The problem with using something like jQuery&#8217;s ready method is that will only get you a browser DOM that is ready, but the SharePoint pieces on the page haven&#8217;t necessarily yet rendered sufficiently, and the SP JavaScript API&#8217;s aren&#8217;t yet ready to respond to your method calls.\n<\/p>\n<p>Below is a simple piece of pure JavaScript code (that&#8217;s right, no need for jQuery if that&#8217;s a limitation for you) that you can use to ensure that both the DOM is ready and the SharePoint page\/API&#8217;s are ready.\n<\/p>\n<p>This should work for on premise SharePoint.  It may not work quite the same in Office 365.  I may write another blog post to address that.\n<\/p>\n<p><span style=\"font-family:Courier New\">var mmmanTimeoutValue = 50; \/\/In Milliseconds<br \/>\n<\/span><\/p>\n<\/p>\n<p><span style=\"font-family:Courier New\">function mmmanStartThisPage()<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;mmmanWaitForPageReady();<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;mmmanWaitForSPPageReady();<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">}<br \/>\n<\/span><\/p>\n<\/p>\n<p><span style=\"font-family:Courier New\">function mmmanWaitForPageReady()<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;if (document)<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mmmanPageIsReady();<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;else<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setTimeout(mmmanWaitForPageReady, mmmanTimeoutValue);<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">}<br \/>\n<\/span><\/p>\n<\/p>\n<p><span style=\"font-family:Courier New\">function mmmanWaitForSPPageReady()<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;if (SP &amp;&amp; SP != null &amp;&amp; typeof SP == &#8220;object&#8221; &amp;&amp; SP.ClientContext &amp;&amp; SP.ClientContext != null &amp;&amp; typeof SP.ClientContext == &#8220;function&#8221; &amp;&amp; SP.ClientContext.get_current &amp;&amp; SP.ClientContext.get_current != null &amp; typeof SP.ClientContext.get_current == &#8220;function&#8221;)<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mmmanSPPageIsReady();<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;else<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setTimeout(mmmanWaitForSPPageReady, mmmanTimeoutValue);<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">}<br \/>\n<\/span><\/p>\n<\/p>\n<p><span style=\"font-family:Courier New\">function mmmanPageIsReady()<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;alert(&#8220;DOM Ready!&#8221;)<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">}<br \/>\n<\/span><\/p>\n<\/p>\n<p><span style=\"font-family:Courier New\">function mmmanSPPageIsReady()<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">{<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;alert(&#8220;SP Ready!&#8221;);<br \/>\n<\/span><\/p>\n<p><span style=\"font-family:Courier New\">}<br \/>\n<\/span><\/p>\n<\/p>\n<p>Obviously where you see the alerts above, you simply replace that with your code that you need to run once SharePoint (or the DOM) is ready.\n<\/p>\n<p>I&#8217;ve used this piles of times, and it&#8217;s worked great for me.  Hopefully this is something you find value in as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve run into this countless times in SharePoint where I need the browser DOM to be ready, but also at the same time, SharePoint isn&#8217;t yet ready to perform operations against its APIs. The problem with using something like jQuery&#8217;s ready method is that will only get you a browser DOM that is ready, but &hellip; <a href=\"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/\"><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[20,4,7],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript that Waits for the SharePoint API\u2019s to be Ready - Archive<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript that Waits for the SharePoint API\u2019s to be Ready - Archive\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve run into this countless times in SharePoint where I need the browser DOM to be ready, but also at the same time, SharePoint isn&#8217;t yet ready to perform operations against its APIs. The problem with using something like jQuery&#8217;s ready method is that will only get you a browser DOM that is ready, but &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/\" \/>\n<meta property=\"og:site_name\" content=\"Archive\" \/>\n<meta property=\"article:published_time\" content=\"2016-02-25T20:31:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-24T21:39:36+00:00\" \/>\n<meta name=\"author\" content=\"Colin Phillips (Alumni)\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Colin Phillips (Alumni)\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/\",\"url\":\"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/\",\"name\":\"JavaScript that Waits for the SharePoint API\u2019s to be Ready - Archive\",\"isPartOf\":{\"@id\":\"https:\/\/regroove.ca\/archive\/#website\"},\"datePublished\":\"2016-02-25T20:31:28+00:00\",\"dateModified\":\"2023-02-24T21:39:36+00:00\",\"author\":{\"@id\":\"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b\"},\"breadcrumb\":{\"@id\":\"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog Archive\",\"item\":\"https:\/\/regroove.ca\/archive\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript that Waits for the SharePoint API\u2019s to be Ready\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/regroove.ca\/archive\/#website\",\"url\":\"https:\/\/regroove.ca\/archive\/\",\"name\":\"Archive\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/regroove.ca\/archive\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b\",\"name\":\"Colin Phillips (Alumni)\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/regroove.ca\/archive\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/14eeab0d377e9630e0983d9c08911979?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/14eeab0d377e9630e0983d9c08911979?s=96&d=mm&r=g\",\"caption\":\"Colin Phillips (Alumni)\"},\"url\":\"https:\/\/regroove.ca\/archive\/author\/cphillips\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript that Waits for the SharePoint API\u2019s to be Ready - Archive","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript that Waits for the SharePoint API\u2019s to be Ready - Archive","og_description":"I&#8217;ve run into this countless times in SharePoint where I need the browser DOM to be ready, but also at the same time, SharePoint isn&#8217;t yet ready to perform operations against its APIs. The problem with using something like jQuery&#8217;s ready method is that will only get you a browser DOM that is ready, but &hellip;","og_url":"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/","og_site_name":"Archive","article_published_time":"2016-02-25T20:31:28+00:00","article_modified_time":"2023-02-24T21:39:36+00:00","author":"Colin Phillips (Alumni)","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Colin Phillips (Alumni)","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/","url":"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/","name":"JavaScript that Waits for the SharePoint API\u2019s to be Ready - Archive","isPartOf":{"@id":"https:\/\/regroove.ca\/archive\/#website"},"datePublished":"2016-02-25T20:31:28+00:00","dateModified":"2023-02-24T21:39:36+00:00","author":{"@id":"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b"},"breadcrumb":{"@id":"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/regroove.ca\/archive\/2016\/02\/26\/javascript-that-waits-for-the-sharepoint-apis-to-be-ready\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Archive","item":"https:\/\/regroove.ca\/archive\/"},{"@type":"ListItem","position":2,"name":"JavaScript that Waits for the SharePoint API\u2019s to be Ready"}]},{"@type":"WebSite","@id":"https:\/\/regroove.ca\/archive\/#website","url":"https:\/\/regroove.ca\/archive\/","name":"Archive","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/regroove.ca\/archive\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b","name":"Colin Phillips (Alumni)","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/regroove.ca\/archive\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/14eeab0d377e9630e0983d9c08911979?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/14eeab0d377e9630e0983d9c08911979?s=96&d=mm&r=g","caption":"Colin Phillips (Alumni)"},"url":"https:\/\/regroove.ca\/archive\/author\/cphillips\/"}]}},"_links":{"self":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts\/1629"}],"collection":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/comments?post=1629"}],"version-history":[{"count":1,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts\/1629\/revisions"}],"predecessor-version":[{"id":1824,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts\/1629\/revisions\/1824"}],"wp:attachment":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/media?parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/categories?post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/tags?post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}