SharePoint 2010 - Editing the Content Editor Web Part

I am using Javascript inside the SharePoint 2010 Content Editor web part to insert a Silverlight object. I need to do this this way, instead of using the Silverlight Web Part, as Silverlight Web Part is not currently included. This is done entirely with Javascript.

The problem occurs when I switch later to edit Javascript inside CEWP. I see that the original Javascript is requesting the generation of a Silverlight object, and I can, this is the weird part, CEWP has all the generated HTML code. The Silverlight object is right there attached to scrept.

So now, when I save, I save the script to create the Silverlight object AND the HTML that was previously generated, effectively duplicating the Silverlight object. If I edit again, now I will have three Silverlight objects, etc.

You can see this in action for yourself with the following code example:

  • Add a new content editor web part to a page in SharePoint 2010
  • Change Source HTML
  • Add the following code:

    <script type="text/javascript">document.write("Hello<br/>");</script>

Save the web part and you're done. Now just keep editing CEWP. Each time you click “Edit Web Part”, “Hello” will be added to your script.

How can I use Javascript to insert DOM elements and not have the generated HTML in CEWP?

+6
javascript sharepoint-2010
source share
3 answers

It does not work because SharePoint 2010 does not want you to copy and paste scripts into the editor. Instead, you should put your scripts in a txt file (yes, this is the correct txt file) stored in SharePoint, and then specify CEWP to use this file as a source.

First create a file with all your code (both Javascript and HTML), basically everything that you usually pasted into the content editor.) Be sure to wrap your Javascript in the <script type="text/javascript"> and save the file with .txt, for example, "scripts.txt".

Then add CEWP to your page and select “Edit Web Part”. In the content editor area on the right in the "Content Link" section, add the URL to the txt file and click "Apply" and you're done.

Take a look at the following URL for a full description of this change in SharePoint 2010: http://sptwentyten.wordpress.com/2010/08/31/insert-javascript-into-a-content-editor-web-part-cewp/

+11
source share

Using jQuery is probably much safer than document.write, which can break javascript further down the page.

Or use the code in this link to put pure HTML in CEWP instead of using JavaScript:

http://karinebosch.wordpress.com/silverlight-meets-sharepoint/walkthrough-2-hosting-silverlight-3-in-a-content-query-web-part/

0
source share

Another option is the HTML Form Web Part (in the Forms category). This can be used to connect to other web parts, but more simply, it can be used to edit JavaScript directly in the web part. It seems that the rules for the Web Parts of the Content Editor do not apply to the web parts of the HTML form, which provides more flexibility.

More information from Microsoft is here:

http://office.microsoft.com/en-us/sharepoint-server-help/use-the-html-form-web-part-to-filter-and-display-data-in-another-web-part- HA101791813.aspx # _Toc274731120

0
source share

All Articles