Showing posts with label Incremental Crawl. Show all posts
Showing posts with label Incremental Crawl. Show all posts

Thursday, April 25, 2013

How to 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 reindex 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();
 }
}