当前位置: 代码迷 >> SharePoint >> 初记SharePoint REST API Search和COM
  详细解决方案

初记SharePoint REST API Search和COM

热度:350   发布时间:2016-05-02 06:52:24.0
小记SharePoint REST API Search和COM

1、管理员身份Visual Studio,新建类项目

  SPCOM

2、编写逻辑实现代码

  重点关注搜索结果的属性包括:

Title,Author,Path,Description,HitHighlightedSummary,FileExtension,FileType,OriginalPath,Size,Write,LastModifiedTime,IsDocument

        public string Search(string keyword, int startrow, int rowlimit, string site, string username, string password, string domain)        {            try            {                string url = string.Format("{3}/_api/search/query?querytext='{0}'&startrow={1}&rowlimit={2}&trimduplicates=false&selectproperties='Title,Author,Path,Description,HitHighlightedSummary,FileExtension,FileType,OriginalPath,Size,Write,LastModifiedTime,IsDocument'", HttpUtility.UrlEncode(keyword), startrow, rowlimit, site);                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);                req.Credentials = new NetworkCredential(username, password, domain);                req.Method = "GET";                req.Accept = "application/json;odata=verbose";                req.ContentType = "application/json;charset=UTF-8";//text/plain; charset=utf-8                HttpWebResponse res = (HttpWebResponse)req.GetResponse();                using (StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8))                {                    string html = reader.ReadToEnd();                    return JsonConvert.SerializeObject(new Response() { Result = true, Data = new SearchResult(html) });                }            }            catch (Exception ex)            {                return JsonConvert.SerializeObject(new Response() { Result = false, Message = ex.Message });            }        }

 分别对SharePoint类和接口类中Search方法添加Guid(使用Visual Studio自带的GUID生成工具:工具 ->创建GUID)

3、强名称签名

  签名 -> 为程序集签名 -> 新建 -> SPCOM.pfx

4、应用程序 ->程序集信息 ->勾选“版本号&使程序Com可见”

5、生成 -> 勾选"为COM互操作注册"

6、编译生成SPCOM.dll

7、管理员方式打开:VS2014开发工具命令提示【部署时,目标机上可能无相关的regasm和gacutil工具,可找到该工具并拷贝到目标机使用】

  跳转到SPCOM.dll所在文件夹

  cd /d D:\Dev\AspCommon\com\dll\

  -> regasm SPCOM.dll /tlb:SPCOM.tlb  生成 *.tlb文件/注册类型

  -> gacutil /i SPCOM.dll  将程序集添加到缓存

8、测试功能

     dim sp     set sp = Server.CreateObject("SPCOM.SharePoint")    dim txtSearch,result    if request.QueryString.Count>0 then        txtSearch = Request("txtSearch")        result = sp.Search(txtSearch,0, 10, "http://**/sites/cnblog", "administrator", "****", "spserver")    end if

输入:“正则表达式”

结果:

{"Result":true,"Data":{"TotalRows":2,"StartRow":0,"RowCount":2,"RowLimit":10,"Rows":[{"Title":"一、正则表达式是什么?","Author":"MINGYUE","Path":"file://192.168.244.170/Doc/正则表达式.docx","Description":null,"HitHighlightedSummary":"比较正规的解释是:正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串 在这里,我希望使用一个更为通俗的自然语言来描述它:正则表达式就是用一类字符的统称来描述这一大批字符 举个例子,正则里说汉字,那么其实它就涵盖了所有的中国文字 现在你是否明白了正则到底是个什么东西了吧 不要紧,继续往下看,文中会通过一些简单的实例来帮助你对正则的理解 三、正则表达式要 ","FileExtension":"docx","FileType":"docx","OriginalPath":"file://192.168.244.170/Doc/正则表达式.docx","Size":25906,"Write":"2013-10-21T18:53:00+08:00","LastModifiedTime":"2013-10-21T18:53:00+08:00","IsDocument":true},{"Title":"SQL数据库.docx","Author":"MINGYUE","Path":"file://192.168.244.170/Doc/SQL数据库.docx","Description":null,"HitHighlightedSummary":"Rank,Dense_rank,Row_number函数\t6 first、last函数:排名查询\t6 private static string connectionString = "Data Source=127.0.0.1:1521/orcl;User ID = test;Password = test;Unicode=True 连接所有的查询结果、去掉 ","FileExtension":"docx","FileType":"docx","OriginalPath":"file://192.168.244.170/Doc/SQL数据库.docx","Size":104514,"Write":"2014-01-27T01:45:00+08:00","LastModifiedTime":"2014-01-27T01:45:00+08:00","IsDocument":true}]},"Message":null} 

 

 附上代码

留下该文以便以后查阅,如有错误,请帮忙指出...

 

更新COM组件的dll时,需要运行第7步中的gacutil /i SPCOM.dll,重新将程序集添加到缓存中

1楼霖雨
好文啊!顶一个!
  相关解决方案