25 April 2012

ILSpy Version 2.0


Today I found out that ILSpy 2.0 was released a few days ago!
For those of you that don't know ILSpy, it is the open-source .NET assembly browser and decompiler!

Release notes:
  • Assembly Lists
  • Support for decompiling Expression trees
  • Support for lifted operatores on nullables
  • Decompile to Visual Basic
  • Search for multiple strings separated by space (searching for "Assembly manager" in ILSpy.exe would find AssemblyListManager)
  • Clicking on a local variable will highlight all other occurrences of that variable
  • Ctrl+F can be used to search within the decompiled code view
See http://www.ilspy.net/ for more info or download ILSpy.

30 March 2010

Using LINQ to get filenames from a directory structure using multiple include and exclude patterns

This example shows how to get list of file names from a directory (including subdirectories) using multiple include- and exclude patterns.

The buildin method System.IO.Directory.GetFiles can handle only one include pattern and no exclude pattern and multiple patterns a definitely out of the question.

So I had to write a little helper method using LINQ to do the trick!

/// <summary>
/// Get the files from the specified path, including subdirectories, which match the specified pattern.
/// Note: To specified multiple patterns, separate them by a semicolumn (;)
/// </summary>
/// <param name="path">The path.</param>
/// <param name="includePatterns">An array of include patterns. Using wildcards.</param>
/// <param name="excludePatterns">An array of exclude patterns. Using Regular expressions.</param>
/// <returns>Returns an array of filenames.</returns>
public static string[] GetFiles(string path, string[] includePatterns, string[] excludePatterns)
{
 if (string.IsNullOrEmpty(path))
 {
  throw new ArgumentNullException("path", "Argument should not be NULL or an empty string.");
 }
 if (includePatterns == null)
 {
  includePatterns = new string[0];
 }
 if (excludePatterns == null)
 {
  excludePatterns = new string[0];
 }

 var files = from includePattern in includePatterns
    from includeFile in Directory.GetFiles(path, includePattern, SearchOption.AllDirectories)
    from excludePattern in excludePatterns.DefaultIfEmpty()
    where excludePattern == null || !Regex.IsMatch(includeFile, excludePattern, RegexOptions.IgnoreCase)
    select includeFile;

 return files.ToArray();
}

The following example gets all files that match the wildcard patterns 'DdH.*.Helpers.dll' & 'DdH.*.exe' from the c:\temp directory, but will exclude all files that match the regular expression 'Tests\.dll'.

string[] fileNames = GetFiles(@"c:\temp", new string[] { "DdH.Helpers.*.dll", "DdH.*.exe" }, new string[] { "Tests\.dll" });

07 February 2010

Agile Poker Cards For Windows Mobile

I just release my first Windows Mobile Application on CodePlex. :-)

It’s use is to display poker cards during a planning session.

During a scrum or other agile processes, you have to estimate the size of a user story during a planning session.
With this little program there is no need for using real cards anymore!

Tools I used to develop this little application:

The program works really simple...
Just press a button and it will display the symbol in full screen. (nothing more, nothing less)

AGP1 AGP2 AGP3

To download or read more, go to the projectsite: Agile Poker Cards

19 October 2009

What’s new with SharePoint Server 2010 Web Content Management

I’ve been waiting for this information like this since Microsoft announced a new version of SharePoint!

There are a lot of blog posts out on the web which are about new features in SharePoint Server 2010, but only a small few contain some information about SharePoint Web Content Management (WCM). Now Andrew Connell wrote three very interesting blog posts only about this subject. :-)

Part 1 – Improvements to the Core SharePoint platform

  • Cleaner Markup
  • Server Ribbon
  • Reduced Postbacks during the authoring process

Part 2 - Improvements to WCM

  • Improved Content Query WebPart
  • Authoring
  • Content Deployment
  • Large Page Libraries
  • Publishing Improvements

Part 3 – What’s new in WCM

  • Content Organiser
  • Page Ratings
  • Metadata Everywhere
  • Web Analytics
  • More Video and Rich Media Support

