[Solved] Force web, whole list or library to be re-crawled by search in SharePoint 2013 using API

There is a new option in SharePoint 2013 that allows you to re-index list or document library.
You can find more about this on those blogs:

Important:It does not work in SharePoint 2010!

It is also possible to do it for whole web. Thanks to this all items in all lists will be re-crawled.

I would like to show you how to do it using API.
When using API you can use it on any type of list. It also works for non-document library items, like tasks.

Re-crawl Web

void Main()
{
  using (SPSite site = new SPSite("http://demosvr1/web1/"))
  using (SPWeb web = site.OpenWeb())
  {
    object versionObj = web.AllProperties["vti_searchversion"];
    int version = ((versionObj is int) ? ((int) versionObj) : 0);

    web.SetProperty("vti_searchversion", (version + 1));
    web.Update();
  }
}

Re-crawl list

void Main()
{
  using (SPSite site = new SPSite("http://demosvr1/web1/"))
  using (SPWeb web = site.OpenWeb())
  {
    SPList docList = web.Lists["Documents"];
    object versionObj = docList.RootFolder.Properties["vti_searchversion"];
    int version = ((versionObj is int) ? ((int) versionObj) : 0);

    docList.RootFolder.SetProperty("vti_searchversion", (version + 1));
    docList.Update();
  }
}

3 thoughts on “[Solved] Force web, whole list or library to be re-crawled by search in SharePoint 2013 using API”

  1. Powershell version for the folder one

    function markFolderForSearch($web,$list){

    $web = get-spweb $web
    $list = $web.lists[$list]

    if($list){
    $version = $list.RootFolder.Properties[“vti_searchversion”]

    if(!$version){
    $version = 0;
    }

    $list.RootFolder.SetProperty(“vti_searchversion”, $version+1)
    $list.update()

    } else {
    write-host “List not found” -ForegroundColor Red
    }
    }

    Reply

Leave a Comment