in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Sander's Blog

February 2008 - Posts

  • Indexed Columns on Lookup fields or Non-Supported fieldtypes

    Indexed columns are a great way to improve performance. Especially the SiteMapProviders (used by a CQWP) benefit from the indexes. I know there are some performance-tests out there, but just try to index every field you use in a Content Query Webpart and you'll know the difference. 

    It's a shame not every fieldtype can be indexed. In my experience only Text, Number, Date and Choice can be indexed. So what about Lookups, Calculated values or Custom fieldtypes?

    My workaround is to copy the values of those field types to a plain textfield and to index those textfields. This can easily be done by a workflow or an event handler:

    public override void HandleEvent(SPItemEventProperties properties)
    {
        if (properties.EventType == SPEventReceiverType.ItemUpdated || properties.EventType == SPEventReceiverType.ItemAdded)
        {
            SPListItem item = properties.ListItem;
    
            // Copy contents of the Lookup to a text field for indexing
            SPFieldLookupValueCollection values = item["LOOKUP"] as SPFieldLookupValueCollection;
            if (values != null)
            {
                foreach (SPFieldLookupValue value in values)
                {
                    item["LOOKUP_INDEXED"] += ", " + value.LookupValue;
                }
    
                // update the item without user/time-stamp 
                item.SystemUpdate(false);
            }
        }
    }
  • Programmatically Add/Remove Site Templates in the Site Gallery

    Took me a while to find out how to connect to the Site Gallery, so this might save you some time.

    using (SPSite root = new SPSite([yourRootURL]))
    {
        // DELETE OLD TEMPLATES
        List<string> toDelete = new List<string>();
    
        using (SPWeb web = root.OpenWeb())
        {
            SPFolder folder = web.Folders["_catalogs"];
            SPFolder subfolder = folder.SubFolders["wt"];
    
            using (SPWeb web = root.OpenWeb())
            {
                SPFolder folder = web.Folders["_catalogs"];
                SPFolder subfolder = folder.SubFolders["wt"];
    
                 int numberOfTemplates = subfolder.Files.Count;
        
                 for (int i = (numberOfTemplates - 1); i >= 0; i--)
                 {
                     subfolder.Files.Delete(subfolder.FilesIdea.Url);
                 }
            }
        }
    
        // SAVE SITE AS TEMPLATE
        using (SPWeb web = root.OpenWeb([yourSiteUrl]))
        {
            web.SaveAsTemplate("[Filename]", "[Title]", "[Description]", true/false);
        }
    }

    Credits to Serge for pointing me in the right direction: http://weblogs.asp.net/soever/archive/2006/06/28/NAnt-task-for-SharePoint_3A00_-Save-SPWeb-as-site-template-to-the-filesystem.aspx


Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts