When you are experimenting with the SharePoint 2010 ECMA scripts, you will at some point get this exception. Some time ago I was experimenting with the ECMA JScript included in a page layout and received this error many times.
After trying and reading a lot of stuff (this also includes tearing parts of my hair), I figured it out. Basically, what I wanted to do is get the permissions of the current user and if the user is not an administrator, hide certain controls on the page layout.
In this post, I will only focus on getting the current user's permissions as this was the part that I lost most of the time.
Normally when using these ECMA scripts, you first tell the system to load a certain object before you can access its properties. You would think that if you use something like this:
var ctx = new SP.ClientContext.get_current();this.web = ctx.get_web();ctx.load(this.web);
would return you the full web object but it's not. Ok maybe I should have read the Microsoft documentation before I investigated this myself because there it's clearly indicated that some properties will never be loaded unless explicitly requested.
The property I want to access is the 'EffectiveBasePermissions' property and this is one of those properties that will not get loaded by default. So, here is the full JScript that shows you how to load such a property and access it:
1. Load the sp.js file so you can actually use the client object model
<SharePoint:ScriptLink Name="sp.js" LoadAfterUI="true" Localizable="false" runat="server" ID="ScriptLink1" />
2. Wait until sp.js is fully loaded
ExecuteOrDelayUntilScriptLoaded(GetCurrenUserPermissions, "sp.js");
3. Load the property
function GetCurrenUserPermissions(){var ctx = new SP.ClientContext.get_current();this.web = ctx.get_web();ctx.load(this.web, 'EffectiveBasePermissions');ctx.executeQueryAsync(Function.createDelegate(this, onSuccessMethod),Function.createDelegate(this, onFailureMethod));}function onSuccessMethod(sender, args){alert(this.web.get_effectiveBasePermissions());}function onFailureMethod(sender, args){alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());}
Geen opmerkingen:
Een reactie posten