Top Menu

Thursday, March 10, 2016

SharePoint Online - JavaScript Injection with PowerShell

Scenario:
Updating the Master Page for SharePoint Online is not recommended by Microsoft now.....fine. So how do we change the UI then? And the answer is...by injecting JavaScript and CSS.

Solution:
You might want to watch this video of JavaScript injection which we will not follow here as injecting JavaScript and/or CSS through Visual Studio takes little bit of time as shown in the above video so I am putting pieces together to inject JavaScript using PowerShell very very...very quickly. Thank you Office Dev PnP's Erwin van Hunen.

Note: We are not going to use SharePoint Online Management Shell ;o) instead we will use Windows PowerShell.
  1. Install PnP PowerShell Cmdlets for SharePoint Online
    Url: https://github.com/officedev/pnp-powershell/releases
  2. Perform a quick test: Run the command below in Windows PowerShell to see if it is available
     Get-Command Add-SPOJavaScriptBlock
  3. Connect to the site where you want to push JavaScript code:
    Connect-SPOnline https://tenant.sharepoint.com/sites/portal
  4. Push the JavaScript code.
    Add-SPOJavaScriptBlock -Name MyScript -Script "alert('Hello World');"
    or
    Add-SPOJavaScriptLink -Name JQuery -Url https://code.jquery.com/jquery.min.js
  5. You are done. Now browse the site and you will get the annoying Hello World alert on all the pages.
Here is the complete script:
#
# This is a sample script and is provided as is.
# Author: Tahir Naveed
# Description: This script connects to a SPO site and 
# injects a JavaScript code block which will execute on all of the pages.
#
#

$url = "https://tenant.sharepoint.com/sites/portal"
$ScriptName = "MyScript"
$scriptBlock = "alert('Hello World');"

try
{
    #Connect to the SPO site
    Connect-SPOnline $url
    
    #Remove script if added before
    Remove-SPOJavaScriptLink -Name $ScriptName

    #Add the script
    Add-SPOJavaScriptBlock -Name $ScriptName -Script $scriptBlock

}
catch
{
    Write-Host "$_.Exception.Message"
}

Write-Host "Completed successfully." -ForegroundColor Green
Ref: Introduction to PnP PowerShell Cmdlets
Ref: OfficeDev/PnP-PowerShell Reference

67 comments:

  1. Great blog thanks for sharing Masters of building brands, Adhuntt Media is making waves in the Chennai digital market! Known for their out of their box ideas, if a creative overhaul for your brand is what you need, you’ve come to the right place. Right from Search Engine Optimization-SEO to Social Media Marketing, Adhuntt Media is your pitstop for original brand identity creation from scratch.
    digital marketing company in chennai
    seo service in chennai
    web designing company in chennai
    social media marketing company in chennai

    ReplyDelete
  2. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. Free Classified Ads In Nigeria

    ReplyDelete
  3. They use injectors instead of carburetors that depend on pressure differential, venturi, planes, metering poles, power valves, stifles, glides, and so forth. injector cleaner

    ReplyDelete
  4. This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs… two shot injection moulding

    ReplyDelete
  5. very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! 2k moulding

    ReplyDelete
  6. You have observed very interesting points ! ps nice website . web design in new york

    ReplyDelete
  7. I would like to express some appreciation to this writer just for rescuing me from such a challenge. As a result of looking out through the internet and getting techniques that were not helpful, I was thinking my entire life was over. Being alive without the approaches to the difficulties you’ve sorted out through this posting is a serious case, as well as the kind that would have badly damaged my career if I had not encountered your website. Your know-how and kindness in taking care of a lot of things was excellent. I am not sure what I would’ve done if I hadn’t come upon such a step like this. I am able to at this time look ahead to my future. Thanks for your time so much for this skilled and effective help. I will not hesitate to endorse your web sites to any individual who should get care on this problem. ny web design firms

    ReplyDelete
  8. Wow, suprisingly I never knew this. Keep up with good posts. branding san francisco

    ReplyDelete
  9. I’d forever want to be update on new posts on this website , bookmarked ! . branding agencies in san francisco

    ReplyDelete
  10. The study of Home Science offers openings for Careers related to: machine learning training in hyderabad

    ReplyDelete
  11. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also cheapest SMM panel

    ReplyDelete
  12. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also Benefits of CBD in Treating Social Anxiety Disorder

    ReplyDelete
  13. It proved to be Very helpful to me and I am sure to all the commentators here! https://andotherbrands.com/product-brand/cbdfx/

    ReplyDelete
  14. Oh my goodness! Incredible article dude! Thank you, However I am having difficulties with your RSS. I don’t understand the reason why I cannot join it. Is there anybody else having the same RSS issues? Anybody who knows the answer will you kindly respond? Thanks!!
    Technology

    ReplyDelete
  15. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. medical spa industry growth

    ReplyDelete
  16. Managed web hosting refers to a dedicated web host who are managing the web hosting servers for their clients. This type of cheap wordpress hosting is suitable for companies which do not have their own IT resources, or who do not intend to tie up their IT resources for any tasks for web hosting but instead to focus in their operational issues.

    ReplyDelete
  17. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. Semaglutide

    ReplyDelete
  18. Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share. fait a la main

    ReplyDelete
  19. Here you will learn what is important, it gives you a link to an interesting web page: kündigung

    ReplyDelete
  20. Amazing thoughts:)
    That was really helpful!
    Very useful blog thanks for sharing.
    According to Indeed, the average java developer salary ranges from $44k-$54k per annum with an average of $50k per annum. Payscale gives java developer salary figures as low as $48k per annum with high statistics of $81k per annum. Well, thank you for the information in the above blog.

    ReplyDelete
  21. A great article and I look forward to readying your next one. Ozempic

    ReplyDelete

  22. A few regions are exceptionally serious. Subsequently one needs to have some expertise in one's specific region and give significant material.https://onohosting.com/

    ReplyDelete
  23. These indexes can be gotten to from pretty much anyplace that has a web association. https://onohosting.com/

    ReplyDelete
  24. It is quite beneficial, although think about the facts when it reaches this target. frclothing.co

    ReplyDelete
  25. That was amazing blog. Thanks for sharing it with us.
    Java classes in Pune

    ReplyDelete

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...