in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Tech MOSS Team

August 2007 - Posts

  • Using ~SiteCollection prefix in SharePoint CSS files

    Using CSS files is the recommended way for incorporating the chrome in a SharePoint WCM solution. Among the various definitions of the layout pieces there are also images’ references. Each such reference can be defined using an absolute of relative URL. As long as you use images in the same directory as the CSS file there is no problem at all: you can manage all the paths easily by simply using one of the URL kinds. Unfortunately a problem occurs when you would like to use images from other directories for example the PublishingImages folder. They should be referenced via relative URLs. Using absolute URLs results in problems when a Site Collection doesn’t reside in the root or /sites directory. Also sharing a CSS file among different Site Collections isn’t possible while using PublishingImages folder: there is no simple way to make the images’ paths be defined dynamically based on the Site Collection calling the CSS file.

    Facing this challenge Erik and I figured out a solution: letting the CSS files be parsed by the ASP.NET handler and making SharePoint webcontrols write correct URLs based on the caller Site Collection. As this approach requires connecting of a file type with the parser it seems convenient to make all the files with .css extensions get parsed by ASP.NET. As there is a big possibility that you may use more than one CSS definition file within a solution it would create quite an overhead. That’s why we have decided to define a new file extension for our dynamic CSS: CSSX.

    Getting our .cssx files parsed by ASP.NET required two steps: sending a .cssx file to the ASP.NET pageHandler (code 1) and making the page built using the buildProvider (code 2). Below you can see both code snippets added within the web.config of the Web Application:

    Code 1: Sending .cssx files to the ASP.NET page handler

    <system.web>
      <httpHandlers>
        <add verb="GET" path="*.cssx" type="System.Web.UI.PageHandlerFactory" />
      </httpHandlers>
    </system.web>

    Code 2: Making the page built using the build provider

    <system.web>
      <compilation batch="false" debug="true">
        <buildProviders>
          <add extension=".cssx" type="System.Web.Compilation.PageBuildProvider" />
        </buildProviders>
      </compilation>
    </system.web>

    Then it came to adjusting our CSSX files to the new situation and make them benefit of the SharePoint webcontrols. On the top of the .cssx file we’ve added:

    <%@ Page Language="C#"%>
    <%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    Registering the PublishingWebControls tag has granted us access to the $SPUrl prefix which helps creating the Site Collection URLs using ~SiteCollection.

    It all worked out pretty well… in Microsoft Internet Explorer. Mozilla Firefox seemed to have problems with parsing the .cssx file. With the FireBug extension I’ve figured out that it doesn’t recognize the .cssx files as CSS. I’ve fixed it by adding ContentType="text/css" to the Page directive:

    <%@ Page Language="C#" ContentType="text/css"%>

    Now we could use the solution and refer to all image resources like:

    background-image: url("<asp:Literal runat='server' Text='<%$SPUrl:~SiteCollection/SiteCollectionImages/someimage.jpg%>'/>");

    SharePoint would take care for the proper URL and the literal would render it in the .cssx file.

    Knowing this approach will require adding some information in the web.config file, we’ve created a separate feature with a custom featurereceiver that would do this task. Later on we’ve found out that we could use the same feature for adding incorporating some more elements like URL rewriting and captchas. 

    To make the feature put these code snippets in the right place we have use the SharePoint Add method of the WebConfigModifications collection. Before we did it we also had to define the XML to be added as an instance of the SPWebConfigModification class.

    Code 3: Processing the modifications by the feature

    SPWebConfigModification modification = new SPWebConfigModification("add[@path='*.cssx']","configuration/system.web/httpHandlers");
    modification.Owner = "FeatureName";
    modification.Sequence = 1;
    modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    modification.Value = "<add verb='GET' path='*.cssx' type='System.Web.UI.PageHandlerFactory'/>";
    someWebApplication.WebConfigModifications.Add(modification);

    modification = new SPWebConfigModification("buildProviders","configuration/system.web/compilation");
    modification.Owner = "FeatureName";
    modification.Sequence = 2;
    modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    modification.Value = "<buildProviders><add extension='.cssx' type='System.Web.Compilation.PageBuildProvider'/></buildProviders>";

    someWebApplication.WebConfigModifications.Add(modification);

    To process the changes we used the ApplyWebConfigModifications():

    SPFarm.Local.Services.GetValue<SPWebService>(someWebApplication.Parent.Id).ApplyWebConfigModifications();
  • Imtech C# String Converter (Free .NET Tool)

    Once in a while you need to paste one of these long strings into your C# code. I've set recently a record by pasting an exported SharePoint 2007 RSS Viewer Web Part: more than 400 lines! If it's only once during the solution development it's doable: you could get it well formatted using some Search and Replace with Regular Expressions. But what if you have to paste a dozen of such strings in your code?

    That's when I thought that a tool would make life easier: Imtech C# String Converter. It's all for free so don't hesitate and try it out.

    Download: Imtech C# String Converter v1.0.0.0.

  • Dynamic CAML Query

    In his recent post Robert J Wheeler has presented his way of dealing with building CAML queries dynamically. Here at Imtech ICT Business Solutions we use a slightly different approach.

    // initiate query
    string whereQry = "<Where>{0}</Where>";

    for
    (int i = 0; i < qryParams.Count; i++)
    {
           // exception for last query parameter: don't add another <And>
           if (i < qryParams.Count - 1)
                  whereQry = String.Format(whereQry, "<And>" + qryParams[ i ] + "{0}</And>");
           else
                  whereQry = String.Format(whereQry, qryParams[ i ]);
    }

    // remove the last connector spacer
    whereQry = String.Format(whereQry, String.Empty);

    qry.Query = "<OrderBy><FieldRef Name='PublishingStartDate' Ascending='False' /></OrderBy>" + whereQry;

    The first part is quite the same: we use an ArrayList to store the separate ‘And’ statements. Where the difference is, is the way we put them together. We use the String.Format method to link the separate statement to each other what keeps IMHO the code a bit more readable.

    Eventually it's not about whose code is better. It's nice to see that other developers share our point of view on the lack of CAML support and trying to develop our own ways of dealing with it.

  • Displaying SharePoint Fields in different order than edit fields

    Some of our customers like to have the information displayed in a different way than the order of the fields within the Content Types: there is frankly a difference of the information meaning for their customers (visitors) and the Information Workers within the organization. As SharePoint 2007 ships with the in-place edit possibility it’s quite obvious that this is the recommended way to use for managing the data – especially when it comes to Information Workers who not necessarily have to have technical background. Implementing the required fields in the Page Layout seems to be the solution for this problem: both IW and visitors have the idea of the page will look after publishing.

    But as I mentioned there seems to be a difference in information’s interpretation for both the visitors and IW: how would you incorporate the required information ordered differently depending on the role of the user? The easiest solution would be to send the IW back to the blue edit screens of SharePoint 2007 and make that their working interface.

    I’ve also found out that you will face the same challenge while dealing with a creative chrome which doesn’t really allows or doesn’t have space for in-place editing.

    While dealing with this problem I came up with a solution: how about setting the ControlMode="Display" attribute for all the display (visitors) fields and incorporating the edit (IW) fields in the edit panel so they are visible only in the edit mode? A typical display field would look then like this:

    <SharePointWebControls:TextField FieldName=”SomeField” ControlMode=”Display” runat=”server” />

    It worked fine until it came to working with required fields: the ControlMode attribute changes only the visual piece of a field: if it’s required it will still need some input in order to be valid. After a little search I came up with another solution.

    In one of the Page Layouts shipped with SharePoint 2007 I’ve found the SharePointWebControls:FieldValue webcontrol. After a little bit of testing it seems to suit the customer needs and solve my problem.

    Each time you will want to make use of alternate fields order you can use the SharePointWebControls:FieldValue webcontrol instead of making a custom one.

  • Imtech Fields Explorer

    Imtech Fields Explorer is a tool that can help you during the development process while working with fields. The tool allows you to explore properties of the fields contained within the (Site Collection) Content Types and Lists.

    To explore the existing fields simply input the URL of your Site (Collection) and press Enter (or click ‘Open’): you will then be able to choose whether you’re interested in the Fields belonging to Content Types or Lists.

    Imtech Fields Explorer

    To improve the performance of the tool I have chosen to extend the TreeNode class. The extended node contains the IsLoaded property which determines whether the child nodes have been loaded. The child nodes are being loaded after selecting a node for the first time. Like this there is no overhead of loading items you don’t need.

    Imtech Fields Explorer: Available Content Types

    For the convenience the available Content Types are grouped.

    Imtech Fields Explorer: Field's Properties

    After selecting a Field all of its properties will show in the property grid. I think the most useful are:

    • FieldValueType
    • Id
    • InternalName
    • SchemaXml
    • StaticName
    • Title
    • TypeAsString 

    I have found out that I need those properties quite often during SharePoint 2007 solutions development.

    Download Imtech Fields Explorer v1.1.0.0 (47,6KB) 

  • AddToEnvPath

    AddToEnvPath is a tiny tool which I wrote to help me add some path to the Environment Path variable. Like this you gain easy access to tools like STSADM.EXE and GACUTIL.EXE no matter the command line location.

    It works pretty simple: addtoenvpath <your path> eg. Addtoenvpath C:\MyFolder As I use the tool most of the times for setting the STSADM.EXE path, it’s been hard coded in the tool. The tool will add it (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\) each time you run addtoenvpath without any parameters.

    Download AddToEnvPath (2,16KB)
  • TechEd Developers 2007 Europe Vista gadget

    I searched the web for a countdown Vista gadget for the TechEd 2007 in Barcelona but could not find one. So I made one myself. I am not too sure if we could go there but now I can see in about how many days I am going... or not...

     

    See attachment below to download the gadget in .zip format or download it here.

    Posted Aug 23 2007, 10:56 AM by tmt with 3 comment(s)
    Filed under:
  • About John Bruin

    Hello my name is John Bruin and I am 37 years old so I am the oldest and supposed to be the wisest of the three of us... I am working as Senior Software Engineer for Imtech ICT Business Solutions in The Netherlands. As a Microsoft .NET Certified Solution Developer I am building solutions in ASP.NET and SharePoint 2007. I have special interest in new technology and rich user experiences and therefore I like Silverlight, WPF and Vista Sidebar gadgets very much! My best achievement this year was winning the Rock My Website (sorry it is only in Dutch) contest together with Waldek and Martijn with a higly accessible and appealing website design for Stichting Drempelvrij.nl

    My other personal interests are: International 10x10 draughts, PlayStation Portable and other gadgets, listening to music, watching movies and not to forget the good old Commodore 64 which I have grown up with.

    I believe that it is very important to share my knowledge with colleagues and now in a bigger perspective with the whole SharePoint community. Learn and enjoy!

  • About Waldek Mastykarz

    Ladies and gentleman,

    My name is Waldek Mastykarz and I am a software engineer with Imtech ICT Business Solutions from the Netherlands. Together with Erik and John I am responsible for delivering SharePoint solutions for our customers. I am specialized in Web Content Management solutions based on Microsoft Office SharePoint Server 2007. I am also responsible for the user experience and implementing user interfaces in web based solutions delivered by Imtech ICT Business Solutions.

    Experience

    I have been working on web based solutions for quite some time now (I still remember using HTML 3.2 and making frames based sites). A few years ago I have started embedding in projects dynamic elements using PHP. As my programming skills evolved I have started using OOP in PHP. September 2006 when I came in touch with Imtech ICT I have started working with the .NET environment and working with Erik and John on my first SharePoint projects. Since then I have worked on delivering a few SharePoint 2007 – mainly Web Content Management centered – I hope they will be able on-line soon so I will be able to present them to you.

    This blog

    A while ago we talked about beginning a weblog and starting sharing our knowledge with other people. As we recently begun working on more projects and using more of other peoples knowledge I have started realizing myself that we should do something back… sharing our knowledge and tools for example.

    I hope you will enjoy reading this blog and won’t hesitate giving us your feedback and sharing your thoughts on the subject we will be presenting here. As we have decided we will try to post the content on regular basis. We will also share some tools we use regularly while working on SharePoint 2007 solutions.

    I would also like you to know that all of the opinions expressed on this blog are personal, and not necessarily shared by my employer.

    Enjoy!


Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts