Thursday 20 October 2011

How To Remove the columns "Barcode", "Barcode Value", "Exempt from Policy" when removing IRM policy in a list in Sharepoint 2007 and Sharepoint 2010

Hi,

Recently someone expressed his issue of unable to remove the columns "Barcode", "Barcode Value" and "Exempt from Policy" from the list even after the policy is removed. These columns can be simply removed from the view by going into the view settings and checking these columns. However the requester expressed his concerns as why these columns where still there and why there were not removed when there the policy was removed. Hence to solve this I created the following application. It takes the site URL and list name as input values. There was no option to delete them through the UI.
When initially I tried removing them from code, I got this:




Microsoft.SharePoint.SPException was unhandled
  Message="You cannot delete a sealed column."
  Source="Microsoft.SharePoint"
  ErrorCode=-2130575213
  StackTrace:
       at Microsoft.SharePoint.Library.SPRequest.RemoveField(String bstrUrl, String bstrListName, String bstrFieldName)
       at Microsoft.SharePoint.SPFieldCollection.Delete(String strName)
       at Microsoft.SharePoint.SPField.Delete()
       at SSPProfile.Form1..ctor() in D:\Users\Rahul\Documents\Visual Studio 2008\RahulTeamsite\SSPProfile\SSPProfile\Form1.cs:line 115
       at SSPProfile.Program.Main() in D:\Users\Rahul\Documents\Visual Studio 2008\RahulTeamsite\SSPProfile\SSPProfile\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Runtime.InteropServices.COMException
       Message="You cannot delete a sealed column."
       Source=""
       ErrorCode=-2130575213
       StackTrace:
            at Microsoft.SharePoint.Library.SPRequestInternalClass.RemoveField(String bstrUrl, String bstrListName, String bstrFieldName)
            at Microsoft.SharePoint.Library.SPRequest.RemoveField(String bstrUrl, String bstrListName, String bstrFieldName)
       InnerException:

I modified my code and now it is working fine. This has been tested in both Sharepoint 2007 and Sharepoint 2010



If you are executing this make sure you have removed the policy otherwise you will face some.other issues


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace DeleteBarcodeIRMPolicyColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This tool will remove barcode related columns from your list. Before executing this tool make sure that you have removed the policy otherwise it will take you to some other issues ");
            Console.WriteLine("Please enter your site url");
            String siteUrl = Console.ReadLine();
            Console.WriteLine("Please enter your List Name");
            String listName = Console.ReadLine();
            try
            {
                using (SPSite site = new SPSite(siteUrl))
                {

                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        SPList listSource = web.Lists[listName];
                        String[] columnNames = { "Barcode", "Barcode Value", "Exempt from Policy" };
                        for (int i = 0; i < columnNames.Length; i++)
                        {
                            SPField fieldtoBedeleted = listSource.Fields[columnNames[i]];
                            fieldtoBedeleted.AllowDeletion = true;
                            fieldtoBedeleted.Sealed = false;

                            fieldtoBedeleted.Delete();
                            listSource.Update();
                        }
                        web.AllowUnsafeUpdates = false;
                        Console.WriteLine("The columns are deleted successfully");
                        Console.ReadLine();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}

I hope this will help you out.

Thanks,
Rahul Rashu


No comments:

Post a Comment