Useful generic example

The following is a great example of the use of generics to provide strongly typed serialization:

using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace SerilizationTest
    static class Pickler
        public static T UnPickle<T>(Stream s)
            XmlSerializer xs = new XmlSerializer(typeof(T));
            return (T)xs.Deserialize(s);

        public static void Pickle<T>(Stream s, T o)
            XmlSerializer xs = new XmlSerializer(typeof(T));
            xs.Serialize(s, o);

        public static string Pickle<T>(T o)
            XmlSerializer xs = new XmlSerializer(typeof(T));
            StringWriter sr = new StringWriter();
            xs.Serialize(sr, o);
            return sr.ToString();

        public static T UnPickle<T>(string s)
            XmlSerializer xs = new XmlSerializer(typeof(T));
            StringReader sr = new StringReader(s);
            return (T)xs.Deserialize(sr);

        public T UnPickle<T>(XmlNode node)
            XmlSerializer xs = new XmlSerializer(typeof(T));
            StringReader sr = new StringReader(node.OuterXml);
            XmlReader xr = XmlReader.Create(sr);
            return (T)xs.Deserialize(xr);        

Minimal adding pages to sharepoint

The following is the boo source to a utility that can be used to add pages to a SharePoint site.
No feature required!

=== ===
import Microsoft.SharePoint
import System.IO

def addPage(_site as string, _web as string, _page as string, _url as string):
    site = SPSite(_site)
    web = site.OpenWeb(_web)
    folder = web.GetFolder(“”)
    folder.Files.Add(_url, File.OpenRead(_page), true)
addPage(argv[0], argv[1], argv[2], argv[3])    

BDC Columns

This is another technique for adding BDC columns in a feature specific manner.

The BDC in SharePoint (which is only available in the paid-for MOSS edition of SharePoint) allows you to add external business data to a SharePoint list.  There are several catches:

  • The document that you need to enable this is complex and really requires an expensive tool (such as Bdc Metaman – which is useful but not ideal).
  • Normally you must add the BDC column to the site via a GUI (I have found a way to do this in code see another post for today).
  • The business data is cached in the sharepoint list until it is explicitly refreshed. The GUI refresh only provides an all the list refresh option (this can be done in code for a specific row).
  • You are limited to data sources that have a .Net 2.0 database driver (Sybase does not have one out of the box and the Data Direct one is very expensive).  Alternatively you can write a web service to fetch the data.
The BDC is powerful but needs a lot of work to get it going – possibly more than a custom solution would require…

Warmup script for sharepoint

I have seen a number of these around.  Most are copies of a broken script.  This one works.

The first component is a boo script:

This requires the Boo.Lang.Dll and needs to be compiled with the boo compiler booc (booc

==== ======
import System.Net

def warmup(url as string):
    request = WebRequest.Create(url)
    request.Credentials = CredentialCache.DefaultCredentials
    request.Method = “GET”
    response = request.GetResponse()

for url as string in argv:

The second component is a batch file:

==== Warmup.bat ====
@cscript iisapp.vbs /a MyAppPool /r
@Start /min warmup http://myurl:50002/page1.aspx http://myurl:50002/page2.aspx
@Start /min warmup http://myurl:50002/page3.aspx

These should save you hours of waiting around for SharePoint at the cost of a little bit of load.  Beware I am unable to test the first line of the script as I am not currently at a W2k3 box.  It recycles a given app pool.

SharePoint – Add a page directly to a sharepoint site

The following is the code from a feature receiver event.

This allows a page to be added to a sharepoint site without using a page library.

public override void FeatureActivated(SPFeatureReceiverProperties properties)


    SPWeb web = properties.Feature.Parent as SPWeb;


    FileStream fStream = File.OpenRead(properties.Definition.RootDirectory + \NewPage.aspx);


Web.AllowUnsafeUpdates = true;


SPFolder folder = web.GetFolder(“”);


Folder.Files.Add(“NewPage.aspx”, fStream);