Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 2 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:

<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <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>
      </properties>
    </data>
  </webPart>
</webParts>

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
add comment

Your Answer

 
discard

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.