Sharepoint 2010を利用して全文検索を行うDemo


注意:不要なコードを注釈します.
public class SharepointSearch
    {
        public static DataTable Search(List<string> column, List<string> scope, string searchWords, IndexServiceSearchOption searchOption)
        {
            try
            {
                QueryWebServiceProxy.QueryService queryService = new QueryWebServiceProxy.QueryService();
                queryService.Url = APJ.Util.Utility.GetAppSetting("SharepointServer");
                
                //queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                queryService.Credentials = new System.Net.NetworkCredential(" ", " ", " ");
                
                System.Data.DataSet queryResults = queryService.QueryEx(BuildQueryText(column, scope, searchWords, searchOption));
                DataTable indexResult = queryResults.Tables[0];
                string rootPath = APJ.Util.Utility.GetAppSetting("SaveFilePath").Trim().ToLower();
                for (int i = 0; i < indexResult.Rows.Count; i++)
                {
                    indexResult.Rows[i][0] = indexResult.Rows[i][0].ToString().Replace("/", "\\");
                    indexResult.Rows[i][0] = indexResult.Rows[i][0].ToString().Replace("file:" + rootPath, "");
                }
                
                //add by zjy 20140620, log sharepoint service infomation
                StringBuilder msg = new StringBuilder();
                msg.Append("Sharepoint Serch Log
");                 string scopes = "";                 foreach (string s in scope)                 {                     scopes += s + "|";                 }                 msg.Append(string.Format("RootPath:{0} , SharepointServer:{1} , Scope:{2} , Keywords:{3} 
", rootPath, "url", scopes, searchWords));                 msg.Append(string.Format("Result count:{0}
", indexResult.Rows.Count));                 msg.Append("Result count:
");                 for (int x = 0; x < indexResult.Rows.Count; x++)                 {                     msg.Append(indexResult.Rows[x][0] + "
");                 }                 APJ.Util.Log.WindowLog.Log(msg.ToString(), APJ.Util.Log.LogType.Message, "SysLog");                 return indexResult;             }             catch (Exception ex)             {                 DataTable dt = new DataTable();                 return dt;             }         }         private static string ParseKeywords(string keyWords, IndexServiceSearchOption searchOption)         {             string[] strArray = keyWords.Split((char[])new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);             if (searchOption.Equals(IndexServiceSearchOption.And))             {                 return "FREETEXT(DEFAULTPROPERTIES,\'" + keyWords + "\')";             }             else if (searchOption.Equals(IndexServiceSearchOption.Or))             {                 for (int i = 0; i < strArray.Length; i = (int)(i + 1))                 {                     strArray[i] = "FREETEXT(DEFAULTPROPERTIES,\'" + strArray[i] + "\')";                 }                 string str = " Or ";                 return string.Join(str, strArray);             }             else             {                 return "FREETEXT(DEFAULTPROPERTIES,\'" + keyWords + "\')";             }         }         private static string ParseColumn(List<string> column)         {             //Join Column  as: Column1 , Column2...             //if  the column.count <1 ,then select   all  column             string returnParseColumn = string.Empty;             StringBuilder queryColumn = new StringBuilder(250);             if (column == null || column.Count < 1)             {                 queryColumn.Append("Title, Rank, Size, Description, Write, Path");                 returnParseColumn = queryColumn.ToString();             }             else             {                 foreach (string _column in column)                 {                     queryColumn.Append(_column + ",");                 }                 if (queryColumn.Length > 1)                     returnParseColumn = queryColumn.ToString().Substring(0, queryColumn.ToString().Length - 1);             }             return returnParseColumn;         }         private static string ParseScope(List<string> scope)         {             //Join Scope  as:    CONTAINS(Path,Scope1) or CONTAINS(Path,Scope2)...             string queryScope = string.Empty;             if (scope != null && scope.Count > 0)             {                 string[] queryScopeArray = new string[scope.Count];                 for (int i = 0; i < scope.Count; i++)                 {                     queryScopeArray[i] = "CONTAINS(Path,\'" + scope[i] + "\')";                 }                 queryScope = string.Join(" or ", queryScopeArray);             }             return queryScope;         }         private static string BuildQueryText(List<string> column, List<string> scope, string searchWords, IndexServiceSearchOption searchOption)         {             string queryColumn = ParseColumn(column);             string queryScope = ParseScope(scope);             string queryKeyWord = ParseKeywords(searchWords, searchOption);                          //Title, Rank, Size, Description, Write, Path             //type='MSSQLFT'             StringBuilder queryText = new StringBuilder(200);             queryText.Append("<QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'>");             queryText.Append("<Query>");             queryText.Append(" <SupportedFormats>");             queryText.Append(" <Format revision='1'> urn:Microsoft.Search.Response.Document:Document");             queryText.Append(" </Format> ");             queryText.Append(" </SupportedFormats>");             queryText.Append(@"  <Range>                                  <StartAt>1</StartAt>                                  <Count>100</Count>                                  </Range> ");             queryText.Append("<Context>");             queryText.Append("<QueryText language='en-US' type='MSSQLFT'>");             queryText.Append(" SELECT ");             queryText.Append(queryColumn);             queryText.Append(" FROM scope() ");             queryText.Append(" WHERE  ");             queryText.Append(" (" + queryKeyWord + ")");             if (!string.IsNullOrEmpty(queryScope))                 queryText.Append(" AND  (" + queryScope + ")");             queryText.Append(" ORDER BY \"Rank\" DESC");             queryText.Append("</QueryText>");             queryText.Append("</Context>");             queryText.Append("</Query>");             queryText.Append("</QueryPacket>");             return queryText.ToString();         }         public enum IndexServiceSearchOption         {             And,             Or         }     }