23 February 2009

Create a Fake HttpContext for SharePoint

While coding for SharePoint I usualy start by putting code in a console application to create the nessasary functionaly and on a later stage move it over to a browser application. I do this because I find it speeding up the development cycle, because SharePoint deployment cause a bit of overhead. But once in a while I need the HttpContext.Current to be set and I there isn't one in a console app.

To overcome this problem I created my own WorkerRequest class and a helper method that returns a HttpContext to fake a current http context. Both the class and method take a SPWeb as contructor parameter to put the correct SharePoint context into the http context.
public class FakeSharePointWorkerRequest : SimpleWorkerRequest
{
    private readonly string _ServerName;

    public FakeSharePointWorkerRequest(SPWeb web) : base(web.ServerRelativeUrl, web.Site.WebApplication.IisSettings[SPUrlZone.Default].Path.FullName, string.Empty, string.Empty, null)
    {
        _ServerName = web.Site.HostName;
    }

    public override string GetServerName()
    {
        return _ServerName;
    }
}

public static HttpContext GetFakeHttpContextForSharePoint(SPWeb web)
{
    FakeSharePointWorkerRequest workerRequest = new FakeSharePointWorkerRequest(web);
    HttpContext httpContext = new HttpContext(workerRequest);
    httpContext.Items["HttpHandlerSPWeb"] = web;
    httpContext.Items["HttpHandlerSPSite"] = web.Site;
 
    return httpContext;
}
Usage example
using(SPSite site = new SPSite("http://demosite"))
{
    using(SPWeb web = site.OpenWeb())
    {
        bool httpContextIsFake = false;
        if (HttpContext.Current == null)
        {
            // Must be set before using the SPLimitedWebPartManager in a console app
            HttpContext.Current = GetFakeHttpContextForSharePoint(web);
            httpContextIsFake = true;
        }
 
        // Do your own thingies!
        // .....
 
        // Don't forget to reset the current http context
        if (httpContextIsFake)
        {
            HttpContext.Current = null;
        }                     
    }
}

17 February 2009

Programmatically show all list templates for a SharePoint web

To programmatically create lists in a SharePoint web, I found you have to look for list templates in two different locations.

The first (obvious) location is in the ListTemplates collection on the SPWeb object, but then u would mis the custom list templates stored in the List Template Gallery.

The code belows shows all list templates for a web.

using(SPSite site = new SPSite("http://demosite"))
{
    using(SPWeb web = site.OpenWeb())
    {
        Console.WriteLine("==== List Templates");
        foreach(SPListTemplate template in web.ListTemplates)
        {
            Console.WriteLine(template.Name);
        }
  
        Console.WriteLine("==== List Templates from the List Template Gallery");
        foreach (SPListTemplate template in site.GetCustomListTemplates(web))
        {
            Console.WriteLine(template.Name);
        }
    }
}

12 February 2009

Introduction

Welcome to my webblog!

I'm Danny de Haas, a Dutch guy who is living in Amsterdam. I'm a professional software developer since 1999 and have experiences with C#, VB.NET, VB6, SQL 2000/2005, SharePoint Portal Server 2003 and MOSS 2007.

I work for a Dutch software company called Macaw and I am a member of the solution center 'Rich Internet Solutions' (RIS). Most of the time I've been busy with SharePoint Web Content Management. This platform has alot to talk about. So stay tuned! :-)

....to be continued.