Thursday, November 01, 2007

Creating Web Part Page libraries in WSS 3.0

We were trying to programmatically provision a library to store web-part pages, and to make sure that the web-part page would be the default template. Ended up with the code below. If there is a smarter way, please point me in the right direction !!

 

private static void ProvisionPageLibrary(SPWeb targetWeb, string libraryName, string libraryDescription)
        {
            //Work out the index of the Web Part Page in the DocTemplates as there is no text indexer
            bool foundWebPartPage = false;
            int counter = 0;
            int webPartPageIndex=0;
            try
            {
                while (!foundWebPartPage && counter <= targetWeb.DocTemplates.Count)
                {
                    if (String.Compare(targetWeb.DocTemplates[counter].Name, "Web Part Page", true, CultureInfo.InvariantCulture) == 0)
                    {
                        webPartPageIndex = counter;
                        foundWebPartPage = true;
                    }
                    counter++;
                }
                if (foundWebPartPage)
                {
                    targetWeb.Lists.Add(libraryName, libraryDescription, targetWeb.ListTemplates["Document Library"], targetWeb.DocTemplates[webPartPageIndex]);
                }
            }
            catch
            {
                //DoSomething meaningful
            }
        }