Error to used SP.ClientContext using javascript Client Object Model

April 18, 2011


Hi all. Last week I got error, when I used Java Script code for get data of list SharePoint.

 

e.g:

<CommandUIHandlers>
<CommandUIHandler
Command="GK_Test_Button"
CommandAction="javascript:function Get_Selected_Item_ID()
{
var selectedItems = SP.ListOperation.Selection.getSelectedItems();
var ListId = SP.ListOperation.Selection.getSelectedList();
for(var i in selectedItems)
{
alert(‘Item ID = ‘ + selectedItems[i].id);
}
alert(‘items = ‘ + items);
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
alert(‘CTX = ‘ + ctx);
alert(‘CTX URL = ‘ + ctx.Url);
alert(‘WEB = ‘ + web);
alert(‘WEB URL = ‘ + web.Url);
}
Get_Selected_Item_ID()" />
</CommandUIHandlers>

 

After, I got error:

"Permission denied" error while trying to get SP.ClientContext using javascript Client Object Model on load event

 

The issue occurs because the scripts required by the Client OM are not loaded before the execution of the Javascript code.

Resolution:

<CommandUIHandler
Command="RedirectWithJS"
CommandAction="javascript:
var web;
SP.SOD.executeOrDelayUntilScriptLoaded(initialize,’SP.js’);
function getRelativeUrl()
{
alert(‘web.get_serverRelativeUrl() ‘ + web.get_serverRelativeUrl());
}
function getFailed()
{
alert(‘Failed to retrieve the server relative URL.’);
}
function initialize()
{
var clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();Ğ°
clientContext.load(web);
clientContext.executeQueryAsync(Function.createDelegate(this, getRelativeUrl), Function.createDelegate(this, getFailed));
}" />

 

More information this – http://support.microsoft.com/kb/2529245

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: