Programmatically enabling Records Management

June 19, 2011 in Uncategorized

Recently I had someone ask about how In-Place Records Management could be enabled in SharePoint 2010 for a given document with C# object model code.  The method ConfigureListForAutoDeclaration() was key to making everything work.  This would allow for easily enabling on many sites all at once.  Below I’ve included C# code and screenshots from the test site where it was enabled.  Please feel free to leave any questions below with a comment.

  • Open site
  • Enable “In-Place Records Management” site collection feature  (da2e115b-07e4-49d9-bb2c-35e93bb9fca9)
  • Open document library
  • Open “Record declaration settings”
  • Enable “Automatic Declaration” with [Microsoft.Office.Policy.dll]
  • Upload test files
  • Confirmed:  see padlock icon and unable to delete document library

 

C# – Visual Studio 2010 Console Application

Add assembly reference to [C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPIMicrosoft.Office.Policy.dll]

 

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using Microsoft;
   6:  using Microsoft.SharePoint;
   7:   
   8:  namespace SharePointConsoleApp1
   9:  {
  10:      class Program
  11:      {
  12:          static void Main(string[] args)
  13:          {
  14:              string siteURL = "http://sp2010";
  15:              using (SPSite site = new SPSite(siteURL))
  16:              {
  17:                  SPWeb w = site.OpenWeb();
  18:                  SPList dl;
  19:                  bool needToCreate = false;
  20:                  try
  21:                  {
  22:                      dl = w.Lists["Records"];
  23:                      Console.WriteLine("found");
  24:                  }
  25:                  catch (System.ArgumentException)
  26:                  {
  27:                      needToCreate = true;
  28:                  }
  29:   
  30:                  if (needToCreate)
  31:                  {
  32:                      //missing so create it
  33:                      Console.WriteLine("Records DL missing on " + w.Url);
  34:                      Guid g = w.Lists.Add("Records", "** Coporate Records Management **", SPListTemplateType.DocumentLibrary);
  35:                      w.Update();
  36:                      dl = w.Lists[g];
  37:   
  38:                      //enable auto-Record for uploads
  39:                      Microsoft.Office.RecordsManagement.RecordsRepository.Records.ConfigureListForAutoDeclaration(dl, true);
  40:   
  41:                      //add CEWP to default view
  42:                      dl = w.Lists[g];
  43:   
  44:                      //show on QuickLaunch
  45:                      if (!dl.OnQuickLaunch)
  46:                      {
  47:                          dl.OnQuickLaunch = true;
  48:                          dl.Update();
  49:                      }
  50:   
  51:                      //disable folders
  52:                      if (dl.EnableFolderCreation)
  53:                      {
  54:                          dl.EnableFolderCreation = false;
  55:                          dl.Update();
  56:                      }
  57:   
  58:                      //enable Content Types
  59:                      if (!dl.ContentTypesEnabled)
  60:                      {
  61:                          dl.ContentTypesEnabled = true;
  62:                          dl.Update();
  63:                      }
  64:   
  65:                      //disable Required Check Out
  66:                      if (dl.ForceCheckout)
  67:                      {
  68:                          dl.ForceCheckout = false;
  69:                          dl.Update();
  70:                      }
  71:   
  72:                      //add Content Type to default view
  73:                      Console.WriteLine("CT");
  74:                      SPField ctField = dl.Fields["Content Type"];
  75:                      SPView defaultView = dl.Views[dl.DefaultView.ID];
  76:                      defaultView.ViewFields.Add(ctField);
  77:                      defaultView.Update();
  78:                      dl.Update();
  79:                  }
  80:              }
  81:          }
  82:      }
  83:  }

Screenshots

image

DisplayName

Id

Scope

RecordResource

5bccb9a4-b903-4fd1-8620-b795fa33c9ba

Site

RecordsManagement

6d127338-5e7d-4391-8f62-a11e43b1d404

Farm

InPlaceRecords

da2e115b-07e4-49d9-bb2c-35e93bb9fca9

Site

RecordsCenter

e0a45587-1069-46bd-bf05-8c8db8620b08

Web

image

image

image

image

image