Tuesday, 24 April 2012

Sharing A Site Collection Term Store Among Multiple Site Collections

Site collection term stores are just special groups (IsSiteCollectionGroup = true) in an MMS term store (TaxonomySession.DefaultSiteCollectionTermStore). By default there is a one to one mapping between a site collection and the associated group, but in theory a group can be accessed by multiple site collections, and a site collection can only have one such group.

For each site collection, the root web o contains a property key in the form of SiteCollectionGroupId{Guid}, i.e. SiteCollectionGroupId75e2d28e-01ce-4100-b130-7898bed9ad24, the value of this property is the id of the corresponding group in the term store which serves as the "site collection term store" for this site collection.

Each group has a property called SiteCollectionAccessIds that contains a list of site collection IDs of which has access to this group; by default it only contains a single value.

Once you understand the information above, it is not difficult to achieve sharing a group among multiple site collections. Let’s say you have site collection SiteA and SiteB and you want them to share the same site collection term store, follow the steps below:
  1. Identify the site collection group SiteA is currently using. This can be done by examine SiteA.RootWeb.AllProperties and look for the key SiteCollectionGroupId{Guid}, mark down the value.
  2. Locate the site collection group based on the id retrieved from step 1 above, and then add SiteB.ID to this group’s SiteCollectionAccessIds.
  3. Point SiteB to the group identified in step 2. This can be done by modifying SiteB.RootWeb.AllProperties["SiteCollectionGroupId{Guid}"]with the group id retrieved from step 1.
You are done!

Inspired by the link below:
Restore a link between a site collection and a local term store group after restoring site collection from backup.

Friday, 30 March 2012

Binding Custom Action to Specific List Instance

In Elements.xml for custom action, use $ListId:Lists/xxx; as RgistrationId, replace xxx with the static name of your list. e.g.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction   Id="RibbonTest"
                Location="CommandUI.Ribbon"
                RegistrationId="$ListId:Lists/MyList;"
                RegistrationType="List"
                Sequence="1">
  
Please note that this approach only works in sandboxed solution, it doesn't work in a farm solution.  

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