Changing properties for lists in events – Problems and Solutions

Hi,

 

Here are a few problem which I ran into when I tried to change the versioning option for document libraries:

Problems:

1. During the list added event at a site collection level feature => this would not work for list during web provisioning but would work if the list where created through the UI as a single instance

2. When I added the version change to be performed on a lists during web provisioned event, I got errors when updating list properties => Save Conflict exception

Solutions:

To avoid the list save conflict you have to retrieve at the very least the list again from the web. I reloaded the SPSite, SPWeb and the SPList object for the list path and to be sure I ran the object creation inside elevated privilages. This allowed me to avoid the error. Sample code:

// This has to be performed because of Save conflict

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    // This has to be performed because of Save conflict

                    using (SPSite site = new SPSite(properties.Web.Site.ID))

                    using (SPWeb web = site.OpenWeb(properties.Web.ID))

                    {

                        foreach (SPList list in web.Lists)

                        {

                            if (list != null && list.BaseType == SPBaseType.DocumentLibrary && !list.Hidden)

                            {

                               

                                    try

                                    {

                                        // This has to be performed because of Save conflict

                                        SPList tempListToAvoidSaveConflict = web.Lists.TryGetList(list.Title);

                                        if (tempListToAvoidSaveConflict != null)

                                        {

                                            

                                            tempListToAvoidSaveConflict.EnableVersioning = false;

                                            tempListToAvoidSaveConflict.Update();

                                        }

                                    }

                                    catch (Exception ex)

                                    {

                                        // Log your error

                                    }

                                }

                            

                        }

                    }

                });

Advertisements

Resources: SharePoint Site Definitions, Site/Web Templates and Provisioning

Creating a Custom SharePoint 2007 Portal Site Definition using the PortalProvisioningProvider Class – Paul Ballard’s WebLog

Understanding Onet.xml Files

Site Definition (Onet.xml) Files

Understanding Web*Temp.xml Files

Site Schema

Exploring The SharePoint 12 Hive :TEMPLATE Directory. – Windows Live

Supported and unsupported scenarios for working with custom site definitions and custom area definitions in SharePoint 2003, 2007 and 2010

Save a site as a site template – SharePoint Server – Microsoft Office

Adding a ‘Save site as template’ Link to Site Settings in WSS v3/MOSS 2007 using a CustomAction Feature — SharePoint Solutions Team Blog

How To Tell Which Template is in Use in SharePoint

Know the site template used for the SharePoint site | SharePoint 2010, SharePoint, C-sharp, ASP.Net, JQuery, SQL Server Solutions

SharePoint 2010 and web templates – Vesa “vesku” Juvonen – Site Home – MSDN Blogs

Understanding webtemp*.xml – Bill Baer – Site Home – TechNet Blogs

Extending the Project Workspace Template

Develop new custom site definitions and create upgrade definition files (Office SharePoint Server)

Site Definitions in SharePoint 2007 – Syrinx on SharePoint

MOSS 2007 – Save site as a template missing – The SharePoint Farmer’s Almanac

Finding site template names and ID’s in SharePoint using PowerShell | Get-SPScripts : PowerShell Scripts for SharePoint

The template you have chosen is invalid or cannot be found. – fengzhimei – 博客园

Microsoft SharePoint: Find the SharePoint web Template ID

SharePoint site creation using SPWebProvisioningProvider « Jamesemann’s Blog

WebTemplate Element (Upgrade)

SharePoint 2010 and web templates – Vesa “vesku” Juvonen – Site Home – MSDN Blogs

Modules

SPSite – The Web application at <address> could not be found – SharePoint