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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s