vrijdag 17 juni 2011

JQuery UI Buttons lose styling after postback

A couple of days ago I had this annoying problem: I had a page with some standard ASP.NET buttons in an update panel which I styled using JQuery UI.

$(".btnInsert").button();


Everything worked well until I caused a postback in the update panel. It seemed that my JQuery would not execute anymore.



I just want to share a simple solution to overcome this:



<script type="text/javascript">
    $(document).ready(function () {
       
        // Launch JQuery at first page load
        ApplyJQuery();
        // Register event so code executes at the end of each request
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        // Event code
        function EndRequestHandler(sender, args) {
            ApplyJQuery();
        }
        function ApplyJQuery() {
            $(".btnInsert").button();
        }
    });
</script>

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

maandag 18 april 2011

SharePoint 2010 refinement panel

When you do a search in SharePoint 2010 and land on the results page, by default on the left you will see the refinement panel. This panel will show up if there are enough results being returned by the system. I think default the minimum is five.

At some point you might want to add your own custom categories. This is already explained in a lot of other posts but I want to make some things clear. Things that are sometimes incorrectly postulated in some articles.


If you want to add your own category, edit the options of the refinements panel and open the 'Refinement' category. You will want to edit the 'Filter Category Definition' xml. (tip: for easy editing, copy paste the XML here to reformat: http://www.shell-tools.net/index.php?op=xml_format).

You will notice that each category is represented by a 'Category' tag with all kinds of options. Basicaly, you just need to copy paste such a tag and edit its options. Don't forget to turn off the 'Use Default Configuration' option or none of your changes will be saved.


1. Create a managed property for your custom category. Anything of which you can make a managed property, can be used as a category (as far as I know).

2. The 'Type' property can end with 'ManagedPropertyFilterGenerator' for any column that is not a managed metadata column or 'TaxonomyFilterGenerator' if the column is a managed metadata column.

3. Your managed metadata does not have to be in lower case for it to show up. Just use the casing you used.

4. Very important property to notice is the 'MetadataThreshold' option. This option defines how many times the column must appear in the search results before the category becomes visible in the refinement panel.

5. Tip: you can only create a managed property from a column that is actually being used. So there must be at least one item which has the metadata column and it must have data. You must then crawl the complete system for it to become available when configuring managed properties.

6. Tip: First make sure you can search your managed property by typing this in the search box: 'managedPropertyName: valueToSearch'.

7. Tip: if your managed metadata does not exist (not configured or typo in the XML), the refinement panel will tell you. So if you configured it correctly and no error message is shown, check if the other options are ok.

vrijdag 15 april 2011

Doing a redirect in ItemUpdated event

Sometimes it might be handy to, based on an update of an item, redirect the user to a different page. For example, if a given metadata field of the item has a certain value, redirect the user to another page.

Although this request seems fairly simple, it's not so easy to do. Why?

1. If you want to do it during ItemUpdating, your event thread will not be finished and your item will not actually be updated.
2. If you do it during ItemUpdated, you will not have access to the HttpContext so you cannot use SPUtility.Redirect. Or can you ... ? :) I really lost a day or more searching for this but now have a working solution.

It seems that with some workarounds, you can have access to the HttpContext in the ItemUpdated. I will here briefly explain how to do it:

1. Create two private global variables in your event receiver:
private HttpContext _httpContext;
private static HttpContext _sHttpContext;

2. Get a hold of the HttpContext in the constructor
_httpContext = HttpContext.Current;

3. In ItemUpdating, again get a hold of the HttpContext
_sHttpContext = _httpContext

4. Use SPUtility.Redirect and _sHttpContext to redirect to a page of your choice.

Unable to create a new group in the SharePoint 2010 managed metadata service

I was having this problem lately and read some articles that explained me to go to the list of service applications via the central administration, select managed metadata service from the list, click the 'Administrators' button in the ribbon and add myself to the list giving me 'Full control' access. This didn't change anything and it's also not necessary if you are a farm administrator.

What is important is that you just add your self to the list of managed metadata owners in the first screen of the managed metadata service management tool which you can access via any site collection via the site settings. This allowed me to create new groups, term sets and terms.

Managed metadata service connection not available

After a fresh installation of your SharePoint 2010 environment, you could face this message. This is because the managed metadata service has not yet been configured correctly. The easiest way to do it in my opinion is by using the configuration wizard in the central administration.

Open central administration and click on 'Configuration Wizard' in the left bar. When you start the wizard, you will get a list of all possible services. In the list, select managed metadata service. You can of course also enable/disable other services. Go to the next step to start the configuring process. The last step also lets you create a new site collection but it's optional.