首先是在任意位置显示网页库中网页的ITEM的webpart
公司动态和公司公告列表Page在只显示Title的情况先查询出来的栏
Title | _ModerationStatus(审批状态) | ID | FileLeafRef | Modified | CheckoutUser | DocIcon | Editor |
阿泰20分姚明错失扳平进球火箭87-89憾负奇才000 阿泰20分姚明错失扳平进球 火箭87-89憾负奇才000 | 0 | 1 | redirect.aspx | 2009-1-9 17:04:32 | INTRANET\Administrator | aspx |
下面的webpart代码,可以用来显示存放Page的列表库的内容,列表项是通过“标题.aspx”来查看的列表项都可以使用这个webpart来显示带链接的标题,也就是说列表中存放的都是网页,webpart可以放置在sharepoint网站的任务位置,通过设置网站集的URL、列表所在网站的URL、列表的名称、显示的条目数、列表是否启用了访问群体过滤,
Code
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Xml;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.ComponentModel;
namespace MainPage
{
[Guid("1fcf5f5a-846d-4ffa-a647-7860382d3939")]
public class MainPageWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
/// <summary>
/// 当前用户所在的SharePoint用户组
/// </summary>
private List<string> currUserGroup = null;
/// <summary>
/// 显示条目的gridview控件
/// </summary>
private GridView gridView = null;
public SPSite SP_Site
{
get
{
return SPContext.Current.Site;
}
}
public SPWeb SP_Web
{
get
{
return SPContext.Current.Web;
}
}
/// <summary>
/// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
/// </summary>
private string _isAccessed="2";
/// <summary>
/// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("是否对显示条目启用目标访问群体控制"),
WebDescription("请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1"),
DefaultValue("2")]
public string IsAccessed
{
get
{
return _isAccessed;
}
set
{
if (value.Equals("1") || value.Equals("2"))
this._isAccessed = value;
else
{
throw new Exception("请注意[是否对显示条目启用目标访问群体控制]的输入格式:\n输入1为启用,输入2为禁用");
}
}
}
private string _caml;
/// <summary>
/// 请输入Caml
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("Caml"),
WebDescription("请输入Caml")]
public string Caml
{
get
{
return _caml;
}
set
{
this._caml = value;
}
}
private string _siteName;
/// <summary>
/// 请输入网站集URL,格式形如:http://virus/sites/intranet
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("网站集URL"),
WebDescription("请输入网站集URL,格式形如:http://virus/sites/intranet")]
public string SiteName
{
get
{
//return "http://virus/sites/intranet";
return _siteName;
}
set
{
this._siteName = value;
}
}
private string _webName;
/// <summary>
/// 请输入网站URL,格式形如:team
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("网站URL"),
WebDescription("请输入网站URL,格式形如:team")]
public string WebName
{
get
{
// return "team";
return this._webName;
}
set
{
this._webName = value;
}
}
private string _listName;
/// <summary>
/// 请输入列表名称,格式形如:已发布动态
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("列表名称"),
WebDescription("请输入列表名称,格式形如:已发布动态")]
public string ListName
{
get
{
// return "已发布公告";
return this._listName;
}
set
{
this._listName = value;
}
}
private string _itemCount;
/// <summary>
/// 请输入要显示的条目数量,格式形如:10
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("显示条目数量"),
WebDescription("请输入要显示的条目数量,格式形如:10")]
public string ItemCount
{
get
{
return this._itemCount;
}
set
{
int count = 0;
if (Int32.TryParse(value, out count) && count < 100)
this._itemCount = value;
else
throw new Exception("请输入小于100的整数");
}
}
public MainPageWebPart()
{
currUserGroup = new List<string>();
}
/// <summary>
/// 获取当前用户所在的SharePoint用户组
/// </summary>
private void getUserSPGroup()
{
SPGroupCollection groups = SPContext.Current.Web.CurrentUser.Groups;
foreach (SPGroup group in groups)
{
currUserGroup.Add(group.Name);
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
getUserSPGroup();
gridView = new GridView();
Controls.Add(gridView);
EnsureChildControls();
if (!string.IsNullOrEmpty(SiteName) && !string.IsNullOrEmpty(WebName) &&
!string.IsNullOrEmpty(ListName) && !string.IsNullOrEmpty(ItemCount))
using (SPSite site = new SPSite(SiteName))
{
using (SPWeb web = site.OpenWeb(WebName))
{
SPList list = web.Lists[ListName];
SPQuery query = new SPQuery();
query.ViewFields = "<FieldRef Name='Title' />";
int count = 0;
int.TryParse(ItemCount, out count);
query.RowLimit = (uint)count;
if (IsAccessed.Equals("1"))
{
if (currUserGroup.Count > 0)
query.Query = @"<OrderBy>
<FieldRef Name='Modified' Ascending='False' />
</OrderBy>
<Where>
<And>
<And>
<Geq>
<FieldRef Name='Created' />
<Value Type='DateTime'>1980-01-12T00:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='Created' />
<Value Type='DateTime'>2109-01-12T00:00:00Z</Value>
</Leq>
</And>
<Contains>
<FieldRef Name='" + web.Lists[ListName].Fields["目标访问群体"].InternalName + @"' />
<Value Type='TargetTo'>" + currUserGroup[0] + @"</Value>
</Contains>
</And>
</Where>";
else
return;
}
else
{
query.Query = @"<OrderBy>
<FieldRef Name='Modified' Ascending='False' />
</OrderBy>
<Where>
<And>
<Geq>
<FieldRef Name='Created' />
<Value Type='DateTime'>1980-01-12T00:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='Created' />
<Value Type='DateTime'>2109-01-12T00:00:00Z</Value>
</Leq>
</And>
</Where>";
}
SPListItemCollection items=web.Lists[ListName].GetItems(query);
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("TitleLink", typeof(string)));
foreach (SPListItem item in items)
{
DataRow row = dt.NewRow();
row["Title"] = item.GetFormattedValue("Title");
row["TitleLink"] = SiteName + "/" + WebName + "/" + web.Lists[ListName].RootFolder + "/" +
item.GetFormattedValue("FileLeafRef");
dt.Rows.Add(row);
}
gridView.Columns.Clear();
gridView.AllowSorting = true;
gridView.AutoGenerateColumns = false;
gridView.ShowFooter = false;
gridView.ShowHeader = false;
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "Title";
hyperLinkField.DataNavigateUrlFields = new string[] { "TitleLink" };
hyperLinkField.DataNavigateUrlFormatString = "{0}";
gridView.Columns.Add(hyperLinkField);
gridView.DataSource = dt;
gridView.DataBind();
}
}
}
private void createGridView()
{
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "Title";
hyperLinkField.DataNavigateUrlFields = new string[] { "FileLeafRef" };
hyperLinkField.DataNavigateUrlFormatString = "{0}";
gridView.Columns.Add(hyperLinkField);
}
}
}
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Xml;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.ComponentModel;
namespace MainPage
{
[Guid("1fcf5f5a-846d-4ffa-a647-7860382d3939")]
public class MainPageWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
/// <summary>
/// 当前用户所在的SharePoint用户组
/// </summary>
private List<string> currUserGroup = null;
/// <summary>
/// 显示条目的gridview控件
/// </summary>
private GridView gridView = null;
public SPSite SP_Site
{
get
{
return SPContext.Current.Site;
}
}
public SPWeb SP_Web
{
get
{
return SPContext.Current.Web;
}
}
/// <summary>
/// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
/// </summary>
private string _isAccessed="2";
/// <summary>
/// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("是否对显示条目启用目标访问群体控制"),
WebDescription("请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1"),
DefaultValue("2")]
public string IsAccessed
{
get
{
return _isAccessed;
}
set
{
if (value.Equals("1") || value.Equals("2"))
this._isAccessed = value;
else
{
throw new Exception("请注意[是否对显示条目启用目标访问群体控制]的输入格式:\n输入1为启用,输入2为禁用");
}
}
}
private string _caml;
/// <summary>
/// 请输入Caml
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("Caml"),
WebDescription("请输入Caml")]
public string Caml
{
get
{
return _caml;
}
set
{
this._caml = value;
}
}
private string _siteName;
/// <summary>
/// 请输入网站集URL,格式形如:http://virus/sites/intranet
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("网站集URL"),
WebDescription("请输入网站集URL,格式形如:http://virus/sites/intranet")]
public string SiteName
{
get
{
//return "http://virus/sites/intranet";
return _siteName;
}
set
{
this._siteName = value;
}
}
private string _webName;
/// <summary>
/// 请输入网站URL,格式形如:team
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("网站URL"),
WebDescription("请输入网站URL,格式形如:team")]
public string WebName
{
get
{
// return "team";
return this._webName;
}
set
{
this._webName = value;
}
}
private string _listName;
/// <summary>
/// 请输入列表名称,格式形如:已发布动态
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("列表名称"),
WebDescription("请输入列表名称,格式形如:已发布动态")]
public string ListName
{
get
{
// return "已发布公告";
return this._listName;
}
set
{
this._listName = value;
}
}
private string _itemCount;
/// <summary>
/// 请输入要显示的条目数量,格式形如:10
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("显示条目数量"),
WebDescription("请输入要显示的条目数量,格式形如:10")]
public string ItemCount
{
get
{
return this._itemCount;
}
set
{
int count = 0;
if (Int32.TryParse(value, out count) && count < 100)
this._itemCount = value;
else
throw new Exception("请输入小于100的整数");
}
}
public MainPageWebPart()
{
currUserGroup = new List<string>();
}
/// <summary>
/// 获取当前用户所在的SharePoint用户组
/// </summary>
private void getUserSPGroup()
{
SPGroupCollection groups = SPContext.Current.Web.CurrentUser.Groups;
foreach (SPGroup group in groups)
{
currUserGroup.Add(group.Name);
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
getUserSPGroup();
gridView = new GridView();
Controls.Add(gridView);
EnsureChildControls();
if (!string.IsNullOrEmpty(SiteName) && !string.IsNullOrEmpty(WebName) &&
!string.IsNullOrEmpty(ListName) && !string.IsNullOrEmpty(ItemCount))
using (SPSite site = new SPSite(SiteName))
{
using (SPWeb web = site.OpenWeb(WebName))
{
SPList list = web.Lists[ListName];
SPQuery query = new SPQuery();
query.ViewFields = "<FieldRef Name='Title' />";
int count = 0;
int.TryParse(ItemCount, out count);
query.RowLimit = (uint)count;
if (IsAccessed.Equals("1"))
{
if (currUserGroup.Count > 0)
query.Query = @"<OrderBy>
<FieldRef Name='Modified' Ascending='False' />
</OrderBy>
<Where>
<And>
<And>
<Geq>
<FieldRef Name='Created' />
<Value Type='DateTime'>1980-01-12T00:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='Created' />
<Value Type='DateTime'>2109-01-12T00:00:00Z</Value>
</Leq>
</And>
<Contains>
<FieldRef Name='" + web.Lists[ListName].Fields["目标访问群体"].InternalName + @"' />
<Value Type='TargetTo'>" + currUserGroup[0] + @"</Value>
</Contains>
</And>
</Where>";
else
return;
}
else
{
query.Query = @"<OrderBy>
<FieldRef Name='Modified' Ascending='False' />
</OrderBy>
<Where>
<And>
<Geq>
<FieldRef Name='Created' />
<Value Type='DateTime'>1980-01-12T00:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='Created' />
<Value Type='DateTime'>2109-01-12T00:00:00Z</Value>
</Leq>
</And>
</Where>";
}
SPListItemCollection items=web.Lists[ListName].GetItems(query);
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("TitleLink", typeof(string)));
foreach (SPListItem item in items)
{
DataRow row = dt.NewRow();
row["Title"] = item.GetFormattedValue("Title");
row["TitleLink"] = SiteName + "/" + WebName + "/" + web.Lists[ListName].RootFolder + "/" +
item.GetFormattedValue("FileLeafRef");
dt.Rows.Add(row);
}
gridView.Columns.Clear();
gridView.AllowSorting = true;
gridView.AutoGenerateColumns = false;
gridView.ShowFooter = false;
gridView.ShowHeader = false;
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "Title";
hyperLinkField.DataNavigateUrlFields = new string[] { "TitleLink" };
hyperLinkField.DataNavigateUrlFormatString = "{0}";
gridView.Columns.Add(hyperLinkField);
gridView.DataSource = dt;
gridView.DataBind();
}
}
}
private void createGridView()
{
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "Title";
hyperLinkField.DataNavigateUrlFields = new string[] { "FileLeafRef" };
hyperLinkField.DataNavigateUrlFormatString = "{0}";
gridView.Columns.Add(hyperLinkField);
}
}
}
公司公告列表Item在只显示Title的情况先查询出来的栏
Title | Created | ID |
湖北随州8人被杀害 案情已上报公安部 | 2009-1-6 14:22:34 | 1 |
下面的webpart代码用来显示列表存放的内容是普通列表项的列表,列表项是通过DispForm.aspx?ID={ID}来查看的列表项都可以使用这个webpart来显示带链接的标题,webpart可以放置在sharepoint网站的任务位置,通过设置网站集的URL、列表所在网站的URL、列表的名称、显示的条目数、列表是否启用了访问群体过滤,
Code
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace MainPage
{
[Guid("75a37ce9-d2dd-4a8a-bf13-e3fd4ff291ef")]
public class MainPageItemWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
/// <summary>
/// 当前用户所在的SharePoint用户组
/// </summary>
private List<string> currUserGroup = null;
/// <summary>
/// 显示条目的gridview控件
/// </summary>
private GridView gridView = null;
public SPSite SP_Site
{
get
{
return SPContext.Current.Site;
}
}
public SPWeb SP_Web
{
get
{
return SPContext.Current.Web;
}
}
/// <summary>
/// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
/// </summary>
private string _isAccessed="2";
/// <summary>
/// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("是否对显示条目启用目标访问群体控制"),
WebDescription("请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1"),
DefaultValue("2")]
public string IsAccessed
{
get
{
return _isAccessed;
}
set
{
if (value.Equals("1") || value.Equals("2"))
this._isAccessed = value;
else
{
throw new Exception("请注意[是否对显示条目启用目标访问群体控制]的输入格式:\n输入1为启用,输入2为禁用");
}
}
}
private string _caml;
/// <summary>
/// 请输入Caml
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("Caml"),
WebDescription("请输入Caml")]
public string Caml
{
get
{
return _caml;
}
set
{
this._caml = value;
}
}
private string _siteName;
/// <summary>
/// 请输入网站集URL,格式形如:http://virus/sites/intranet
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("网站集URL"),
WebDescription("请输入网站集URL,格式形如:http://virus/sites/intranet")]
public string SiteName
{
get
{
//return "http://virus/sites/intranet";
return _siteName;
}
set
{
this._siteName = value;
}
}
private string _webName;
/// <summary>
/// 请输入网站URL,格式形如:team
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("网站URL"),
WebDescription("请输入网站URL,格式形如:team")]
public string WebName
{
get
{
// return "team";
return this._webName;
}
set
{
this._webName = value;
}
}
private string _listName;
/// <summary>
/// 请输入列表名称,格式形如:已发布动态
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("列表名称"),
WebDescription("请输入列表名称,格式形如:已发布动态")]
public string ListName
{
get
{
// return "已发布公告";
return this._listName;
}
set
{
this._listName = value;
}
}
private string _itemCount;
/// <summary>
/// 请输入要显示的条目数量,格式形如:10
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("显示条目数量"),
WebDescription("请输入要显示的条目数量,格式形如:10")]
public string ItemCount
{
get
{
return this._itemCount;
}
set
{
int count = 0;
if (Int32.TryParse(value, out count) && count < 100)
this._itemCount = value;
else
throw new Exception("请输入小于100的整数");
}
}
public MainPageItemWebPart()
{
currUserGroup = new List<string>();
}
/// <summary>
/// 获取当前用户所在的SharePoint用户组
/// </summary>
private void getUserSPGroup()
{
SPGroupCollection groups = SPContext.Current.Web.CurrentUser.Groups;
foreach (SPGroup group in groups)
{
currUserGroup.Add(group.Name);
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
getUserSPGroup();
gridView = new GridView();
Controls.Add(gridView);
EnsureChildControls();
if (!string.IsNullOrEmpty(SiteName) && !string.IsNullOrEmpty(WebName) &&
!string.IsNullOrEmpty(ListName) && !string.IsNullOrEmpty(ItemCount))
using (SPSite site = new SPSite(SiteName))
{
using (SPWeb web = site.OpenWeb(WebName))
{
SPList list = web.Lists[ListName];
SPQuery query = new SPQuery();
query.ViewFields = "<FieldRef Name='Title' />";
int count = 0;
int.TryParse(ItemCount, out count);
query.RowLimit = (uint)count;
if (IsAccessed.Equals("1"))
{
if (currUserGroup.Count > 0)
query.Query=@"<OrderBy>
<FieldRef Name='Modified' Ascending='False' />
</OrderBy>
<Where>
<And>
<And>
<Geq>
<FieldRef Name='Created' />
<Value Type='DateTime'>1980-01-12T00:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='Created' />
<Value Type='DateTime'>2109-01-12T00:00:00Z</Value>
</Leq>
</And>
<Contains>
<FieldRef Name='" + web.Lists[ListName].Fields["目标访问群体"].InternalName + @"' />
<Value Type='TargetTo'>" + currUserGroup[0] + @"</Value>
</Contains>
</And>
</Where>";
else
return;
}
else
{
query.Query = @"<OrderBy>
<FieldRef Name='Modified' Ascending='False' />
</OrderBy>
<Where>
<And>
<Geq>
<FieldRef Name='Created' />
<Value Type='DateTime'>1980-01-12T00:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='Created' />
<Value Type='DateTime'>2109-01-12T00:00:00Z</Value>
</Leq>
</And>
</Where>";
}
SPListItemCollection items = web.Lists[ListName].GetItems(query);
DataTable dt = new DataTable();
DataColumn Title = new DataColumn("Title", typeof(string));
DataColumn TitleLink = new DataColumn("TitleLink", typeof(string));
dt.Columns.Add(Title);
dt.Columns.Add(TitleLink);
foreach (SPListItem item in items)
{
DataRow row = dt.NewRow();
row[Title] = item.GetFormattedValue("Title");
row[TitleLink] = SiteName + "/" + WebName + "/" + web.Lists[ListName].RootFolder +
"/" + "DispForm.aspx?ID=" +
item.GetFormattedValue("ID") +
"&Source=" + SiteName + "/" + WebName + "/"; ;
dt.Rows.Add(row);
}
gridView.Columns.Clear();
gridView.AutoGenerateColumns = false;
gridView.AllowSorting = true;
gridView.ShowFooter = false;
gridView.ShowHeader = false;
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "Title";
hyperLinkField.DataNavigateUrlFields = new string[] { "TitleLink" };
hyperLinkField.DataNavigateUrlFormatString = "{0}";
gridView.Columns.Add(hyperLinkField);
gridView.DataSource = dt;
gridView.DataBind();
}
}
}
}
}
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace MainPage
{
[Guid("75a37ce9-d2dd-4a8a-bf13-e3fd4ff291ef")]
public class MainPageItemWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
/// <summary>
/// 当前用户所在的SharePoint用户组
/// </summary>
private List<string> currUserGroup = null;
/// <summary>
/// 显示条目的gridview控件
/// </summary>
private GridView gridView = null;
public SPSite SP_Site
{
get
{
return SPContext.Current.Site;
}
}
public SPWeb SP_Web
{
get
{
return SPContext.Current.Web;
}
}
/// <summary>
/// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
/// </summary>
private string _isAccessed="2";
/// <summary>
/// 请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("是否对显示条目启用目标访问群体控制"),
WebDescription("请输入是否对显示条目启用目标访问群体控制,输入1为启用,输入2为禁用,格式形如:1"),
DefaultValue("2")]
public string IsAccessed
{
get
{
return _isAccessed;
}
set
{
if (value.Equals("1") || value.Equals("2"))
this._isAccessed = value;
else
{
throw new Exception("请注意[是否对显示条目启用目标访问群体控制]的输入格式:\n输入1为启用,输入2为禁用");
}
}
}
private string _caml;
/// <summary>
/// 请输入Caml
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("Caml"),
WebDescription("请输入Caml")]
public string Caml
{
get
{
return _caml;
}
set
{
this._caml = value;
}
}
private string _siteName;
/// <summary>
/// 请输入网站集URL,格式形如:http://virus/sites/intranet
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("网站集URL"),
WebDescription("请输入网站集URL,格式形如:http://virus/sites/intranet")]
public string SiteName
{
get
{
//return "http://virus/sites/intranet";
return _siteName;
}
set
{
this._siteName = value;
}
}
private string _webName;
/// <summary>
/// 请输入网站URL,格式形如:team
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("网站URL"),
WebDescription("请输入网站URL,格式形如:team")]
public string WebName
{
get
{
// return "team";
return this._webName;
}
set
{
this._webName = value;
}
}
private string _listName;
/// <summary>
/// 请输入列表名称,格式形如:已发布动态
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("列表名称"),
WebDescription("请输入列表名称,格式形如:已发布动态")]
public string ListName
{
get
{
// return "已发布公告";
return this._listName;
}
set
{
this._listName = value;
}
}
private string _itemCount;
/// <summary>
/// 请输入要显示的条目数量,格式形如:10
/// </summary>
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true), WebDisplayName("显示条目数量"),
WebDescription("请输入要显示的条目数量,格式形如:10")]
public string ItemCount
{
get
{
return this._itemCount;
}
set
{
int count = 0;
if (Int32.TryParse(value, out count) && count < 100)
this._itemCount = value;
else
throw new Exception("请输入小于100的整数");
}
}
public MainPageItemWebPart()
{
currUserGroup = new List<string>();
}
/// <summary>
/// 获取当前用户所在的SharePoint用户组
/// </summary>
private void getUserSPGroup()
{
SPGroupCollection groups = SPContext.Current.Web.CurrentUser.Groups;
foreach (SPGroup group in groups)
{
currUserGroup.Add(group.Name);
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
getUserSPGroup();
gridView = new GridView();
Controls.Add(gridView);
EnsureChildControls();
if (!string.IsNullOrEmpty(SiteName) && !string.IsNullOrEmpty(WebName) &&
!string.IsNullOrEmpty(ListName) && !string.IsNullOrEmpty(ItemCount))
using (SPSite site = new SPSite(SiteName))
{
using (SPWeb web = site.OpenWeb(WebName))
{
SPList list = web.Lists[ListName];
SPQuery query = new SPQuery();
query.ViewFields = "<FieldRef Name='Title' />";
int count = 0;
int.TryParse(ItemCount, out count);
query.RowLimit = (uint)count;
if (IsAccessed.Equals("1"))
{
if (currUserGroup.Count > 0)
query.Query=@"<OrderBy>
<FieldRef Name='Modified' Ascending='False' />
</OrderBy>
<Where>
<And>
<And>
<Geq>
<FieldRef Name='Created' />
<Value Type='DateTime'>1980-01-12T00:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='Created' />
<Value Type='DateTime'>2109-01-12T00:00:00Z</Value>
</Leq>
</And>
<Contains>
<FieldRef Name='" + web.Lists[ListName].Fields["目标访问群体"].InternalName + @"' />
<Value Type='TargetTo'>" + currUserGroup[0] + @"</Value>
</Contains>
</And>
</Where>";
else
return;
}
else
{
query.Query = @"<OrderBy>
<FieldRef Name='Modified' Ascending='False' />
</OrderBy>
<Where>
<And>
<Geq>
<FieldRef Name='Created' />
<Value Type='DateTime'>1980-01-12T00:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='Created' />
<Value Type='DateTime'>2109-01-12T00:00:00Z</Value>
</Leq>
</And>
</Where>";
}
SPListItemCollection items = web.Lists[ListName].GetItems(query);
DataTable dt = new DataTable();
DataColumn Title = new DataColumn("Title", typeof(string));
DataColumn TitleLink = new DataColumn("TitleLink", typeof(string));
dt.Columns.Add(Title);
dt.Columns.Add(TitleLink);
foreach (SPListItem item in items)
{
DataRow row = dt.NewRow();
row[Title] = item.GetFormattedValue("Title");
row[TitleLink] = SiteName + "/" + WebName + "/" + web.Lists[ListName].RootFolder +
"/" + "DispForm.aspx?ID=" +
item.GetFormattedValue("ID") +
"&Source=" + SiteName + "/" + WebName + "/"; ;
dt.Rows.Add(row);
}
gridView.Columns.Clear();
gridView.AutoGenerateColumns = false;
gridView.AllowSorting = true;
gridView.ShowFooter = false;
gridView.ShowHeader = false;
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "Title";
hyperLinkField.DataNavigateUrlFields = new string[] { "TitleLink" };
hyperLinkField.DataNavigateUrlFormatString = "{0}";
gridView.Columns.Add(hyperLinkField);
gridView.DataSource = dt;
gridView.DataBind();
}
}
}
}
}