SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a powershell script that creates article pages in a publishing portal site collection. As part of that script I insert the content for each page by setting the value of the $item["Page Content"] field for the SPListItem that represents that page.

However, I must now insert a javascript block to that page as well. Suppose I have this block: <script type="text/javascript">alert('test');</script>. If I add it to the Page Content field and call Update() it is not actually added to the rendered page's source code.

I understand that the correct way to do this via the Sharepoint UI is to use the Embed Code ribbon button. How can I do this with Powershell?

share|improve this question
up vote 3 down vote accepted

What happens when you use Embed Code is that a Script Editor web part is inserted into your page.

If you want to do that programmatically then read Programmatically adding Web Parts to Rich Content in SharePoint 2010 down in the comment there is a powershell version of the code.

The web part has the following xml if exported:

  <webPart xmlns="">
      <type name="Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
        <property name="HelpMode" type="helpmode">Navigate</property>
        <property name="ExportMode" type="exportmode">All</property>
        <property name="HelpUrl" type="string" />
        <property name="Hidden" type="bool">False</property>
        <property name="Description" type="string">Allows authors to insert HTML snippets or scripts.</property>
        <property name="Content" type="string">&lt;script&gt;alert('test')&lt;/script&gt;</property>
        <property name="CatalogIconImageUrl" type="string" />
        <property name="Title" type="string">Script Editor</property>
        <property name="AllowHide" type="bool">True</property>
        <property name="AllowMinimize" type="bool">True</property>
        <property name="AllowZoneChange" type="bool">True</property>
        <property name="ChromeType" type="chrometype">None</property>
        <property name="AllowConnect" type="bool">True</property>
        <property name="Width" type="unit" />
        <property name="Height" type="unit" />
        <property name="TitleUrl" type="string" />
        <property name="AllowEdit" type="bool">True</property>
        <property name="TitleIconImageUrl" type="string" />
        <property name="Direction" type="direction">NotSet</property>
        <property name="AllowClose" type="bool">True</property>
        <property name="ChromeState" type="chromestate">Normal</property>

If it's the same JavaScrip you want on all pages, then you might want to consider creating a Feature With a <Control> element to put it into AdditionalPageHead

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.