How To Adjust Sharepoint OOTB Webparts Relative Paths

Posted by Ahmed Tarek Hasan on 1/13/2013 09:30:00 PM with 1 comment
While working on some content query webparts (CQWP), I had the need to apply my own XSLT for styling. So, after building my custom XSLT, I thought I have finished the hard part and that everything is almost ready. Unfortunately this wasn't true.

In my ".webpart" file I needed to set the path for my custom XSLT and it popped into my head that I can use the "~sitecollection" token to be replaced by the real site collection URL. So, I did it and started testing my webpart. Unfortunately, it didn't work :(

The webpart showed me an error and after tracing the error I figured out that the webpart couldn't recognize the XSLT file path because the "~sitecollection" token wasn't resolved to the real site collection URL.

After searching, I found out that this is a known issue as the "~sitecollection" token is only resolved when it is found in some webpart properties but not all of them. The XSLT path property is one of those which are not resolved.

So, to fix this issue, I kept the "~sitecollection" token in the XSLT and wrote some code in the feature activation to replace this token with the real site collection URL. This way, the XSLT path will be recognized by the webpart and the webpart will work.

Now, it is time to see some code.

The ".webpart" file (just a sample of the file)
 <webPart xmlns="">
   <type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
   <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    <property name="Title" type="string">OOTB WebPart</property>
    <property name="ItemXslLink" type="string">~sitecollection/Style Library/XSL Style Sheets/OOTB_WebPart_ItemStyle.xsl</property>
    <property name="MainXslLink" type="string">~sitecollection/Style Library/XSL Style Sheets/ContentQueryMain_OOTB_WebPart.xsl</property>

The "Elements.xml" file
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
  <Module Name="NewsWebParts" Url="_catalogs/wp" RootWebOnly="TRUE">
    <File Path="OOTB_WebPart.webpart" Url="OOTB_WebPart.webpart" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
      <Property Name="Group" Value="MyWebParts" />
      <Property Name="Title" Value="OOTB WebPart" />

The feature activation code
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace DevelopmentSimplyPut.Features.WebParts
    public class WebPartsEventReceiver : SPFeatureReceiver
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
            AdjustSiteRelativePaths(properties, "OOTB_WebPart.webpart");
  public static void AdjustSiteRelativePaths(SPFeatureReceiverProperties featureProp, string webPartName)
   SPSite site = null;

   if (featureProp.Feature.Parent is SPSite)
    site = featureProp.Feature.Parent as SPSite;
    site = ((SPWeb)featureProp.Feature.Parent).Site;

   if (site != null)
    SPList webPartsGallery = site.GetCatalog(SPListTemplateType.WebPartCatalog);
    SPListItemCollection allWebParts = webPartsGallery.Items;

    if (!webPartName.EndsWith(".webpart", StringComparison.OrdinalIgnoreCase))
     webPartName += ".webpart";

    SPListItem webPart =
     from SPListItem wp
     in allWebParts
     where wp.File.Name == webPartName
     select wp

    if (webPart != null)
     string siteCollectionUrl = site.ServerRelativeUrl;
     if (!siteCollectionUrl.EndsWith("/", StringComparison.OrdinalIgnoreCase))
      siteCollectionUrl += "/";

     string fileContents = Encoding.UTF8.GetString(webPart.File.OpenBinary());
     fileContents = fileContents.Replace(@"~sitecollection/", siteCollectionUrl);

So, now, whenever the feature provisioning the webpart is activated, the webpart in the webparts gallery will be updated with the new ".webpart" file, then the "~sitecollection" token will be replaced with the real site collection  URL.

That's it, mission accomplished.

Categories: ,