Sharepoint 2013 Script Editor web part CData for custom page layout using visual studio.

In order to include a javascript into a custom page layout,we can use script editor web part. Below snippet used to add a Script Editor web part to a layout.This script editor web part specific for Sharepoint 2013.But in the case of a Sharepoint 2010, we don't have a script editor web part.So we can use content editor web part in order to include a javascript to a page or layout.


<AllUsersWebPart WebPartZoneID="MainTopZone" WebPartOrder="1">
        <![CDATA[
      <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>$Resources:core,ImportErrorMessage;</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">$Resources:core,ScriptEditorWebPartTitle;</property>
<property name="Description" type="string">$Resources:core,ScriptEditorWebPartDescription;</property>
<property name="ChromeType" type="chrometype">None</property>
        <property name="Content" type="string">
        &lt;script type=&quot;text/javascript&quot; src=&quot;/_layouts/15/Ava.PM.Projects/Scripts/yourscript.js&quot; &gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
_spBodyOnLoadFunctionNames.push(&quot;yourfunctionOnload&quot;); 
&lt;/script&gt;
        </property>
</properties>
</data>
</webPart>
</webParts>
        ]]>

      </AllUsersWebPart>

Where the content highlighted in red is a Encoded HTML.The actual content is 

<script type="text/javascript" src="/_layouts/15/Ava.PM.Projects/Scripts/yourscript.js" ></script>
<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("yourfunction"); 

</script>

Use the below link to encode a your HTML.

HTML Encoder





Comments

Popular posts from this blog

Bulk update and delete using SPservice in SharePoint

List attachment link in SharePoint 2013 search.

Create page layout for SharePoint 2013 search result webpart.