Our Code

Creating an Epic Wedding Reception with Silverlight 4

By Petteri Lehtonen
Sunday, July 31, 2011
Posted in Code
Tags:

This might no be very useful information when creating line of business applications, but I thought that it is cool enough that it’s worth sharing.

It all started when my brother decided to get married and organize a party after the ceremony… you know the usual wedding stuff… As a best man i wanted to do something to make this wedding stand out from the rest. Since i pretty much suck at everything not computer related so that’s what I had to go for. :)

Read More

How to create Waterfall charts in QlikView

By Niklas Lindman
Wednesday, March 16, 2011
Posted in Code

Every now and then customers want waterfall charts in our QlikView applications. As this is not a standard chart type in QlikView, it is not completely straightforward. This is how we have done it.

Read More

IPv4 address conversion for log reporting in TMG 2010

By
Tuesday, November 9, 2010
Posted in Code
Tags: , ,

Both ISA Server 2004/2006 and TMG 2010 can store logs in SQL Server (MSDE/Express Edition) which is recommended configuration.

Unlike ISA Server which stores IP address as bigint type TMG 2010 stores them as GUIDs for IPv4 and IPv6 compatibility.

So, a function is needed to see IPv4 addresses in easily readable way.

CREATE FUNCTION [dbo].[GuidToIPv4]
(
@guid VARCHAR(36)
)
RETURNS VARCHAR(15)
AS
BEGIN
 DECLARE @hex VARCHAR(8)
 SET @hex = LEFT(@guid, 8 )
 RETURN CONVERT(VARCHAR(3), sys.fn_replvarbintoint(sys.fn_cdc_hexstrtobin(LEFT(@hex, 2))), 0) + '.' +
  CONVERT(VARCHAR(3), sys.fn_replvarbintoint(sys.fn_cdc_hexstrtobin(SUBSTRING(@hex, 3, 2))), 0) + '.' +
  CONVERT(VARCHAR(3), sys.fn_replvarbintoint(sys.fn_cdc_hexstrtobin(SUBSTRING(@hex, 5, 2))), 0) + '.' +
  CONVERT(VARCHAR(3), sys.fn_replvarbintoint(sys.fn_cdc_hexstrtobin(RIGHT(@hex, 2))), 0)
END
GO

Actually, IPv4 address forms a GUID where first 8 characters naturally make 4 bytes in hex – 1.1.1.1 makes 01010101-FFFF-0000-0000-000000000000 and 255.255.255.255 makes FFFFFFFF-FFFF-0000-0000-000000000000, so if you read hex fluently – here you go. :-)

SharePoint: Run with elevated privileges

By Andreas Finne
Tuesday, June 22, 2010
Posted in Code
Tags: ,

When developing using the SharePoint object model, certain operations has to be run with elevated privileges. For instance, setting information into the property bag of a site needs to be done with elevated privileges. This post shows how this is done, and also includes a helper method for making it easier and shorter to do this.

There is a method called SPSecurity.RunWithElevatedPrivileges in the object model that takes a delegate as argument. The delegate is then run with more permissions. An important note regarding this: You must not use old references to SPWeb or SPSite instances inside your delegate, since they are “contaminated” with lower permissions. What you have to do is to create new SPSite and SPWeb instances using the Ids of the sites and webs you already have a reference to, like this:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
    {
        using (SPWeb elevatedWeb = site.OpenWeb(webId))
        {
            //Your code here
        }
    }
});

If you need to do these kind of operations in several locations, this is a lot of repeating code to include. A colleague of mine made a small helper method that does the setup for elevated privileges for you. Here’s the short code snippet:

private delegate void CodeToRunElevated(SPWeb elevatedWeb);
private static void RunElevated(Guid webId, CodeToRunElevated secureCode)
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb elevatedWeb = site.OpenWeb(webId))
            {
               secureCode(elevatedWeb);
            }
        }
    });
}

To invoke this, you call on the method with the Id of your web as one argument, and a delegate taking an SPWeb as the other. When the method is invoked, the SPWeb argument will be “uncontaminated” and pushed to elevated privileges. This example sets the name, title, and description of a site.

RunElevated(web.ID, delegate(SPWeb elevatedWeb)
{
    elevatedWeb.Name = siteTitle;
    elevatedWeb.Title = siteTitle;
    elevatedWeb.Description = siteDescription;
    elevatedWeb.Update();
});

SQL access with integrated security from SharePoint

By Andreas Finne
Thursday, April 15, 2010
Posted in Code

When creating custom webparts or other custom features in SharePoint that retrieves data from a SQL server, there are a few gotchas and pitfalls that you should be aware of. I spent a few hours battling a couple of problems, so I thought I’d document the things I found out here to spread the knowledge.

Read More

