Showing posts with label WebPart. Show all posts
Showing posts with label WebPart. Show all posts

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, 6 February 2012

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