1. 创建一个简单的webpart
在vs2008中建立一个class,该class在添加引用中引用一个System.web,然后再引用个System.Web.UI.WebControls.WebParts的命名空间,该类需要继承一个该命名空间下的webpart类,
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
namespace Liang.WebParts.Test1
{
/**//// <summary>
/// Symbio,PSD-LiangLiang
/// </summary>
public class Instances : WebPart
{
private string _text = "this is my webpark test!";
public string test
{
get { return _text; }
set { _text = value;}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write(_text);
}
}
}
任意输出一句话,将该class编译成dll文件,将编译好的该dll放入系统盘sharepoint的路径 Inetpub\wwwroot\wss\VirtualDirectories\9998,9998为你创建sharepoint网站时的端口,找到你自己端口号下的web.config文件,在类似
<SafeControl Assembly="liang.event.test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Namespace="liang.eventclass.test" TypeName="eventclass" Safe="True"/>
的设置中加入上面一段代码,Assembly里填写为dll名称,Version=1.0.0.0, Culture=neutral几乎固定不变,PublicKeyToken为当前dll加密的key,目前发现可以不加密使用,Namespace为dll的命名空间名,TypeName为类名,Safe="True"为固定值。
注意:(根据别人的案例,当完成Webpart class方面后,为当前的Webpart创建了一个强名称,同时,在添加sharepoint中webconfig文件之前,需要借助于第三方软件,如reflector等反编译软件,对生成的webpart的dll进行解析,同时会产生对应的PublicKeyToken值,包括Assembly中所有信息,必须完全填写正确,否则后面基于sharepoint的操作将无法继续)
这样vs2008方面操作基本完成,将生成的dll文件copy到与上面提到的sharepoint下web.config同级目录下的_app_bin文件夹中。
在sharepoint主页的site actions下的site settings中的Modify all site setting 转到 Site setting 页面,点击Galleries列表下的Web parts,在这个页面中新建,你将能找到你之前所加的自己写的web part的名字,将你自己加的web part选中,然后Populate Gallery,你的web part就已经加入到你自己的share point里了,返回你的首页,在set actions里第一项,编辑你的页面,点击Add a Web Part,选中你自己的web part类名,add后,一个简单的webpart就添加完成!
2.带事件的webpart以及变相使用
方法一:由于你个整个webpart类要和sharepoint整合起来,在编写带事件的webpart类时,还需继承一个IPostBackEventHandler(服务器事件类),带事件一般就带有控件,就还需要引用更多的web下的命名空间。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using liang.webparts.demo;
using System.Web.UI.WebControls;
namespace liang.eventclass.test
{
/**//// <summary>
/// Symbio,PSD-LiangLiang
/// </summary>
public class eventclass : WebPart, IPostBackEventHandler
{
private ArrayList al = new ArrayList();
private HtmlTableCell htc = new HtmlTableCell();
private DropDownList ddl = new DropDownList();
public eventclass()
{
addArray();
this.ddl.AutoPostBack = true;
this.ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
RaisePostBackEvent(ddl.SelectedItem.ToString());
}
}
protected override void CreateChildControls()
{
this.Controls.Add(new LiteralControl("<table>" + "\n"));
this.Controls.Add(new LiteralControl("<tr>" + "\n"));
this.Controls.Add(htc);
this.Controls.Add(new LiteralControl("</tr>" + "\n"));
this.Controls.Add(new LiteralControl("</table>" + "\n"));
if (!this.Page.IsPostBack)
{
this.addControl();
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Page.IsPostBack)
{
this.CreateChildControls();
}
}
private void addArray()
{
al.Add("team1");
al.Add("team2");
al.Add("team3");
al.Add("team4");
al.Add("team5");
}
private void addControl()
{
for (int i = 0; i < al.Count; i++)
{
ddl.Items.Add(al[i].ToString());
}
htc.Controls.Add(new LiteralControl("<table>" + "\n"));
htc.Controls.Add(new LiteralControl("<tr>" + "\n"));
htc.Controls.Add(ddl);
//htc.Controls.Add(new LiteralControl("<td style=\"cursor.hand\" οnclick=\""+this.Page.ClientScript.GetPostBackEventReference(this,al.ToString()+"\">"+this.al.ToString())+"</td>" + "\n"));
htc.Controls.Add(new LiteralControl("</tr>" + "\n"));
htc.Controls.Add(new LiteralControl("</table>" + "\n"));
}
public void RaisePostBackEvent(string eventArgument)
{
for (int i = 0; i < WebPartManager.WebParts.Count; i++)
{
if (WebPartManager.WebParts[i].Title == "webparkclass")
{
webparkclass wpc = (webparkclass)WebPartManager.WebParts[i];
wpc.test = eventArgument;
}
}
}
}
}
同时,在webpart类中,还能画表格,随意添加控件,这些经过测试都是可以的,如果想要在一个页面中加载2个webpart,并且让2个webpart相互关联,这需要带事件的webpart引用另一个webpart,该webpart用来显示你所选中的list,参考上面的webpart显示,当然,2个webpart都需要像上面所讲的添加到sharepoint服务上,这样,在webpart上能实现很多功能!
方法二:在同一个项目中创建2个以及更多的相关联的webpart。以2个webpart为例,编写你所需要的相互关联事件,显示的控件,同时添加一个相互交互的类库,该类库创建对象,用于2个webpart传递数据。Demo为输入webpart1 输入string 提交,在webpart2中显示出来,所以webpart1 中提交方法需要加上ConnectionProviderAttribute的属性修饰,为以后sharepoint添加webpart之间关联使用。下面Code分在三个类中编写:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
namespace liang.mywebpart
{
/**//// <summary>
/// Symbio,PSD-LiangLiang
/// </summary>
public class setText : WebPart
{
private TextBox tb=null;
protected override void CreateChildControls()
{
tb = new TextBox();
tb.ToolTip = "test";
Button bt = new Button();
bt.Text = "Submit";
this.Controls.Add(tb);
this.Controls.Add(bt);
base.CreateChildControls();
}
[ConnectionProvider("return message")]
public ITextMessage returnMessage()
{
this.EnsureChildControls();
ITextMessage it = new TextMessage();
it.strMessage = tb.Text;
return it;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
namespace liang.mywebpart
{
/**//// <summary>
/// Symbio,PSD-LiangLiang
/// </summary>
//一个传递文本信息的接口
public interface ITextMessage
{
string strMessage { get; set; }
}
//定义一个类来实现上面的接口
public class TextMessage : ITextMessage
{
private string _strmessage = default(string);
public string strMessage
{
get { return _strmessage; }
set { _strmessage = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Web.UI.HtmlControls;
namespace liang.mywebpart
{
public class WebPartClass : WebPart
{
/**//// <summary>
/// Symbio,PSD-LiangLiang
/// </summary>
private string _strtext = "";
private mycolor _mc = mycolor.Black;
protected override void RenderContents(HtmlTextWriter writer)
{
string showtext=this.Page.Server.HtmlEncode(this.strtext);
writer.Write("<font color=\"" +this.mc.ToString()+"\">"+ showtext +"</font>");
}
[Personalizable]
[WebBrowsable]
[WebDisplayName("strtext")]
[WebDescription("return text")]
public string strtext
{
get { return _strtext; }
set { _strtext = value; }
}
[Personalizable]
[WebBrowsable]
[WebDisplayName("mc")]
[WebDescription("return color")]
public mycolor mc
{
get { return _mc; }
set { _mc = value; }
}
public enum mycolor
{
Red,
Green,
Black,
Blue,
White,
Yellow
}
[ConnectionConsumer("receive text")]
public void receiveTextMessage(ITextMessage it)
{
if (it != null && it.strMessage != null)
{
this.strtext = it.strMessage;
}
}
}
}
同理,webpart2 显示方法也需要加上ConnectionConsumerAttribute的属性修饰。完成编译后,以上述方法加载到sharepoint中,将带事件的webpart点击编辑,选择连接,选择webpart2中ConnectionConsumerAttribute 所修饰的连接,完成该webpart的编写。效果等同方法一。
方法一和方法二区别在于实现连接的方式不同,效果等同。目前而言,方法二更符合sharepoint的编程方式。
关于sharepoint的自定义webpart:在webpart上预设好接收的对象属性,以及属性修饰,当加载webpart到sharepoint中后,编辑里选择 修改共享webpart,找到你创建的属性修饰名称,输入后,你webpart将显示你输入的对应值。