Silverlight 4.0 Navigation Framework and Prism

By
Thursday, March 25, 2010

A few weeks ago I had a problem how to combine the Silverlight Navigation Framework (now on NF) with Prism. It is a known fact that the Prism and the NF are not directly compatible. The main problem was that the Prism makes the UI composition with regions and the NF uses frames. I wanted to use the NF because it interacts with the Browser History journal.

I googled to find a good way to do this  but all the solutions where somewhat limited or required some kind of hack. I was not happy with those so I dived deeper to the NF. I found out that in Silverlight 4, the navigation framework allows one to write own content loaders for the Frame. This is great because I could build my own content loader using Unity.

Read More

Extension Method: Convenient way to add items to an ICollection

By Per Lundberg
Wednesday, March 24, 2010
Posted in Code

Triggered by a recent blog posting by Andreas Finne, I’ll share a convenient extension method that I have written.

First, let’s give an introduction the “why” that triggered me writing this extension method from the beginning. The List<T> generic class is a pretty good class for keeping a list of objects (which are strongly typed, because this is a generic type). The List<T>, in turn, implements the ICollection<T> interface, which has a method defined which looks like this:

void Add(T item);

Now, there is only one big problem with this method: it should really return an ICollection<T>. Why, oh why didn’t Microsoft think of that? That way, you could have written your code like this:

IEnumerable<Customer> customers = new List<Customer>()
    .Add(new Customer { CustomerNumber = 12345, Name = "Customer #1" })
    .Add(new Customer { CustomerNumber = 23456, Name = "Customer #2" });

The problem withthis code snippet is that the Add() method returns void, which in turns means that the “fluid” syntax above is not possible.

Not until you work around it, that is. :-)

The way we can solve this is by writing a generic extension method that works on an ICollection<T> object and does the Add() and then returns the collection itself. That way, we should be able to accommodate a similar syntax similar to this.

I prefer to place my extension methods in a class which is named “<class>ExtensionMethods”, where <class> is the name of the class which the extension methods operate on. Thus, I will call this class ICollectionTExtensionMethods. Here is the code:

public static class ICollectionTExtensionMethods
{
    /// <summary>
    /// Adds an item to the <see cref="System.Collections.Generic.ICollection<T>"/>.
    /// </summary>
    /// <typeparam name="T">The type of object which the collection contains</typeparam>
    /// <param name="collection">The <see cref="System.Collections.Generic.ICollection<T>"/> to operate on</param>
    /// <param name="item">The object to add to the <see cref="System.Collections.Generic.ICollection<T>"/></param>
    /// <returns>The <see cref="System.Collections.Generic.ICollection<T>"/> we are operating on</returns>
    public static ICollection<T> AddWithReturn<T>(this ICollection<T> collection, T item)
    {
        collection.Add(item);
        return collection;
    }
}

(I copied the XML documentation from the ICollection<T> interface definition metadata. :-)

Now, we can write some code that looks like this: (Remember to include a using-line for the namespace where you placed this ICollectionTExtensionMethods class.)

IEnumerable<Customer> customers = new List<Customer>()
    .AddWithReturn(new Customer { CustomerNumber = 12345, Name = "Customer #1" })
    .AddWithReturn(new Customer { CustomerNumber = 23456, Name = "Customer #2" });

Pretty nice, huh? :-)

Alright, this was it for this time. HTH!

Generic extension method for sorting by property name

By Per Lundberg
Tuesday, March 23, 2010
Posted in Code

I found some really nice code the other day. Unfortunately I cannot say that I wrote it myself, but I think it’s so cool that it needs to be published here. It was written by “mazzy maxim” as Community Content to MSDN. What it does is to extend the OrderBy method for IEnumerables. You provide the name of the property to sort by, and the sort order.

Read More

From Windows Forms to Silverlight (WPF)

By
Friday, January 22, 2010
Posted in Code

I usually start learning new technologies by doing something more than hello world example. This time I wanted to learn Silverlight and decided to build Othello game. I have done it with other technologies as well like Java ME so I was quite familiar with the game concepts. So I just rushed to code.

Read More

Programmatically updating bitmaps

By Mikael Riska
Tuesday, January 19, 2010
Posted in Code
Tags: ,

Those of you that are familiar with eCraft and the types of applications we create, know that we mostly work with business applications and shuffle data back and forth between different back office systems or create applications that lets users interact with back office systems through more lightweight user interfaces tailored for their daily tasks. Therefore it was very interesting to be able to do yet another Microsoft Surface project, this time where the application was not data-centric but instead the focus was shifted towards the presentation end of the spectrum. We were given the opportunity to develop a Surface application to be used at the Nobel festivities with a very tight deadline for the development work. You can read a blog post about our involvement in the project here (in Finnish).

Read More