dinsdag 31 mei 2011

Changing the SharePoint 2010 search results XSLT

The core search results web part has a setting where you can change the XSL to change the appearance of the search results.

If you want results quickly, use the following XSL first to see what XML is return by the system. This will give you more insight in what is going on and will let you check if the data that you expected is returned by the server.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xmp><xsl:copy-of select="*"/></xmp>
</xsl:template>
</xsl:stylesheet>


You can then use an online tool like this http://markbucayan.appspot.com/xslt/index.html which can show the XSLT result immediately.

vrijdag 20 mei 2011

SharePoint 2010 ECMA property or field not initialized

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());
}