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:

  1. Navigate to your page
  2. Click Edit
  3. Wait for the page to reload in edit mode
  4. 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)
  5. 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)
  6. Scroll through the snippet editor and make your modifications, or over-write the snippet with your modified code
  7. Click OK on the snippet editor (note: Your changes may or may not be readily visible until checked in)
  8. Check in the changes
  9. Publish the changes

This is the process for updating your code using CEWP:

  1. Navigate to the document library where you saved your code file
  2. Click upload existing file
  3. 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

http://summit7systems.com/dont-script-in-the-wrong-web-part/

The Content Editor Web Part still seems to work fine for HTML or XHTML. However, if you attempt to add JavaScript to your page using the Content Editor Web Part in SharePoint 2013, the script tag entries will be removed. In SharePoint 2013/ Office 365, any script should to be entered into the Script Editor Web Part.

https://social.msdn.microsoft.com/forums/sharepoint/en-US/dc299877-ac9e-4493-bf85-801faf0ec1d3/content-editor-webpart-losing-its-javascript

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.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.