For more information read the posts of Andrew Connell

29 August 2009

Programmatically Adding a Control Adapter

I have a Control Adapter I use frequently and every time I start a new SharePoint WCM project, I have to rethink about how to use  it to my Web Application and how to deploy the compat.browser modifications. After some research on the net I came up with two solutions to add Control Adapters programmatically, so no browser.compat modifications are necessary any more.

  1. Add a Control Adapter to the current HttpContext.
  2. Add a Control Adapter to a particular Control.

I created some helper functions that can do the binding of the Control Adapter to a Control or Control-Type. In both cases the functions are used from within a custom (master)page class.

The first solution is using strate forward .NET code, but for the second I had to use some Reflection. (so beware!)

1. Adding a Control Adapter to the current HttpContext

You can use the Global.asax file or a masterpage to add Control Adapters to the current HttpContext.

In the global.asax implement the code in the Application_Start event to bind the Control Adapter.

<script runat="server">
    void Application_Start(Object sender, EventArgs e)
    {
        AddControlAdapterToType<Unive.Foundation.UI.ControlAdapters.SharePointWebPartZoneControlAdapter>(typeof(Microsoft.SharePoint.WebPartPages.WebPartZone));
    }

    private static void AddControlAdapterToType<T>(Type controlType) where T : ControlAdapter, new()
    {
        if (controlType == null)
        {
            throw new ArgumentNullException("controlType", "This argument can not be null!");
        }

        IDictionary adapters = HttpContext.Current.Request.Browser.Adapters;
        string key = controlType.AssemblyQualifiedName;
        if (!adapters.Contains(key))
        {
            string adapter = typeof(T).AssemblyQualifiedName;
            adapters.Add(key, adapter);
        }
    }
</script>

In a masterpage you must implement code in the constructor to bind the Control Adapter. This is necessary so the page will use the Control Adapter in the first run. Doing the binding in the OnInit or later is to late and all the child controls in the markup will already be instantiated.

using System;
using System.Web;
using System.Web.UI.Adapters;

public class MyMasterPage : System.Web.UI.MasterPage
{
    public MyMasterPage()
    {
        AddControlAdapterToType<SharePointWebPartZoneControlAdapter>(typeof(Microsoft.SharePoint.WebPartPages.WebPartZone));
    }

    private static void AddControlAdapterToType<T>(Type controlType) where T : ControlAdapter, new()
    {
        if (controlType == null)
        {
            throw new ArgumentNullException("controlType", "This argument can not be null!");
        }

        IDictionary adapters = HttpContext.Current.Request.Browser.Adapters;
        string key = controlType.AssemblyQualifiedName;
        if (!adapters.Contains(key))
        {
            string adapter = typeof(T).AssemblyQualifiedName;
            adapters.Add(key, adapter);
        }
    }
}

2. Add a Control Adapter to a particular Control

To bind a Control Adapter to a particular control, you must first find the control instance in the page and then you can do the binding.

using System;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.Adapters;

