Wednesday, June 27, 2007

Adding table headers and/or footers to a Content Query Web Part Layout

Just found this article by Mike Gehard which shows a good step by step of making a content query web part look like a grid - with column headers and all.

Mike is even using the same ddwrt trick as I am for formatting dates, and I now see that his blog seems to be dedicated to the content query web part!

I recommend you go to Mike's blog and read all about it. The method he is taking is chaning the "ContentQueryMain.xsl" file to pass the lastrow as a parameter to the "CallItemTemplate", and then in "CallItemTemplate" he passes the lastrow to the apply templates itemstyle.

In the "Itemstyle.xsl" file you add html variables for the header and the footer, and use them before the first row and after the last row.

I will try to contact Mike and ask for permission to reprint his article here for redundancy.

Mike - if you are reading this, how about you join the Enhanced Content Query Web Part project?

Tuesday, June 26, 2007

Using DDWRT in xslt-based web parts

What is DDWRT?
well, its a script that microsoft packaged for it's xslt dataviews, that gives them more xslt power.
I needed to use the ddwrt functions in my content query web part, but I guess that the following approach will work in the search web parts as well.

Why do we need it?
I needed it to format the date I was getting back from the content query. The format I was getting back was ugly to the user (2007-06-27 15:52:00) and I wanted to format it, but I didn't want to write my own function.

So how to use it?
you need to add to your xslt the following namespace where the namespaces are declared (at the top of the xsl file, in the "xsl:stylesheet" tag):

Then you can use the ddwrt's functions. For example, to format the date I used the following:
<xsl:value-of disable-output-escaping="no" select="ddwrt:FormatDate(string(@Modified), 3081, 5)" />
Note that in the FormatDate function I used 2 hard coded values - 3081 which is the LCID for Australia (so the date will be formatted to Australian date format) and 5 which specifies the what do I want to display - date, time, date and time ect. I have no idea what values give what, but I do know that 5 gives me the date and the time.

Update to ItemAdding fiasco

This is to let you know that I updated my posts about the event handlers and ItemAdding, after I did some more research and found the correct way to get and set item's properties during ItemAdding. I swear this method did not work during Beta2 when I posted my old posts, and I swear that contacts in Microsoft did tell me it was by design and not possible (I have the emails to show that).
However, it turned out that it is possible, and easy, and I posted an update and a code sample in the old posts:
Bad news - synchronous list events bug (or missing feature)
Synchronous Add List event (ItemAdding) will not give access to item properties

Thursday, June 21, 2007

Great MSDN developer article - Development Tools and Techniques for Working with Code

There is a new article in MSDN which enraptured me. It is a magnificent summary of what development in sharepoint is all about, with example on how to do sample tasks.
It is in two parts, and is from my fellow MVP Patrick Tisseghem, (from U2U, Patrick is one of the authors of my favorite sharepoint developer utility - the CAML Query Builder) Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0 (Part 1 of 2)
Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0 (Part 2 of 2)

Call for beta testers! Enhanced Content Query Web Part

This is to call beta testers. I want to kick the ECQWP project to beta2, which requires beta testers to start testing and reporting on Beta 1 (well, 1.3 actually) .
You don't need to write to me or anything - just get a free codeplex account, and use it in the ECQWP project site to add issues (bugs) to be fixed, or ask for enhancements.
Please try to be VERY clear on the problem - preferably with a step by step of what I need to do to get the error you are describing.

While I am at it, if you are a good SharePoint developer and you think you have the ability to solve the bugs, let me know, and I will add you to the project as a developer, and assign bugs for you to fix.
Please note - this is not a practice run. I do not have time to mentor beginners, so don't use this as a free training exercise. Sorry.

Wednesday, June 20, 2007

Adding custom fields to the Enhanced Content Quey Web Part (Beta 1.2)

I just got around to adding another important feature to the Enhanced Content Query Web Part and I uploaded it to codeplex.

