设为首页 收藏本站
查看: 809|回复: 0

[经验分享] SPGridView and SPMenuField: Displaying custom data through SharePoint lists

[复制链接]

尚未签到

发表于 2017-5-24 10:40:51 | 显示全部楼层 |阅读模式
原文链接

http://blogs.msdn.com/b/powlo/archive/2007/02/25/displaying-custom-data-through-sharepoint-lists-using-spgridview-and-spmenufield.aspx
  Paul Robinson

25 Feb 2007 5:30 PM

 
   

Part 1: Using SPGridview, adding menus, grouping and sorting

Click along to build this lovely Web Part, taking data directly from a standard .NET DataSet:
 

DSC0000.png
 
SharePoint lists and libraries are great for storing almost everything, but what about if you need to display structured lists in SharePoint where the data is stored elsewhere? If you use Office SharePoint Server (MOSS), one great feature for this purpose is the Business Data Catalogue. However, if you only have Windows SharePoint Services (WSS), or need to dynamically construct data, chances are you'll end up needing to write a Web Part.
Web Parts give you some standard look and feel elements for free, like the ‘crome' or border, plus an ability to add your own properties to the properties grid. After that, you're pretty much on your own. Wouldn't it be nice if you could display your own data in a sexy SharePoint list?
SharePoint uses the Microsoft.SharePoint.WebControls.SPGridView control to display its own lists. This class inherits from System.Web.UI.WebControls.GridView, so the development experience to bind data, adjust columns, perform sorting etc is similar. The key difference is the control renders the grid in the SharePoint style - perfect.
Create a Web Part
Any blank Web Part will do - you could use the template provided by Visual Studio if you have the SharePoint SDK installed. My web part started out life like this:
 

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace ListMenuSample
{
public class ListMenuSample : System.Web.UI.WebControls.WebParts.WebPart
{
private SPGridView oGrid;
private TVData oDataset;
private DataView oView;
private void PopulateDataset()
{
// TODO
}
protected override void CreateChildControls()
{
// TODO
}
void oGrid_Sorting(object sender, GridViewSortEventArgs e)
{
// TODO
}
}
}



 
  Create the data source
  In this example, we'll create and populate a dataset and use it for data binding. I want a list to keep track of who presents my favourite BBC current affairs programmes, so I'll design and populate a DataSet accordingly.
  If you want to bind your SPGridView back to SharePoint data, that's simple too, check out this great article from Share This Point.
  My DataSet, TVProgrammeData has a single table, Presenters, comprising an int and two string columns:
  
DSC0001.png
 
  We'll fill the DataTable from code, but obviously you'd want to pull this from somewhere, probably SQL, a filesystem, a web service or XML. Pop this into the PopulateDataset() method.

private void PopulateDataset()
{
oDataset = new TVData();
oDataset.Presenters.AddPresentersRow(1, "Jeremy Paxman", "Newsnight");
oDataset.Presenters.AddPresentersRow(2, "Kirsty Wark", "Newsnight");
oDataset.Presenters.AddPresentersRow(6, "Bill Turnbull", "Breakfast");
oDataset.Presenters.AddPresentersRow(7, "Sian Williams", "Breakfast");
// plus a few more entries
}
  Render the Grid
  Overriding CreateChildControls() is a good place to create your SPGridView and add it to the controls collection. You'll also need to bind up the columns and specify sorting. To give us magical sorting abilities, we'll bind to a DataView rather than directly back to the DataTable. Pop this into CreateChildControls():
  

protected override void CreateChildControls()
{
PopulateDataset();
oView = new DataView(oDataset.Presenters);
oGrid = new SPGridView();
oGrid.DataSource = oView;
oGrid.AutoGenerateColumns = false;
oGrid.AllowSorting = true;
oGrid.Sorting += new GridViewSortEventHandler(oGrid_Sorting);
BoundField colName = new BoundField();
colName.DataField = "PresenterName";
colName.HeaderText = "Presenter Name";
colName.SortExpression = "PresenterName";
oGrid.Columns.Add(colName);
// Add the menu control here
BoundField colProgramme = new BoundField();
colProgramme.DataField = "ProgrammeName";
colProgramme.HeaderText = "Programme";
colProgramme.SortExpression = "ProgrammeName";
oGrid.Columns.Add(colProgramme);
Controls.Add(oGrid);
oGrid.DataBind();
base.CreateChildControls();
}

  Notice we specify the SortExpression to use, which together with AllowSorting enables users to order the results by clicking the columns headers. We need to perform the sort ourselves though, through the event handler; and we'll need to keep track of the sort direction in ViewState so we can flip it next time the user clicks the same header. I'm not sure my code is very elegant in this area, so leave a comment if you can think of a better way to do it in fewer lines of code.
  Add this event handler:

