Tuesday 28 February 2012

Override XSL templates in XSLT List View Web Part


You don’t even need to edit the page in SPD. XSLT List View Web Part has the ability to specify a XSL file through web part property, and the templates defined in the file will take precedence over the OOTB XSL code.  Now you can override the XSL templates selectively. Just save the custom XSL file in a library and point to it.
 In the example below I redefined the template for Description column:

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:o="urn:schemas-microsoft-com:office:office" ddwrt:ghost="show_all"
  <xsl:include href="/_layouts/xsl/main.xsl"/> 
  <xsl:include href="/_layouts/xsl/internal.xsl"/>   
   <xsl:template name="FieldRef_Text_body.Description" ddwrt:dvt_mode="body" match ="FieldRef[@Name='Description']" mode="Text_body" ddwrt:ghost="hide">
  <xsl:param name="thisNode" select="."/>
  <xsl:value-of select="$thisNode/@Description" />
 </xsl:template>  
</xsl:stylesheet>

In order to view the raw xml returned by the query, point to another custom XSL file that contains the following content:

<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>

Wednesday 15 February 2012

Hide "Recently Modified" for a Wiki page

Drop a Content Editor Web Part to the page and add the following:

<style type="text/css">
    .s4-recentchanges
    {
        display:none;
    }
</style>

Monday 13 February 2012

Setting InfoPath promoted properties (boolean type) through SharePoint has no effect.

I have an InfoPath form that contains some boolean (true/false) fields that being promoted to list columns upon publishing. However when I updated those values through SharePoint starndard property page and then opened the form itself the changes are not reflected. After hours of digging, it turned out that InfoPath service can only handle boolean value as 1/0, instead of true/false or 1/0 by the InfoPath thick client. Once I forced all the boolean fields and corresponding controls in the InfoPath form to be 1/0 instead of true/false, everything works fine.

Monday 6 February 2012

Error when publishing InfoPath form containg sandboxed code to a SharePoint form library


When I tried to publish an InfoPath form with codebehind to a form library, I got the error "The form template has been published to the server but it can only be opened in the InfoPath Filler". And it complained that there is something wrong with the InternalStartup method in the code. It turned out has something to do with the fact that the machine (VM) I published to is also acting as a domain controller. Changing the permission setting of 3 registry keys will resolve the issue.

http://support.microsoft.com/kb/2026191

Gochas When Using SPCalendarView


Please note that there are a few gotchas when using SPCalendarView in your web part class:

1. Your web part class has to inherit from Microsoft.SharePoint.WebPartPages.WebPart
2. EnableV4Rending of SPCalendarView has to be false otherwise the css class specified in BackgroundColorClassName of SPCalendarItem has no effect
3. BackgroundColorClassName of SPCalendarItem is only applicable if the IsAllDayEvent is set to true
4. There is a bug with SPCalendarView control when EnableV4Rending is set to true, SPCalendarItem stays when switching month, this can be fixed by applying December 2011 SharePoint Foundation CU



using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
 
namespace WebPartTest.MyCalendarWebPart
{
    [ToolboxItemAttribute(false)]
    public class MyCalendarWebPart :  Microsoft.SharePoint.WebPartPages.WebPart
    {
        private SPCalendarView calendar;
 
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
 
            calendar = new SPCalendarView();
            calendar.EnableV4Rendering = false;
            calendar.SelectedDate = GetSelectedDate();
            calendar.EnableViewState = true;
            calendar.ViewType = GetViewType();
            calendar.DataSource = GetCalendarItems();
            calendar.DataBind();
            Controls.Add(calendar);
        }
 
        private SPCalendarItemCollection GetCalendarItems()
        {
            SPCalendarItemCollection items = new SPCalendarItemCollection();
 
            items.Add(new SPCalendarItem(){
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddHours(5),
                hasEndDate = true,
                IsAllDayEvent = true,
                Title = "Lunch",
                Location = "KFC",
                CalendarType = Convert.ToInt32(SPCalendarType.Gregorian),
                BackgroundColorClassName = "LunchClass",
                DisplayFormUrl = "www.google.com"
            });
 
            items.Add(new SPCalendarItem()
            {
                StartDate = DateTime.Now.AddDays(1),
                EndDate = DateTime.Now.AddDays(2),
                hasEndDate = true,
                IsAllDayEvent = true,
                Title = "Vacation",
                Location = "Cuba",
                CalendarType = Convert.ToInt32(SPCalendarType.Gregorian),
                BackgroundColorClassName = "VacationClass",
                DisplayFormUrl = "www.microsoft.com"
            });
 
            return items;
        }
 
        private string GetViewType() 
        { 
            string viewType = string.Empty;
            string calendarPeriod = Page.Request.QueryString["CalendarPeriod"];
            if (calendarPeriod != null) 
            {
                switch (calendarPeriod.ToString().ToLower()) 
                { 
                    case "day":                     
                        viewType = "day"; 
                        break; 
                    case "month":                     
                        viewType = "month"; 
                        break; 
                    case "timeline":                     
                        viewType = "timeline"; 
                        break; 
                    default:                     
                        viewType = "week"; 
                        break; }
            }
            else { viewType = "week"; } 
            
            return viewType;
        } 
 
        private string GetSelectedDate()
        {
            string selectedDate = Page.Request.QueryString["date"];
        
            return (selectedDate == null)?String.Empty:selectedDate; 
        }
    }
}