I am sure you read my articles, or articles by other MVPS like Heather about showing custom columns in the content query webpart. This requires (as Heather's article shows) that you export the web part to a file, manually modify the file to add the fields you want, and import it back.

This annoyed me enough to add a property to my ECQWP that will allow you to add those fields without having to export-import.

In this version (1.2) you have two new properties in the propeties pane - Data Fields and Common View Fields. I have yet to find out how Data Fields are used, so you don't need to use that property - it is enough that you set the Common View Fields.

I will refer you to Heather's article to find out the format that the value of this property should be written in - how to get the field's internal name, and how to get the field's type. Just a quick reminder though - the value in that field is a pipe seperated value of fields, where each field is a coma seperated pair of field (internal) name and field type.
For example, a text field with internal name "Comments" will be added like so:


A choice field with internal name "Client_x0020_Name" will be added like so:


and if we want to add both of the above fields, it will look like so:


As always, get the web part, installation instructions and source code from codeplex. Just be sure to download the most recent version.

Tuesday, June 19, 2007

Enhanced Content Query Web Part upgraded to Beta 1.1

I am happy to announce that the Enhanced Content Query Web Part project has progressed to Beta 1.1, with some very important fixes to major bugs.

The web part now correctly prints out the item ID, has context menus for folders (not just documents) and supports displaying icons for documents.

Be sure to read the documentation (installation instructions) and use the new XSLT that is provided there. The old XSLT will not solve any of the bugs I fixed.

Community Kit for SharePoint 2.0 Pre-Release

You can read about it in the SharePoint team blog - there are some pre-release components from the Community Kit that are ready for download.
The downloads are:
Enhanced Blog Edition Beta 1
Enhanced Wiki Edition Alpha
ChatterBox AJAX Beta
Tag Cloud

Wednesday, June 13, 2007

Using Javascript to Manipulate a List Form Field

This is something I have been doing for a while (see my nested tasks project - it has been using this trick), and now Rob Howard from Microsoft published a tip on how to use javascript to manipulate a list form field.
The example of use I would like to give is my nested tasks project- we need to set a default value when a user creates a sub task. We have a web part sitting in the "newform.aspx" page, and checks the querystring the page was opened with. If the page was opened with a querystring of "parentID=", then it loads the item that is the parent, gets the values for start date and end date, and then uses javascript to set the "Parent Task", "Start Date" and "End Date" fields.
My javascript also hides the "Parent Task" field control from the user, so he will not edit it manually. This gives us a very user friendly interface.

The same trick can be used to implement some custom validation - for example not allowing the user to enter a start date before the start date of the parent.

Wednesday, June 06, 2007

My favorite tool just got better!

U2U have released a new version of the U2U CAML Query Builder, and this time it can be installed as a feature, and has a web interface instead of the windows form application it was so far. GREAT!
Check out the post from Karine Bosch.
If you don't know what the CAML Query Builder is, I suggest you start by downloading it (either the new release or the old one) and see how easy it is to write CAML queries with it.

Allowing InfoPath forms to redirect to pages in other site collections

In InfoPath 2007, when you publish a form as a web form, you have the option to specify several query string parameters. One of those parameters is the "Source" parameter, which is "The location to which the user will be redirected when the form is closed. The URL must be in the same site collection or an error will be returned."
The error mentioned is "The following location is not accessible, because it is in a different site collection:" and the url you specified.

So it seems that specifying a source from another site collection is impossible. But what if I have a site collection dedicated to forms? lets say "http://forms", to which I link from the intranet home page or another page, and I want that when the user closes the form, he will be redirected back to the page in the intranet (lets say "http://intranet") that we started from?

I had to come up with a solution, and a simple one jumped to mind - a redirect page!
The trick is simple. Instead of specifying a link to "http://intranet" as the source parameter (which will cause an error), specify a link to a special page in the layouts folder, and give that page the query string that links to the page you want to return to.

Confused? here is an example:
Instead of linking to the form with this link:


Link to it like this:

As you can see from the links, the first one will cause an error because we are linking to another site collection, while the second one will work, because we are linking to the current site collection.

So the only thing left for me to show you is how to create the SPSTipsFormRedirector.aspx page that will handle the redirect to the page you specified in the red section in the second link:
Open your layouts folder (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS) and create a new text file by the name SPSTipsFormRedirector.aspx. Open the file in a text editor such as notepad, and copy the following code:

string target = Page.Request.QueryString["target"];
@ Page Language="C#" Inherits="System.Web.UI.Page" %>

Repeate this in every front end server, and you are ready to write links to forms that, when closed, will go back to a page you specify in the link!

Tuesday, June 05, 2007

Enhanced Content Query Web Part goes Beta 1!

Hey Everyone!
This is to let you know that I released beta 1 of the Enhanced Content Query Web Part Project. Make sure you read the installation documentation to the end, because that is where you will find the sample XSLT that will cause the menus to appear.

Anyone want to volunteer to draw me a logo? Contact me!

Monday, June 04, 2007

Coming Soon! Content Query Web Part with Context Menus

Well, I promised you something that will knock your socks off, so hold on!

I have started a new open source project for an Enhanced Content Query Web Part that will have the enhancement (in the first stage) of Context Menus.

(note - the project is not yet published - I need to figure out why my laptop suddenly refuses to connect to codeplex, and I will release the beta)

Content Query Web Part for MOSS with Context Menus

Additional features that will come with this release include the ability to add the default toolbar (new, updload, actions, settings) to the web part when it is connected to a specific library or list (not in aggregation mode)

Content Query Web Part with Toolbar

Some things will not work as you'd expect (if you select a view using the view selector it will redirect you to the view, not change the current web part) but it is a very useful addition to the web part's abilities!

I intend for this project to grow to be a true replacement of the content query web part, and that is why I am calling this project "Enhanced Content Query Web Part".

I hope others who have done work around enhancing the built-in web part like Meron Friedman (Content Query Web Part with marquee) and Steven Van de Craen (extended Content Query Web Part with item limit per group *link seems to be broken?*) will join me as developers on this project, and integrate their code with mine.

I am also looking for advanced developers to help me get my current code stabilized. If you want to help - contact me.

Why isn't there a default value for lookup fields?

Oh, lookup fields, how I hate you.
I just realized that it is not possible in the user inteface to set a default value for a lookup field. Is it that hard to do an interface that will show the user creating the field the options from the looked-up list and allow him to select a default value?
Apperantly it is. I guess Microsoft were worried about what happens if someone deletes the looked-up value.

Not content with my findings, I tried looking in the object model and discovered that you can set up a default value for a lookup field using the following code:

SPList docs = w.Lists["Documents"];
SPFieldLookup fl = (SPFieldLookup)docs.Fields["City"];
fl.DefaultValue = "1;#Mexico City";

I will add this option to my Utility Pack when possible.

Using the above code solves the issue almost totaly. When you use the web interface to add a list item or upload a document, the default value comes up in the lookup box. However (and this is the big one), from the office applications (both 2003 and 2007) the default value is ignored and the lookup box always begins at the "blank" option.

Should I waste one of my MSDN support calls on this and ask MS for a fix? or should I just learn to live with it?