void oGrid_Sorting(object sender, GridViewSortEventArgs e)
{
string lastExpression = "";
if (ViewState["SortExpression"] != null)
lastExpression = ViewState["SortExpression"].ToString();
string lastDirection = "asc";
if (ViewState["SortDirection"] != null)
lastDirection = ViewState["SortDirection"].ToString();
string newDirection = "asc";
if (e.SortExpression == lastExpression)
newDirection = (lastDirection == "asc") ? "desc" : "asc";
ViewState["SortExpression"] = e.SortExpression;
ViewState["SortDirection"] = newDirection;
oView.Sort = e.SortExpression + " " + newDirection;
oGrid.DataBind();
}

  If you build and deploy this web part, you should get something like this (see this post for tips on the debugging process):
  
DSC0002.png
 
  That looks alright, and it will adapt correctly if you apply different style sheets, themes or a new master page. But it's still not a very rich interface. How about if you wanted users to edit items, or get more detail. Umm, better add a menu.
  Add a menu
  It's worth pointing out about now that the documentation around this area is still in production - so I'm coding with a slight emphasis on experimentation for some of the property values - I'll point you to the official source when it's revised.
  Anyway, SPMenuField is the class we need, and combines the roles of controlling the drop-down menu with the basic display work done by BoundField. Let's replace our boring colName column with a shiny menu that looks like this:
  
DSC0003.png
 

// Replace the Name coloumn with a shiny menu
colName.Visible = false;  // You could remove colName completely
SPMenuField colMenu = new SPMenuField();
colMenu.HeaderText = "Presenter Name";
colMenu.TextFields = "PresenterName";
colMenu.MenuTemplateId = "PresenterListMenu";
colMenu.NavigateUrlFields = "ID,PresenterName";
colMenu.NavigateUrlFormat = "do.aspx?p={0}&q={1}";
colMenu.TokenNameAndValueFields = "EDIT=ID,NAME=PresenterName";
colMenu.SortExpression = "PresenterName";
MenuTemplate presenterListMenu = new MenuTemplate();
presenterListMenu.ID = "PresenterListMenu";
MenuItemTemplate biogMenu = new MenuItemTemplate(
"Read Biography", "/_layouts/images/EawfNewUser.gif");
biogMenu.ClientOnClickNavigateUrl = "do.aspx?this=%EDIT%&that=%NAME%";
//entry.ClientOnClickScript = "your javascript here";
presenterListMenu.Controls.Add(biogMenu);
MenuItemTemplate broadcastMenu = new MenuItemTemplate(
"Recent Broadcasts", "/_layouts/images/ICWM.gif");
presenterListMenu.Controls.Add(broadcastMenu);
MenuSeparatorTemplate sepMenu = new MenuSeparatorTemplate();
presenterListMenu.Controls.Add(sepMenu);
MenuItemTemplate favMenu = new MenuItemTemplate(
"Add to Favorites", "/_layouts/images/addtofavorites.gif");
presenterListMenu.Controls.Add(favMenu);
this.Controls.Add(presenterListMenu);
oGrid.Columns.Add(colMenu);

  Tip: You can have a poke around the standard icon collection and pick some suitable images from C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\IMAGES
  SPMenuField serves two purposes - it configures the hyperlink you follow if you click on the item directly and, optionally, links to a snazzy dropdown menu.
  This great post at SharePoint Solution Blog gives a good overview of how to extend the Admin interface with custom menus. However, the difference with custom menus on list items is that you're not creating one - you're creating one for each list item, and your menu handling routine needs to know which item you clicked in.
  Fortunately, there are a few ways to achieve with. First we setup colMenu to pass in the required parameters when you click on the main item hyperlink:
  
DSC0004.png
 
  Here, NavigateUrlFields is a comma-separated list of data bound items we want to use in the URL. Then we replace placeholders starting at {0} with the items in sequence.
  
DSC0005.png
 
  Next, we need to decide how to respond to clicks on the drop-down menu. We have two options here - build a click URL similar to the one above, or use our own javascript. We might have a third option to do a sexy postback to an event in the code behind, but I can't decipher exactly how to use that yet - keep tuned.
  The URL way uses a modification in syntax but essentially the same principle as above. This time we name the data fields we want and then consume them within % signs on the menu items:
  
DSC0006.png
 
  Let's finish off for now by adding some grouping (and a few more presenters):

oGrid.AllowGrouping = true;
oGrid.AllowGroupCollapse = true;
oGrid.GroupField = "ProgrammeName";
oGrid.GroupDescriptionField = "ProgrammeName";
oGrid.GroupFieldDisplayName = "Programme";

  
DSC0007.png
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-380404-1-1.html 上篇帖子: Exchange与Sharepoint用户权限冲突的解决办法(转) 下篇帖子: 两种SharePoint 身份验证的选择:Kerberos .vs. NTLM
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表