Is it possible to开发者_如何转开发 programmatically start crawling a given content source (say a file share) through Sharepoint API or any other means?
Based on Rob's comment above I found this to be helpful. Following is the C# code I did.
The code in the link throws an error on SPServiceContext.Current if you're building a console app. So the first step and the GetContext() method are specific to that situation.
SPSite mySite = new SPSite("http://localhost");
SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(mySite)); Guid appId = proxy.GetSearchServiceApplicationInfo().SearchServiceApplicationId;
//Console.WriteLine("AppID : " + appId.ToString());
SearchServiceApplication app = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(appId);
Content content = new Content(app);
ContentSourceCollection cs = content.ContentSources;
Console.WriteLine("Name\tId\tCrawlCompleted");
foreach (ContentSource csi in cs)
{
Console.WriteLine(csi.Name + "\t" + csi.Id + "\t" + csi.CrawlCompleted);
}
Console.WriteLine("Starting full crawl....");
ContentSource css = content.ContentSources["source name"]; //csi.Name within square brackets
css.StartFullCrawl();
Console.WriteLine("Full crawl on source name started...");
Be sure to adjust the build target platform in the Project Properties with the Sharepoint installation. Otherwise SpSite will not be created.
精彩评论