February 2008 - Posts

Did you ever had the need to navigate to a SharePoint list item or document, when you only got the ID's of the item, the item's container (list or document library) and the item's web? I sure did! For example when you query SharePoint data by making use of the SPSiteDataQuery class; the resulting data table includes all those ID's (and additional properties if you want), but it doesn't include a link back to the item. You could make use of the Object Model to build the link in code, but that is both resource intensive and pretty complex. In that case you have to check out the CopyUtil.aspx page, which is also used by the Content Query Web Part by the way. The CopyUtil.aspx page is an application page to which you can provide a bunch of ID's, as a result the page will redirect you to the corresponding item or document.

You just have to build a URL like this (replace the X's with the actual ID's of course):

http://yoursite/_layouts/CopyUtil.aspx?Use=id&Action=dispform&ItemId=X&ListId=X&WebId=X&SiteId=X

And the CopyUtil.aspx page will do the rest!

I couldn't find any documentation for this page, but with a little Reflect-ering, I found out that the Action parameter can be either dispform or editform (redirecting either to the view item URL or edit item URL).

During the last year I've been developing custom solutions for SharePoint 2007 for various customers and I've been evangelizing my approach for writing code which is easy to deploy and maintainable on your servers. Of course I used the best practices for building SharePoint solutions: making customizations available with Features and packaging those customizations and components in Solutions (WSP's). Building SharePoint Features comes down to writing XML and the only tool Microsoft gave us developers was Notepad; of course I'm exaggerating a little bit: you can use Visual Studio as well, but still you have to write XML. Packaging SharePoint customizations into Solutions is even worse: a WSP file is actually a CAB file that you have to create by making use of the famous tool MakeCab. You need to feed the MakeCab tool a DDF file, which is a plain text file (not XML) describing the contents of the CAB. Besides the customizations themselves, you also need to have a Solution manifest, again a XML file, that tells SharePoint how everything should get deployed. Bottom line: for developer it's quite hard to implement these best practices.

Microsoft acknowledges the issues that SharePoint developers are facing, they created the Visual Studio extensions for Windows SharePoint Services (VSeWSS). This is an extension on top of Visual Studio that adds some project and item templates to the development environment. These templates take care of building the feature XML files, the DDF for MakeCab and even the Solution manifest. Version 1.0 of those extensions were only good to use in demos and proof-of-concepts in my opinion: it was not possible to tweak the generated XML files or influence the creation of the Solution (e.g. web part DLL's were always deployed to the GAC). Recently version VSeWSS 1.1 has been released with some very interesting new functionality; but still, in some scenarios want to have more flexibility and freedom. And maybe even more important, sometimes you want to use the shiny new Visual Studio 2008! (currently VSeWSS 1.1 only works with Visual Studio 2005)

Let me introduce you to the SharePoint SmartTemplates for Visual Studio! I've created (and I'm still creating) some templates for Visual Studio 2008 that allow me to create new Visual Studio projects for the typical SharePoint development tasks, that are configured like I want them to be configured. Typically my project looks like this:

  • Solution
    • Class Library Project
      • *.cs files
      • WSP folder
        • 12 folder
          • Template/features folder
        • batch files to create the WSP
      • Setup folder (contains installer)

I prefer to use the excellent WSPBuilder tool to quickly build the WSP file, and I use the SharePoint Solution installer to build a setup wizard for easy deployment. For example by using the SmartTemplates you can create a new Visual Studio project based on the web part template, it creates the folder structure, generates the feature XML files, generates an empty web part and configures the installer. So everything is in place to get started writing that code instead of worrying about how to package your code and get it deployed. If you want to see the web part template in action check this small screencast.

I've put the first version of the SmartTemplates on CodePlex in December and (without actually telling anybody) they have been download almost 1500 times. This week I created a new version which includes a second template for building a web part using the SmartPart. So you can now build a web part by making use of the user control technique, package it into real WSP, and get it deployed by making use of a setup wizard!

I the near future I'm going to add new templates for an empty Feature (with event receiver), Application page, event handler etc. If you feel like contributing to the SmartTemplates: drop me a line! It would be great if somebody could build a VB.NET version, and/or a Visual Studio 2005 version.

I've seen the question if it is possible to enable Audience Targetting on a SharePoint document library or list trough code, but I've never found an answer to it. But this weekend Ryan Ramcharan posted a solution in one of the SharePoint Forums posts. It looks like you can enable Audience Targeting programatically by adding the Target Audiences field as XML, here is a small code snippet:

using (SPSite site = new SPSite(http://mysite/))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["Shared Documents"];

        XmlElement fldElement = new XmlDocument().CreateElement("Field");
        fldElement.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d");
        fldElement.SetAttribute("Type", "TargetTo");
        fldElement.SetAttribute("Name", "TargetTo");
        fldElement.SetAttribute("DisplayName", "Target Audiences");
        fldElement.SetAttribute("Required", "FALSE");

        list.Fields.AddFieldAsXml(fldElement.OuterXml);
        list.Update();                  
    }
}

The strange thing is that this seems to only way to add the field, you can't get a hold of the Target Audiences field through the Object Model. Thanks Ryan for posting this solution! And if you know a nicer way to accomplish this, feel free to drop a comment. :-)

Yesterday evening I've uploaded a new version of the SmartPart to the CodePlex site. For those of you who don't know the SmartPart: it's a generic web part wrapper for SharePoint 2007 which can host any ASP.NET Web User Controls. So basically you can build web parts by using the Visual Studio designers instead of having to write all the code by hand. The new version includes a setup wizard for easy installation, localization support for ASP.NET AJAX controls and some minor bug fixes. It's not a beta version anymore and there is a 64 bit version available as well. On top of that I've included a bunch of sample controls to get you started (both normal controls and ASP.NET AJAX controls). There is even a short (only 2 minutes) screencast that shows you how easy it is to install the Return of the SmartPart v1.3 by using the new wizard. Get the 1.3 release from here: http://www.codeplex.com/smartpart/Release/ProjectReleases.aspx?ReleaseId=10697

In the next days/week I'll be uploading more samples and templates to the CodePlex site.

[Via the SharePoint Team Blog] The long awaited version 1.1 of the Visual Studio Extensions for Windows SharePoint Services has RTM-ed, you can download it here. There is still no support for Visual Studio 2008 (but it will come in June 2008), but there are some interesting new things:

What's New in VSeWSS 1.1?

  • "WSP View", aka "Solution Package editing"
    • No more hidden, non-editable solution content!
    • Create new Features
    • Reorder Elements between Features
    • Conflict validation against existing Features
    • Rename existing Features
    • Change Feature activation order
  • Visual Basic support!
  • New Item Templates:
    • "List Instance" project item
    • "List Event Handler" project item
    • “SharePoint Template” item template, e.g. "layouts" files
  • Faster F5 speed (The tool no longer performs an IISReset. Now it only recycles the app pool.)
  • Solution Generator: you can now choose which lists should be included when exporting a site.
  • Many bug fixes, e.g.
    • No more GUIDs in Feature Names
    • Support complex project names, e.g. with periods.
    • Can deploy assemblies to the bin folder, instead of GAC
More Posts