MSDN オフライン セミナー 「SharePoint開発のすすめ」
昨日、MSDN Offline セミナー 「SharePoint開発のすすめ」を開催しました。
たくさんの方にご参加いただきました。ありがとうございました。
セミナーのデモで使ったコードを紹介します。
これはWeb パーツのサンプルコードで、連絡先リストのアイテムを検索して結果を表示します。
==========================================================================
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using System.Collections.Generic;
namespace ExtensionSearch
{
[Guid("66ac8693-e4f6-427f-999a-08f3483a447b")]
public class ExtensionSearch : System.Web.UI.WebControls.WebParts.WebPart
{
private TextBox m_keywordTextBox = new TextBox();
private Button m_searchButton = new Button();
public ExtensionSearch()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.m_searchButton.Text = "検索";
this.m_searchButton.Click += new EventHandler(m_searchButton_Click);
this.Controls.Add(this.m_keywordTextBox);
this.Controls.Add(this.m_searchButton);
}
void m_searchButton_Click(object sender, EventArgs e)
{
// サイトの取得
SPWeb web = SPControl.GetContextWeb(this.Context);
// 内線番号リストを取得
SPListTemplateType type = SPListTemplateType.Contacts;
SPList list = this.GetListByType(web, type);
// アイテムを検索
string keyword = this.m_keywordTextBox.Text;
SPListItemCollection items = this.GetIems(list, keyword);
// 結果の表示
this.ShowResult(items);
}
private void ShowResult(SPListItemCollection items)
{
Table table = new Table();
this.Controls.Add(table);
foreach (SPListItem item in items)
{
string name = item.Title + " " + item["FirstName"].ToString();
string number = item["WorkPhone"].ToString();
string url = item.ParentList.ParentWebUrl
+ "/"
+ item.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url
+ "?ID=" + item.ID.ToString();
TableRow row = new TableRow();
table.Rows.Add(row);
TableCell titleCell = new TableCell();
HyperLink link = new HyperLink();
link.Text = name;
link.NavigateUrl = url;
titleCell.Controls.Add(link);
row.Cells.Add(titleCell);
TableCell numberCell = new TableCell();
Label label = new Label();
label.Text = number;
numberCell.Controls.Add(label);
row.Cells.Add(numberCell);
}
}
private SPListItemCollection GetIems(SPList list, string keyword)
{
// 入力値のチェック
// クエリの組み立て
SPQuery query = new SPQuery();
query.RowLimit = 0;
query.Query = "<Where>"
+ "<Or>"
+ "<Contains>"
+ "<FieldRef Name=\"Title\" />"
+ "<Value Type=\"Text\">"
+ keyword
+ "</Value>"
+ "</Contains>"
+ "<Contains>"
+ "<FieldRef Name=\"FirstName\" />"
+ "<Value Type=\"Text\">"
+ keyword
+ "</Value>"
+ "</Contains>"
+ "</Or>"
+ "</Where>";
// アイテムの取得
return list.GetItems(query);
}
private SPList GetListByType(SPWeb web, SPListTemplateType type)
{
foreach (SPList list in web.Lists)
{
if (list.BaseTemplate != type)
{
continue;
}
return list;
}
return null;
}
}
}
==========================================================================
コミュニティにおけるマイクロソフト社員による発言やコメントは、マイクロソフトの正式な見解またはコメントではありません。