Creating List Indexes with the SharePoint Object Model

Many scenarios in SharePoint 2010 require you to create indexes on lists – e.g. enforcing unique values, CAML joins, and referential integrity. I was writing some code yesterday that needed an index and I wasn’t able to find a sample – so I figured I’d put up a quick post.

Before you get started, understand that you can only index certain field types and that compound indexes are even more restricted. See this page for the types of indexable fields: Metadata Navigation and Filtering.

Here is the sample c# code. (list is an instance of SPList for the list that will be indexed)

Single Column Index

SPField title = list.Fields["Title"];
title.Indexed = true;
title.Update();

list.FieldIndexes.Add(title);

Compound Index

SPField createdBy = list.Fields["Created By"];
createdBy.Indexed = true;
createdBy.Update();

SPField created = list.Fields["Created"];
created.Indexed = true;
created.Update();

list.FieldIndexes.Add(createdBy, created);

Author: Doug Ware