Subscribe: SharePoint SharePoint SharePoint

Friday, July 5, 2013

Content Editor Web Part script error

Many a times when we add a ECMA script to a CEWP, it does not work as expected. This mainly happens because CEWP does not start a server thread. ECMA script can only be executed when a server thread or worker process is associated to it. So in order to make it work, we need to add a function i.e. "ExecuteOrDelayUntilScriptLoaded". It first loads the "SP.js" file so that it can start a server thread.

SharePoint 2010


After loading the "SP.js" file , the ECMA script in the CEWP is executed without any error.

Following is the code depicting the same :



<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script><script type="text/javascript">// <![CDATA[
$(document).ready(function() {
ExecuteOrDelayUntilScriptLoaded(function test(){
var clientContext = SP.ClientContext.get_current();
this.website = clientContext.get_web();
clientContext.load(website);
clientContext.executeQueryAsync(Function.createDelegate
(this, this.onSuccessMethod), Function.createDelegate
(this, this.onFailureMethod));}, "sp.js")
});

function onSuccessMethod(sender, args){
alert('web title:' + website.get_title());
}
function onFailureMethod(sender, args){
alert('request failed' + args.get_message())
}
</script>

0 comments:

Post a Comment