{"id":735,"date":"2013-03-21T21:44:07","date_gmt":"2013-03-22T04:44:07","guid":{"rendered":"https:\/\/mmman.itgroove.net\/?p=735"},"modified":"2023-02-24T21:48:47","modified_gmt":"2023-02-24T21:48:47","slug":"how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too","status":"publish","type":"post","link":"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/","title":{"rendered":"How to Pull Content via jQuery from Another Web Site (Cross Domain) &#8212; And do it in SharePoint Too!"},"content":{"rendered":"<p>I recently ran into a situation where I wanted to pull some content from another web site, however, due to a configuration constraint, I was unable to to this in a normal fashion.\u00a0 Essentially, there was a table of data (a subset of the content on the page) on another site which I wanted to include on my web page, but using an IFrame to display the content simply wasn\u2019t an option (due to the fact, I only wanted a subset of the data on the page).\u00a0 In fact, I wanted to pull the data and display it in a web part, embedded in a web part page in SharePoint.\u00a0 The main issue was that since I was doing this across domains (which is a big no-no for many security reasons), I had to come up with another technique.<\/p>\n<p>After searching around the web for a while and trying to come up with a decent technique, I fell upon this thread on stackoverflow.com <a title=\"http:\/\/stackoverflow.com\/questions\/3076414\/ways-to-circumvent-the-same-origin-policy\" href=\"http:\/\/stackoverflow.com\/questions\/3076414\/ways-to-circumvent-the-same-origin-policy\">http:\/\/stackoverflow.com\/questions\/3076414\/ways-to-circumvent-the-same-origin-policy<\/a>.\u00a0\u00a0 There\u2019s a number of noted techniques in there, but the one I chose was not one of the many highlighted earlier on in the thread, but instead a little one buried later on where someone discusses \u201canyorigin.com\u201d.\u00a0 Basically, ruling the items out one by one, iframe doesn\u2019t work, as I wanted sub-content.\u00a0 The AJAX discussion they\u2019re talking about sounded very browser specific, and also quite flaky, depending on the browser version.\u00a0 And window.postmessage wasn\u2019t an option as I didn\u2019t have direct access to the content on the other end, so I had no way of embedding anything that into the destination.<\/p>\n<p>How <a href=\"http:\/\/anyorigin.com\">anyorigin.com<\/a> works is that you provide it a URL you\u2019re wanting to retrieve, and it will provide you back the content from that site, basically the entire web page, as a string in JavaScript (or jQuery if you want to call it that).\u00a0 You can then take that string and set it as the innerHTML value of a div (or other DOM node), and voila, you\u2019ve got the content from the other site.\u00a0 A little jQuery tweaking, and you\u2019ve got sub-content from that site.\u00a0 This was the complete solution for me \u2013 yay!\u00a0 So how did I do it?<\/p>\n<h2>The Code<\/h2>\n<p><em>Disclaimer 1: Do not use this technique to take other\u2019s intellectual property or copyright material.\u00a0 This is only meant to be used on content you already own, or have permission to use.<\/em><\/p>\n<p><em>Disclaimer 2: The possibility of having a cross site scripting attack is very real by using this technique \u2013 below, I\u2019ve added a line which strips out any inline JavaScript \u201cscript\u201d tags for this very reason \u2013 I highly recommend you do not remove that security precaution.<\/em><\/p>\n<p>So what my solution looks like is below.\u00a0 Essentially, it\u2019s a block of code I embed into a SharePoint script editor web part (in 2013 \u2013 in 2010 use a content editor), and it runs itself when the page loads.<\/p>\n<p>Simply replace the <span style=\"background-color: #ffff00\">yellow highlighted text<\/span> below with the website URL you\u2019re looking to query for data.\u00a0 And the <span style=\"color: #ff00ff\">purple text<\/span> below with the location of the content (see the comment).<\/p>\n<p><span style=\"font-family: Courier New;font-size: small\">\/\/Start of code<\/span><\/p>\n<p><span style=\"font-family: Courier New;font-size: small\">&lt;script src=&#8221;<\/span><a href=\"http:\/\/code.jquery.com\/jquery-1.9.1.min.js&quot;\"><span style=\"font-family: Courier New;font-size: small\">http:\/\/code.jquery.com\/jquery-1.9.1.min.js&#8221;<\/span><\/a><span style=\"font-family: Courier New;font-size: small\">&gt;&lt;\/script&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\n&nbsp;&nbsp;&nbsp;var ExternalURL = &#8220;<span style=\"background-color: #ffff00\">www.dummysite.com<\/span>&#8220;; \/\/ This address must not contain any leading &#8220;<\/span><span style=\"font-family: Courier New;font-size: small\">http:\/\/&#8221;<\/span><br \/>\n<span style=\"font-family: Courier New;font-size: small\">&nbsp;&nbsp;&nbsp;var ContentLocationInDOM = &#8220;<span style=\"color: #ff00ff\">#someNode &gt; .childNode<\/span>&#8220;; \/\/ If you&#8217;re trying to get sub-content from the page, specify the &#8220;CSS style&#8221; jQuery syntax here, otherwise set this to &#8220;null&#8221;<\/span><\/p>\n<p><span style=\"font-family: Courier New;font-size: small\">&nbsp;&nbsp;&nbsp;$(document).ready(loadContent);<\/span><br \/>\n<span style=\"font-family: Courier New;font-size: small\">&nbsp;&nbsp;&nbsp;function loadContent()<\/span><br \/>\n<span style=\"font-family: Courier New;font-size: small\">&nbsp;&nbsp;&nbsp;{<\/span><br \/>\n<span style=\"font-family: Courier New;font-size: small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var QueryURL = &#8220;http:\/\/anyorigin.com\/get?url=&#8221;<\/span><span style=\"font-family: Courier New;font-size: small\"> + ExternalURL + &#8220;&amp;callback=?&#8221;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$.getJSON(QueryURL, function(data){<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (data &amp;&amp; data != null &amp;&amp; typeof data == &#8220;object&#8221; &amp;&amp; data.contents &amp;&amp; data.contents != null &amp;&amp; typeof data.contents == &#8220;string&#8221;)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = data.contents.replace(\/&lt;script[^&gt;]*&gt;[sS]*?&lt;\/script&gt;\/gi, &#8221;);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (data.length &gt; 0)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (ContentLocationInDOM &amp;&amp; ContentLocationInDOM != null &amp;&amp; ContentLocationInDOM != &#8220;null&#8221;)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(&#8216;#queryResultContainer&#8217;).html($(ContentLocationInDOM, data));<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(&#8216;#queryResultContainer&#8217;).html(data);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br \/>\n&nbsp;&nbsp;&nbsp;}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;div id=&#8221;queryResultContainer&#8221;\/&gt;<\/span><\/p>\n<p><span style=\"font-family: Courier New;font-size: small\">\/\/End of code<\/span><\/p>\n<p>This is a simple way to be able to pull content (or sub-content) from another web site into a web part on your SharePoint site.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently ran into a situation where I wanted to pull some content from another web site, however, due to a configuration constraint, I was unable to to this in a normal fashion.\u00a0 Essentially, there was a table of data (a subset of the content on the page) on another site which I wanted to &hellip; <a href=\"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/\"><\/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>How to Pull Content via jQuery from Another Web Site (Cross Domain) - And do it in SharePoint Too! - 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\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Pull Content via jQuery from Another Web Site (Cross Domain) - And do it in SharePoint Too! - Archive\" \/>\n<meta property=\"og:description\" content=\"I recently ran into a situation where I wanted to pull some content from another web site, however, due to a configuration constraint, I was unable to to this in a normal fashion.\u00a0 Essentially, there was a table of data (a subset of the content on the page) on another site which I wanted to &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/\" \/>\n<meta property=\"og:site_name\" content=\"Archive\" \/>\n<meta property=\"article:published_time\" content=\"2013-03-22T04:44:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-24T21:48:47+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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/\",\"url\":\"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/\",\"name\":\"How to Pull Content via jQuery from Another Web Site (Cross Domain) - And do it in SharePoint Too! - Archive\",\"isPartOf\":{\"@id\":\"https:\/\/regroove.ca\/archive\/#website\"},\"datePublished\":\"2013-03-22T04:44:07+00:00\",\"dateModified\":\"2023-02-24T21:48:47+00:00\",\"author\":{\"@id\":\"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b\"},\"breadcrumb\":{\"@id\":\"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog Archive\",\"item\":\"https:\/\/regroove.ca\/archive\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Pull Content via jQuery from Another Web Site (Cross Domain) &#8212; And do it in SharePoint Too!\"}]},{\"@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":"How to Pull Content via jQuery from Another Web Site (Cross Domain) - And do it in SharePoint Too! - 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\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/","og_locale":"en_US","og_type":"article","og_title":"How to Pull Content via jQuery from Another Web Site (Cross Domain) - And do it in SharePoint Too! - Archive","og_description":"I recently ran into a situation where I wanted to pull some content from another web site, however, due to a configuration constraint, I was unable to to this in a normal fashion.\u00a0 Essentially, there was a table of data (a subset of the content on the page) on another site which I wanted to &hellip;","og_url":"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/","og_site_name":"Archive","article_published_time":"2013-03-22T04:44:07+00:00","article_modified_time":"2023-02-24T21:48:47+00:00","author":"Colin Phillips (Alumni)","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Colin Phillips (Alumni)","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/","url":"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/","name":"How to Pull Content via jQuery from Another Web Site (Cross Domain) - And do it in SharePoint Too! - Archive","isPartOf":{"@id":"https:\/\/regroove.ca\/archive\/#website"},"datePublished":"2013-03-22T04:44:07+00:00","dateModified":"2023-02-24T21:48:47+00:00","author":{"@id":"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b"},"breadcrumb":{"@id":"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/regroove.ca\/archive\/2013\/03\/21\/how-to-pull-content-via-jquery-from-another-web-site-cross-domain-and-do-it-in-sharepoint-too\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Archive","item":"https:\/\/regroove.ca\/archive\/"},{"@type":"ListItem","position":2,"name":"How to Pull Content via jQuery from Another Web Site (Cross Domain) &#8212; And do it in SharePoint Too!"}]},{"@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\/735"}],"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=735"}],"version-history":[{"count":1,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts\/735\/revisions"}],"predecessor-version":[{"id":1956,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts\/735\/revisions\/1956"}],"wp:attachment":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/media?parent=735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/categories?post=735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/tags?post=735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}