About Scripts, Web Parts and Urban Myths

Today, I came across conversations, initiated by Marc Anderson (@sympmarc on Twitter), about the Content Editor Web Part.

Reported by Marc on his blog:

One of the things I heard at SPTechCon several times was that in SharePoint 2010, it is no longer possible to put script into Content Editor Web Parts (CEWPs). Instead, I was told, you have to use the new HTML Form Web Part.

And on twitter:

sympmarc Just verified that you *can* put script in a #CEWP in #SP2010, contrary to what several people said at #SPTechCon . Urban myths can kill…

EUSP RT @sympmarc: Just verified that you *can* put script in a #CEWP in #SP2010 <- Problems with <form> tags in CEWP 2010.

sympmarc @eusp: Problems with <form> tags in CEWP 2010. -> Different issue. Some HTML tags may not work, but it looks like script does.

sympmarc @eusp Remember that basically the whole page is a form already. That may be the issue rather than SP2010.

 

Let me try and clarify how this works.

The Content Editor Web Part

It was August 2008, I had just started my blog, and I was already writing about the CEWP. At that time, I did not even mention the ability to link to external content. I only talked about this several months later, when I started promoting an architecture where scripts are stored in a central library.

In SharePoint 2007, the most advertised way to add scripts to a SharePoint page is via the “Source Editor” option of the CEWP. So no wonder people are lost when they start using SP 2010: no “Source Editor” button anymore!

It doesn’t mean that the feature has been removed though. As with many others, the option – renamed “Edit HTML source” - is now located in the ribbon, and becomes visible when you click on the content section of the CEWP (in edit mode), or select the “Edit Web Part” option:

In addition, SharePoint tries to help: it will screen the code you enter, and you’ll receive this notification:
“the HTML source you entered might have been modified”

The cool part is that SharePoint will never tell you whether anything was modified, you’re on your own to figure it out. Feel free to take a look at some examples I collected when I posted the question on SharePoint Overflow two months ago.

Another issue with the CEWP is that you cannot include form elements. This is not new to 2010, it has always been the case even with previous versions. As Marc explains in the above tweet, the reason is that the whole page is already a form. So here comes…

The HTML Form Web Part

Yes, you can use the HTML Form Web Part to include scripts in a page. Yes, it will also accept form tags, it’s its primary purpose. Yes, it has this “Source Editor” button everybody is looking for. And no, it is not a new Web Part, it was already present in SP 2007. It is actually one of the building blocks of my SharePoint User Toolkit, in both SP 2007 and SP 2010.

So why is everybody promoting the CEWP, if the HTML Form Web Part offers more?

For one, it seems that few people actually know about the capabilities of this Web Part (it is better known for its role in Web Part connections). But another reason is that it doesn’t have the “Content Link” option that I recommend as a good practice.

Other Web Parts

Can’t use the CEWP or the Form Web Part on your site? There are still other Web Parts available (not to mention the option to add scripts outside Web Parts). Although they are not as convenient as the CEWP and the HTML Form Web Part, they can help in specific cases.

For example, back in SP 2003, forms (new, display, edit) were not as easy to edit as today. I used to rely on a Page Viewer Web Part, which allowed me to modify scripts without editing the form page itself. Come talk to me if you need more details!

About these ads

10 thoughts on “About Scripts, Web Parts and Urban Myths

  1. As soon as the beta for SharePoint Foundation was available we downloaded it and installed it into a test server. Not having the Edit Source button threw me for a loop for a while.

    On the internet everyone was asking about it and very few were talking and those that were, were often only speaking in generalities. I was really struggling until I was able to find enough hints on the internet to finally figure out what and how to use the Edit HTML button.

    It was during this time that I resolved to finally build my Scripting Center and now often create the scripts in notepad and link to the file instead of entering the script directly into the web part. Much easier to update a script by editing one file.

    Hmmm. . .wonder where I got that idea?

  2. Pingback: Client side scripts: packaged Web Part vs. Content Editor Web Part « Path to SharePoint

  3. Pingback: Adding Script into a Content Editor Web Part (CEWP) in SharePoint 2010 « Marc D Anderson's Blog

  4. Anyone have any examples of how to use the HTML Form webpart to simply pass the T1 variable to a HTTP string and have it open it in a new window?

  5. Thank you for make this post.

    Nice to get an explanation as to how to avoid the “the HTML source you entered might have been modified”.

    I still use the Content Editor webpart to reference the javascript in a document library in order get versioning on my javascript an also avoiding the bug “the HTML source you entered might have been modified”.

    But the HTML Form Web Part will be usefull when this might be overkill :)

  6. I have followed your suggestions for adding the status indicator used the CEWP. On 2007 share point it worked fine even after we converted to 2010. Any new site made on 2010 that includes the code does not work and gives this result:

    This is the code in the calculated column

    =”•”

  7. That didn’t work. Lets try again.

    I have followed your suggestions for adding the status indicator used the CEWP. On 2007 share point it worked fine even after we converted to 2010. Any new site made on 2010 that includes the code does not work and gives this result:

    // •

    This is the code in the calculated column

    // “=”•”

  8. Pingback: Script insertion via a Page Viewer Web Part | SharePoint Samurai

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s