以编程方式开始在Sharepoint 2010中抓取给定的内容源

时间:2011-09-29 08:33:40

标签: sharepoint-2010

是否可以通过Sharepoint API或任何其他方式以编程方式开始抓取给定内容源(比如文件共享)?

1 个答案:

答案 0 :(得分:1)

基于Rob上面的评论,我发现这有用。以下是我做的C#代码。

如果您正在构建控制台应用程序,则链接中的代码会在SPServiceContext.Current上引发错误。所以第一步和GetContext()方法特定于那种情况。

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...");

确保使用Sharepoint安装在Project Properties中调整构建目标平台。否则将不会创建SpSite。