Programmatically enabling Records Management

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]

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft;
using Microsoft.SharePoint;
namespace SharePointConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteURL = "http://sp2010";
            using (SPSite site = new SPSite(siteURL))
            {
                SPWeb w = site.OpenWeb();
                SPList dl;
                bool needToCreate = false;
                try
                {
                    dl = w.Lists["Records"];
                    Console.WriteLine("found");
                }
                catch (System.ArgumentException)
                {
                    needToCreate = true;
                }
                if (needToCreate)
                {
                    //missing so create it
                    Console.WriteLine("Records DL missing on " + w.Url);
                    Guid g = w.Lists.Add("Records", "** Coporate Records Management **", SPListTemplateType.DocumentLibrary);
                    w.Update();
                    dl = w.Lists[g];
                    //enable auto-Record for uploads
                    Microsoft.Office.RecordsManagement.RecordsRepository.Records.ConfigureListForAutoDeclaration(dl, true);
                    //add CEWP to default view
                    dl = w.Lists[g];
                    //show on QuickLaunch
                    if (!dl.OnQuickLaunch)
                    {
                        dl.OnQuickLaunch = true;
                        dl.Update();
                    }
                    //disable folders
                    if (dl.EnableFolderCreation)
                    {
                        dl.EnableFolderCreation = false;
                        dl.Update();
                    }
                    //enable Content Types
                    if (!dl.ContentTypesEnabled)
                    {
                        dl.ContentTypesEnabled = true;
                        dl.Update();
                    }
                    //disable Required Check Out
                    if (dl.ForceCheckout)
                    {
                        dl.ForceCheckout = false;
                        dl.Update();
                    }
                    //add Content Type to default view
                    Console.WriteLine("CT");
                    SPField ctField = dl.Fields["Content Type"];
                    SPView defaultView = dl.Views[dl.DefaultView.ID];
                    defaultView.ViewFields.Add(ctField);
                    defaultView.Update();
                    dl.Update();
                }
            }
        }
    }
}

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

3 Comments

  1. Hi Jeff,
    Thanks for the post. I have a question … Can you turn off In Place Records for a specific site? (below a site collection?).
    Thanks
    DW

    1. Great question. I think you can turn it off so long as no documents have been uploaded.

      If you have SPFile() items which are declared as a record then you must first “un-declare” each SPFile() before the parent SPDocumentLibrary() can have in place RM turned off. The disabling seems to check each SPFile() and ensure there are no records before it will continue.

Leave a Comment

Return to Top ▲Return to Top ▲