public class MyMasterPage : System.Web.UI.MasterPage
{
    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        Control control = Page.FindControl("WebPartZone3");
        AddControlAdapterToControl<SharePointWebPartZoneControlAdapter>(control);
    }

    private static void AddControlAdapterToControl<T>(Control control) where T : ControlAdapter, new()
    {
        if (control == null)
        {
            throw new ArgumentNullException("control", "This argument can not be null!");
        }

        T adapter = new T();

        // Using reflection to bind controladapter to control
        control.GetType().GetField("_adapter", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(control, adapter);
        // Using reflection to bind control to controladapter
        adapter.GetType().GetField("_control", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(adapter, control);
    }
}

24 June 2009

Bye Bye GuidGen.exe!

While developing in Visual Studio and I needed to create a new guid, I always used the “Create GUID” under the Tools dropdown menu. This menuitem started the “guidgen.exe” application and using this was a fine method, until I attended the last DevDays 2009 (The Hague, Netherlands). There I saw a demonstration of a much easier way to create a new guid in Visual Studio by making use of a simple macro. (Thanks Wouter)

Below is the explanation on how to implement this macro.
1. Open the Macro Explorer and Load or Create a Macro Project.

NewMacroProject

2. Inside the macro IDE, create a method to generate a new guid.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Module1
    Sub GenerateGuid()
        If (DTE.ActiveDocument IsNot Nothing) Then
            DTE.ActiveDocument.Selection.Text = Guid.NewGuid().ToString("B")
        End If
    End Sub
End Module
3. Save and close the macro IDE.
4. For the finishing though, create a keyboard shortcut to activate the GeneratGuid macro. I like to use the combination CTRL+K, CTRL+G

ShortcutGenerateGuid

5. Now it is so easy to create a new guid…..just by pressing your favourite keyboard shortcut.

CreateGuid

CreateGuid2

Bye Bye GuidGen!

27 May 2009

DevDays 2009 (PreConference DeepDive)

Today I visited the first day of the DevDays 2009 (PreConference DeepDive) in The Hague (Netherlands) and attended a session about SharePoint developing by Wouter van Vugt.

After the introduction and agenda, Wouter started with presenting some develop tools you can you use while developing and talked about the pro's and con's of every one of them.

Next was creating custom list templates and how to build your own entity picker controls using classes that are already provided inside the SharePoint object model.

After the lunchbreak he continued with a demo of how he created a complex fieldtype using a entity picker and a resultdialog. How he stored the selected value, which contained some technical data and how to split it in a displaytext and id before displaying in a list. Wouter demonstrated how he used the RenderPattern of the fieldtype to only display the displaytext and hide to technical stuf.

Last part of the session was about how he created his own SharePoint WCM Publishing site that is W3C Complaint. I found this a great demo and he gave me a lot of new idea's to work on.

Overall I had a very interesting day full of How To's, demo's and lots of code examples.

15 May 2009

The SharePoint Developer Introduction for .NET Developers

Today I found that Microsoft release a SharePoint Introduction website for .NET develpers.The site is created with SilverLight and contains lots of information on what developer can use when they start with SharePoint.

  • Background info
  • Webcasts
  • Screencasts
  • Demo's
  • How to's
  • Virtual PC's image
  • Hands on Labs
  • Links to resources and forums
  • ...and add some

Check it out on: Do Less. Get More. Develop on SharePoint.

11 May 2009

How to detect when a SharePoint Publishing Page is detached from it's PageLayout

Recently I found a new thing about SharePoint Publishing pages. From within SharePoint Designer, publishing pages can be detached from there pagelayout!

I guess that for some of you this not a a new thing, but I didn't know.

In a normal publishing senario this is also not a common thing to do, because the page is disconnected from the pagelayout and changes to the pagelayout will no longer affect the page. But in certain situations it can be usefull, like using SharePoint Designer to place webparts on a publishing page. Normaly this can only be done from the browser interface. Of course a page can also be attached to a pagelayout again.

To detach a page in SharePoint designer, open the Pages list and right click on the page.


Now you get a dialog 'Detaching from the page layouts http:/xxxx/_catalogs/masterpage/xxxx.aspx will copy its markup into this page, where it can be customized. Changes to the pagelayout will no long affect this page.'

To reattach a page to it's pagelayout, right click on the page again.


After knowing how to detach a page from it's pagelayout, I wanted to find out how a publishing page would know it was detached and after some investigation I found that this information is stored in the listitem for the page. Each publishing page has a field called 'PublishingPageLayout' and this field normaly contains the url of the pagelayout and the name of the content type.
Value format: 'http://xxxx/catalogs/masterpage/xxxx.aspx, ContentTypeName'

But after detaching the pagelayout this field contains another kind of information. Namely an indication that the page is a disconnected publishing page and a reference to the pagelayout is once was connected to.
Value format: 'http://www.microsoft.com/publishing?DisconnectedPublishingPage=true, http://xxxx/_catalogs/masterpage/xxxx.aspx

To detect all disconnected publishing pages in a site collection I created a the following code:
using (SPSite site = new SPSite("http://demosite"))
{
    SPWeb rootWeb = site.RootWeb;

    // Query to get all publishing pages in the sitecollection that are detached from pagelayout.
    // ServerTemplate=850, is template for the Pages lists.
    SPSiteDataQuery dataQuery = new SPSiteDataQuery();
    dataQuery.Webs = "< Webs Scope="SiteCollection">";
    dataQuery.Lists = "< Lists ServerTemplate="850">";
    dataQuery.ViewFields = "< FieldRef Name="FileRef" Nullable="TRUE">";
    dataQuery.Query = "< Where>" +
                            "< Contains>" +
                                "< FieldRef Name="PublishingPageLayout">" +
                                "< Value Type="Text">?DisconnectedPublishingPage=true< /Value>" +
                            "< /Contains>" +
                        "< /Where>";                              

    // Store result in datatable
    DataTable dt = rootWeb.GetSiteData(dataQuery);
    foreach (DataRow row in dt.Rows)
    {
        if (row.IsNull("FileRef"))
        {
            Console.WriteLine("FileRef should not be null!");
            continue;
        }

        // Strip listitem id from fieldref value
        string fieldRef = row["FileRef"].ToString().Trim();
        if (!string.IsNullOrEmpty(fieldRef))
        {
            int pos = fieldRef.IndexOf(";#");
            if (pos > -1)
            {
                fieldRef = fieldRef.Substring(pos + 2);
            }
        }

        Console.WriteLine(fieldRef);
    }
}          
Also you can use the 'Content and Structure Reports' functionality from MOSS to get an overview of all publishing pages that are disconnected from the pagelayout.

To do this, go to the 'Content and Structure Reports' list inside to root site. Create a new item and fill the field like below.
  • Report Title: All Pages Disconnected from PageLayout
  • Resource Id: (leave blank)
  • Resource Id: (leave blank)
  • CAML List Type: <Lists ServerTemplate='850' />
  • CAML Query: <Where><Contains><FieldRef Name='PublishingPageLayout' /><Value Type='Text'>?DisconnectedPublishingPage=true</Value></Contains></Where>
  • Target Audiences: (leave blank)
  • Report Description: All publishing pages that are detached from their pagelayout by using SharePoint Designer.



Afer creating a new report, you can access it through the Site Actions button.

26 April 2009

SPSiteColumnUsage, Find all references to a site column

When I try to delete a site column from a SharePoint site I often get the message "Site columns which are included in content types cannot be deleted. Remove all references to this site column prior to deleting it."

Well with only a few content types this isn't so hard to click through all content types and checking if the site column is used, but with a lot of them it's no fun and can be a hell of a job.

To make my life a bit easier I decided to create a piece of code that could delete site columns programmatically. I knew that SharePoint object model has a class that can return the usage of content types, namely SPContentTypeUsage. With this class you can tell if a content type is used in one or more lists somewhere inside the complete site collection and what the urls to those lists are.

My guess was that there should also be something like a site column usage, but I was wrong!

After some investigation on MSDN, several SharePoint blogs and good old Reflector, I created a SPSiteColumnUsage class. This class uses the LINQ and SPContentTypeUsage and can find all references to content types and lists where a site columns is in use.

SPSiteColumnUsage
Methods
  • GetUsages, this method takes a SPField instance as input argument and returns an array of SPSiteColumnUsage objects.
Properties
  • Id, the Id of the site column.
  • Scope, the scope of the site column.
  • ContentTypeId, the id of a content type where the site column is in use.
  • IsUrlToList, indicates if the information is about a list or a content type.
  • Url, the server relative to a list where the site column is in use.


Usage example to get info about the site column: Title
using (SPSite site = new SPSite("http://demosite"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPField field = web.Fields.GetFieldByInternalName("Title);

        Console.WriteLine("Field: {0}", fieldName);
        Console.WriteLine("==============================");

        var usage = SPSiteColumnUsage.GetUsages(field);
        foreach (var u in usage)
        {
            Console.WriteLine("Id: {0}", u.Id);
            Console.WriteLine("Scope: {0}", u.Scope);
            Console.WriteLine("ContentTypeId: {0}", u.ContentTypeId);
            Console.WriteLine("IsUrlToList: {0}", u.IsUrlToList);
            Console.WriteLine("Url: {0}", u.Url);
            Console.WriteLine();
        }
    }
}
See a working example on http://SPSiteColumnUsage.codeplex.com

The class also makes use of the extension method CoppyToArray I created.
For this method see a previous post: Using LINQ to query SharePoint collections

/// 
/// Class with site column usage information.
/// 
public class SPSiteColumnUsage
{
   public static IList< SPSiteColumnUsage> GetUsages(SPField field)
   {
       List< SPSiteColumnUsage> list = new List< SPSiteColumnUsage>();

       if(field != null && field.UsedInWebContentTypes)
       {
           // Use reflection to get the fields collection for the specified field
           SPFieldCollection fieldCollection = field.GetType().GetProperty("Fields", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(field, null) as SPFieldCollection;
           if(fieldCollection != null)
           {
               // Get the web context from the collection
               SPWeb web = fieldCollection.Web;

               // First collect all contenttypes to an array, so we can use Linq
               var contentTypes = web.ContentTypes.CopyToArray< SPContentType>();

               // Filter contenttypes where field is used
               var contentTypesWithField = (from contentType in contentTypes
                                            where contentType.Fields.ContainsField(field.InternalName)
                                            select contentType).ToArray();

               // Create usages for fields in contenttypes
               foreach(SPContentType contentType in contentTypesWithField)
               {
                   SPSiteColumnUsage siteColumnUsage = new SPSiteColumnUsage(field.Id, field.Scope, contentType.Id);
                   list.Add(siteColumnUsage);
               }

               // Get unique lists-urls where contentypes are beeing used
               var listUrlsContentTypeUsage = (from contentType in contentTypesWithField
                                               let contentTypeUsages = SPContentTypeUsage.GetUsages(contentType)
                                               from contentTypeUsage in contentTypeUsages
                                               where contentTypeUsage.IsUrlToList
                                               select contentTypeUsage.Url).Distinct().ToArray();

               // Create usages for fields in list
               foreach (string listUrl in listUrlsContentTypeUsage)
               {
                   SPSiteColumnUsage siteColumnUsage = new SPSiteColumnUsage(field.Id, field.Scope, listUrl);
                   list.Add(siteColumnUsage);
               }
           }
       }

       return list;
   }

   private readonly Guid _Id;
   private readonly string _Scope;
   private readonly SPContentTypeId _ContentTypeId;
   private readonly string _Url;

   private SPSiteColumnUsage(Guid id, string scope, SPContentTypeId contentTypeId)
   {
       _Id = id;
       _Scope = scope;
       _ContentTypeId = contentTypeId;
       _Url = null;
   }

   private SPSiteColumnUsage(Guid id, string scope, string url)
   {
       _Id = id;
       _Scope = scope;
       _ContentTypeId = SPContentTypeId.Empty;
       _Url = url;
   }

   public Guid Id
   {
       [DebuggerStepThrough]
       get { return _Id; }
   }

   public string Scope
   {
       [DebuggerStepThrough]
       get { return _Scope; }
   }

   public SPContentTypeId ContentTypeId
   {
       [DebuggerStepThrough]
       get { return _ContentTypeId; }
   }

   public string Url
   {
       [DebuggerStepThrough]
       get { return _Url; }
   }

   public bool IsUrlToList
   {
       [DebuggerStepThrough]
       get { return _ContentTypeId.Equals(SPContentTypeId.Empty) && !string.IsNullOrEmpty(_Url); }
   }
}