home > news Automatic removal of lists when deactivating ListInstance feature

Automatic removal of lists when deactivating ListInstance feature




Creating lists using ListInstances is quite easy to do within a feature. However, deleting those lists again when deactivating that feature does not happen automatically (which is understandable, since you don't want to lose all data in the list when accidentally deactivating the feature ...).

So, I created a feature receiver to do the job for you :) The receiver simple deletes all lists created by a ListInstance tag in the feature.

This is how I did it:

1. The feature.xml file

<?xml version="1.0" encoding="utf-8" ?>

<Feature xmlns="http://schemas.microsoft.com/sharepoint/"

         DefaultResourceFile="blog"

         Id="{40B0BD50-8A65-4cc3-99E4-28024DA1A487}"

         Title="Blogging list instances"

         Description="This feature enables the custom list instance definitions."

         Version="1.0.0.0"

         Scope="Web"

         ReceiverAssembly="SharePoint.Blogging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bc158afeef6140c2"

         ReceiverClass="SharePoint.Blogging.ListInstanceRemover">

  <ElementManifests>

    <ElementManifest Location="Elements.xml"/>

  </ElementManifests>

</Feature>

Notice that I set the DefaultResourceFile property to a self-created resource file, which is deployed in the 12/Resources folder.

2. The elements.xml file

<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <ListInstance Id="0"

                Description="A test list for the blogging article."

                FeatureId="{00BFEA71-DE22-43B2-A848-C05709900100}"

                OnQuickLaunch="TRUE"

                QuickLaunchUrl="/BlogList/AllItems.aspx"

                TemplateType="100"

                Title="$Resources:ListInstances_List_Title"

                Url="BlogList" />

  <ListInstance Id="1"

                Description="A test document library for the blogging article."

                FeatureId="{00BFEA71-E717-4E80-AA17-D0C71B360101}"

                OnQuickLaunch="TRUE"

                QuickLaunchUrl="/BlogDocLib/Forms/AllItems.aspx"

                TemplateType="101"

                Title="$Resources:blog,ListInstances_DocLib_Title;"

                Url="BlogDocLib" />

</Elements>

3. The feature receiver (the important part)

To create a feature receiver, we create a class overriding SPFeatureReceiver.

using System;


using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

using Microsoft.SharePoint.Utilities;

using System.Globalization;


namespace SharePoint.Blogging

{

    public class ListInstanceRemover : SPFeatureReceiver

    {

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

        {

            CultureInfo ci = System.Globalization.CultureInfo.InvariantCulture;

            SPElementDefinitionCollection elementFiles = properties.Definition.GetElementDefinitions(ci);


            foreach (SPElementDefinition elementFile in elementFiles)

            {

                if (elementFile.ElementType == "ListInstance")

               {

                    string defaultResourceFile = properties.Feature.Definition.DefaultResourceFile;

                    string listTitle = SPUtility.GetLocalizedString(elementFile.XmlDefinition.Attributes["Title"].Value, defaultResourceFile, Convert.ToUInt32(ci.LCID));


                    SPWeb web = properties.Feature.Parent as SPWeb;

                    SPList list = web.Lists[listTitle];


                    list.Delete();

                }

            }

        }


        public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)

        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

        {

        }

    }

}

You might also notice me using the SPUtility.GetLocalizedString method, which actually is very handy to use when dealing with resource files, as it translates the resource string into the localized value.

There, that's it. I hope it helps someone in some way ... ;)


Posted on SharePoint Blogs







FFTF News Archives :
News Articles Archives: Apr 2005 | Aug 2005 | Dec 2005 | Feb 2005 | Jul 2005 | Jun 2005 | Mar 2005 | May 2005 | Nov 2005 | Oct 2005 | Sep 2005 | Oct 2005 | Nov 2005 | Dec 2005 | Jan 2006 | Feb 2006 | Mar 2006 | Apr 2006 | May 2006 | Jun 2006 | July 2006 | August 2006 | September 2006 | October 2006 |  
SharePoint Blogs