{"id":446,"date":"2016-09-02T13:24:45","date_gmt":"2016-09-02T13:24:45","guid":{"rendered":"http:\/\/blog.softreeconsulting.com\/?p=446"},"modified":"2016-09-02T13:24:45","modified_gmt":"2016-09-02T13:24:45","slug":"inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/","title":{"rendered":"How to inject javascript to SharePoint or Office 365 master pages using CSOM"},"content":{"rendered":"<p>In office 365 environment, Microsoft had suggested not to modify the master pages. So to avoid modification, we can inject JavaScript at run time to the Master Pages of Office 365. This can be accomplished using the below CSOM Code (concocted by our <a href=\"https:\/\/www.softreetechnology.com\/offshore-sharepoint-developers\/\">Offshore SharePoint Developers<\/a>) in any provider hosted model app development. Fundamentally we are creating Custom Action at run time and injecting JavaScript using custom actions.<\/p>\n<p>These below mentioned code blocks will help you to inject the JavaScript to SharePoint master pages by using CSOM (Client Object Model). In this below mentioned examples I am going to add SP notification messages to SharePoint site. Similarly, you can inject your custom JavaScript functions to SharePoint or Office 365 sites.<\/p>\n<p>[code lang=&#8221;c&#8221;]<\/p>\n<p>using System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.Linq;<br \/>\nusing System.Text;<br \/>\nusing System.Threading.Tasks;<br \/>\nusing System.Net;<br \/>\nusing Microsoft.SharePoint.Client;<\/p>\n<p>namespace customaction<br \/>\n{<br \/>\nclass Program<br \/>\n{<br \/>\nstatic void Main(string[] args)<br \/>\n{<br \/>\nStringBuilder statusBarMsgBuilder = new StringBuilder(@&#8221;ExecuteOrDelayUntilScriptLoaded(function(){<br \/>\nvar statusID = SP.UI.Status.addStatus(&#8216;Title:&#8217;, &#8216;Type your status bar message here you want to add.&#8217;, true);<br \/>\nSP.UI.Status.setStatusPriColor(statusID, &#8216;yellow&#8217;);},&#8217;sp.js&#8217;)&#8221;);<\/p>\n<p>using (ClientContext context = new ClientContext(&#8220;http:\/\/yoursiteurl&#8221;))<br \/>\n{<br \/>\ncontext.Credentials = new NetworkCredential(&#8220;UserName&#8221;, &#8220;Password&#8221;);<br \/>\nWeb myWeb = context.Web;<br \/>\ncontext.Load(myWeb, mW =&amp;gt; mW.UserCustomActions);<br \/>\ncontext.ExecuteQuery();<\/p>\n<p>UserCustomActionCollection myCustomActionColl = myWeb.UserCustomActions;<\/p>\n<p>var myNewCustomAction = myCustomActionColl.Add();<br \/>\nmyNewCustomAction.Description = &#8220;My New Custom Action&#8221;;<br \/>\nmyNewCustomAction.Location = &#8220;ScriptLink&#8221;;<br \/>\nmyNewCustomAction.ScriptBlock = statusBarMsgBuilder.ToString();<br \/>\nmyNewCustomAction.Update();<\/p>\n<p>myWeb.Update();<br \/>\ncontext.ExecuteQuery();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p><strong>Hope this information will help you!<\/strong><\/p>\n<h6><strong>This solution is brought to you by our SharePoint professionals&#8230;<\/strong><\/h6>\n<p><em><a href=\"http:\/\/softreeconsulting.com\/\">Softree Technology <\/a>employs SharePoint consultants, who are experienced in writing for a multiplicity of SharePoint verticals including technical, promotional, creative, branding content, cataloguing and ethical media comprising journalism.<\/em><\/p>\n<p><em>With more than 10 years of industry experience these professionals have the best resources to deliver optimum results. They have been satisfying customers with some of the best SharePoint Strategies. \u00a0<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In office 365 environment, Microsoft had suggested not to modify the master pages. So to avoid modification, we can inject JavaScript at run time to the Master Pages of Office 365. This can be accomplished using the below CSOM Code (concocted by our Offshore SharePoint Developers) in any provider hosted model app development. Fundamentally we are creating Custom Action at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":452,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127,43],"tags":[122,123,124,125,126,128],"class_list":["post-446","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-office-365","category-sharepoint","tag-add-javascript-to-sharepoint-sites-programmatically","tag-how-to-inject-javascript-to-office365-master-pages-using-csom","tag-how-to-inject-javascript-to-office365-master-pages-using-custom-actions","tag-how-to-inject-javascript-to-sharepoint-master-pages-using-csom","tag-how-to-inject-javascript-to-sharepoint-or-office-365-master-pages-using-csom","tag-sharepoint-custom-actions-and-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Inject javascript to SharePoint or Office 365 master pages using CSOM<\/title>\n<meta name=\"description\" content=\"These below mentioned code blocks will help you to inject the JavaScript to SharePoint master pages by using CSOM (Client Object Model)\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inject javascript to SharePoint or Office 365 master pages using CSOM\" \/>\n<meta property=\"og:description\" content=\"These below mentioned code blocks will help you to inject the JavaScript to SharePoint master pages by using CSOM (Client Object Model)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-02T13:24:45+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inject javascript to SharePoint or Office 365 master pages using CSOM","description":"These below mentioned code blocks will help you to inject the JavaScript to SharePoint master pages by using CSOM (Client Object Model)","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:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/","og_locale":"en_US","og_type":"article","og_title":"Inject javascript to SharePoint or Office 365 master pages using CSOM","og_description":"These below mentioned code blocks will help you to inject the JavaScript to SharePoint master pages by using CSOM (Client Object Model)","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/","og_site_name":"Softree Technology","article_published_time":"2016-09-02T13:24:45+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"How to inject javascript to SharePoint or Office 365 master pages using CSOM","datePublished":"2016-09-02T13:24:45+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/"},"wordCount":326,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/#primaryimage"},"thumbnailUrl":"","keywords":["add javascript to sharepoint sites programmatically","How to inject javascript to Office365 master pages using CSOM","How to inject javascript to Office365 master pages using custom actions","How to inject javascript to SharePoint master pages using CSOM","How to inject javascript to SharePoint or Office 365 master pages using CSOM","SharePoint custom actions and javascript"],"articleSection":["Office 365","SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/","name":"Inject javascript to SharePoint or Office 365 master pages using CSOM","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/#primaryimage"},"thumbnailUrl":"","datePublished":"2016-09-02T13:24:45+00:00","description":"These below mentioned code blocks will help you to inject the JavaScript to SharePoint master pages by using CSOM (Client Object Model)","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/inject-javascript-to-sharepoint-or-office-365-master-pages-using-csomcsom\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to inject javascript to SharePoint or Office 365 master pages using CSOM"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=446"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}