moss 2007 添加关键字及最佳匹配
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.Office.Server.Search.Administration;
namespace MossKeywords
{
public partial class frmMossKeywords : Form
{
public frmMossKeywords()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string siteUrl = this.txtSiteUrl.Text.Trim();
string keywords = this.txtKeywords.Text.Trim();
string synonyms = this.txtSynonyms.Text.Trim();
string bestBetTitle = this.txtBestBetTitle.Text.Trim();
string bestBetUrl = this.txtBestBetUrl.Text.Trim();
if (string.IsNullOrEmpty(siteUrl) && string.IsNullOrEmpty(keywords) && string.IsNullOrEmpty(bestBetTitle) && string.IsNullOrEmpty(bestBetUrl))
{
MessageBox.Show("信息不完整!", "提示");
}
else
{
using (SPSite site = new SPSite(siteUrl))
{
SPWeb rootweb = site.OpenWeb();
Keywords Keywords = new Keywords(SearchContext.GetContext(site), new Uri(rootweb.Url));
KeywordCollection keyCol = Keywords.AllKeywords;
if (!KeywordIsExist(keyCol, keywords))
{
//添加关键字
Keyword newKeyword = Keywords.AllKeywords.Create(keywords, DateTime.Now.AddDays(-1));
//添加同义词 注意:同义词与关键词不能重复
if (!SynonymsIsExist(newKeyword.Synonyms,synonyms))
{
newKeyword.Synonyms.Create(synonyms);
}
//创建最佳匹配项,可以创建多个
BestBet bestBet = newKeyword.BestBets.Create(bestBetTitle, bestBetTitle, new Uri(bestBetUrl));
newKeyword.Update();
MessageBox.Show("添加成功!", "提示");
}
else
{
foreach (Keyword kwd in keyCol)
{
if (kwd.Term.Equals(keywords))
{
//添加同义词 注意:同义词与关键词不能重复
if (!SynonymsIsExist(kwd.Synonyms,synonyms) && !KeywordIsExist(keyCol, synonyms))
{
kwd.Synonyms.Create(synonyms);
}
//创建最佳匹配项,可以创建多个
BestBet bestBet = kwd.BestBets.Create(bestBetTitle, bestBetTitle, new Uri(bestBetUrl));
kwd.Update();
MessageBox.Show("添加成功!", "提示");
}
}
}
}
}
}
/// <summary>
/// 判断关键字是否存在
/// </summary>
/// <param name="keyCol"></param>
/// <param name="keyword"></param>
/// <returns></returns>
private bool KeywordIsExist(KeywordCollection keyCol, string keyword)
{
foreach (Keyword kwd in keyCol)
{
if (kwd.Term.Equals(keyword))
return true;
}
return false;
}
/// <summary>
/// 同义词是否存在
/// </summary>
/// <param name="synCol"></param>
/// <param name="synonym"></param>
/// <returns></returns>
private bool SynonymsIsExist(SynonymCollection synCol, string synonym)
{
foreach (Synonym syn in synCol)
{
if (syn.Term.Equals(synonym))
return true;
}
return false;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.Office.Server.Search.Administration;
namespace MossKeywords
{
public partial class frmMossKeywords : Form
{
public frmMossKeywords()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string siteUrl = this.txtSiteUrl.Text.Trim();
string keywords = this.txtKeywords.Text.Trim();
string synonyms = this.txtSynonyms.Text.Trim();
string bestBetTitle = this.txtBestBetTitle.Text.Trim();
string bestBetUrl = this.txtBestBetUrl.Text.Trim();
if (string.IsNullOrEmpty(siteUrl) && string.IsNullOrEmpty(keywords) && string.IsNullOrEmpty(bestBetTitle) && string.IsNullOrEmpty(bestBetUrl))
{
MessageBox.Show("信息不完整!", "提示");
}
else
{
using (SPSite site = new SPSite(siteUrl))
{
SPWeb rootweb = site.OpenWeb();
Keywords Keywords = new Keywords(SearchContext.GetContext(site), new Uri(rootweb.Url));
KeywordCollection keyCol = Keywords.AllKeywords;
if (!KeywordIsExist(keyCol, keywords))
{
//添加关键字
Keyword newKeyword = Keywords.AllKeywords.Create(keywords, DateTime.Now.AddDays(-1));
//添加同义词 注意:同义词与关键词不能重复
if (!SynonymsIsExist(newKeyword.Synonyms,synonyms))
{
newKeyword.Synonyms.Create(synonyms);
}
//创建最佳匹配项,可以创建多个
BestBet bestBet = newKeyword.BestBets.Create(bestBetTitle, bestBetTitle, new Uri(bestBetUrl));
newKeyword.Update();
MessageBox.Show("添加成功!", "提示");
}
else
{
foreach (Keyword kwd in keyCol)
{
if (kwd.Term.Equals(keywords))
{
//添加同义词 注意:同义词与关键词不能重复
if (!SynonymsIsExist(kwd.Synonyms,synonyms) && !KeywordIsExist(keyCol, synonyms))
{
kwd.Synonyms.Create(synonyms);
}
//创建最佳匹配项,可以创建多个
BestBet bestBet = kwd.BestBets.Create(bestBetTitle, bestBetTitle, new Uri(bestBetUrl));
kwd.Update();
MessageBox.Show("添加成功!", "提示");
}
}
}
}
}
}
/// <summary>
/// 判断关键字是否存在
/// </summary>
/// <param name="keyCol"></param>
/// <param name="keyword"></param>
/// <returns></returns>
private bool KeywordIsExist(KeywordCollection keyCol, string keyword)
{
foreach (Keyword kwd in keyCol)
{
if (kwd.Term.Equals(keyword))
return true;
}
return false;
}
/// <summary>
/// 同义词是否存在
/// </summary>
/// <param name="synCol"></param>
/// <param name="synonym"></param>
/// <returns></returns>
private bool SynonymsIsExist(SynonymCollection synCol, string synonym)
{
foreach (Synonym syn in synCol)
{
if (syn.Term.Equals(synonym))
return true;
}
return false;
}
}
}
作者:深潭
出处:http://www.cnblogs.com/dbasys/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/dbasys/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。