DANGER! Do not use SharePoint 2013's Script Editor Web Part
and why you should use Content Editor Web Part
At some point during SharePoint 2013's cycle, misinformation was given about Content Editor Web Part (CEWP), leading to recommendation to always use the new Script Editor web part over CEWP. This information is not accurate! CEWP works perfectly when used properly — by linking to an html file through your CEWP’s Content Link property.
I wouldn't make a post about this if Script Editor wasn't so horrible. As a professional developer, you should never use Script Editor even if it is to inject 5 lines of code. Your code should be linked to using a CEWP’s Content Link property.
Why You Should Use CEWP Instead of a Script Editor
Have you ever had to change your code at some point? I’m sure you have! Updating your HTML/CSS/JS is very simple when using a CEWP, and very difficult and dangerous when using a Script Editor.
This is the process for updating your code when using a Script Editor:
- Navigate to your page
- Click Edit
- Wait for the page to reload in edit mode
- Click Edit Snippet on your Script Editor (note: This is dangerous, if you have css/js that could interfere with the Script Editor or Edit tools, you may have to wiggle your way into the snippet editor or delete and re-create the Script Editor with new code)
- Wait for the snippet editor to display (note: Depending on the complexity of your code, the snippet editor may not properly display the snippet preview, or may break the snippet editor entirely)
- Scroll through the snippet editor and make your modifications, or over-write the snippet with your modified code
- Click OK on the snippet editor (note: Your changes may or may not be readily visible until checked in)
- Check in the changes
- Publish the changes
This is the process for updating your code using CEWP:
- Navigate to the document library where you saved your code file
- Click upload existing file
- Upload your code file that has your modifications
There is no exaggeration in this process. You can see this is a maintenance nightmare. What will you do if you add erroneous JS through a Script Editor that damages your page? Worst case: you might need to delete your page and re-create it. If you work with a CEWP, you have access to your code from outside of the page — if you upload erroneous JS, you can simply fix it in your IDE and re-upload it.
Misleading Articles & Posts
SharePoint removes all the script from Content Editor web part. Use Script Editor web part instead.
SharePoint 2013 removes the script tag entries in the Content Editor Web Part. In SharePoint 2013, all script details are supposed to be entered into the Script Editor Web Part.
Where Did These Articles Go Wrong?
The misinformation stems from the fact that there is more than one way to add HTML/CSS/JS into a CEWP. For example, there is the inline content way, where you can insert code directly into the web part’s “Content” area and there is the Content Link way. It is true that there appears to be problems if you add script tags into the inline Content area of a CEWP, but you should never do this.
Even if you only have a few lines of code, it should be a best practice to maintain that code in a separate file and use Content Link to embed it. Having all of your code in a single repository, rather than spread out across potentially many pages, will make maintenance easier.