当前位置: 代码迷 >> 综合 >> microsoft.sharepoint.webpartpages.webpart by dwp file configuration
  详细解决方案

microsoft.sharepoint.webpartpages.webpart by dwp file configuration

热度:53   发布时间:2023-12-08 02:15:29.0

WebPart 对于学习和研究SharePoint的人,已经不是什么新鲜的概念了。但对于学习.net Framework的人,可能会发现在System.web.ui.webcontrols下会发现多了WebPart的踪影。WebPart作为 WebControl中的一种,比WebControl更高级一些,它可以在线Drag and Drop,并可以在线设置它的属性。 以前利用 VS2003开发WebPart,得安装WebPart Template For VSNet。当然也可以在VS2005下安装WebPart Template来开发WebPart。在这里我就不介绍用WebPart Template For VSNet来开发WebPart了。 在VS2005下就可以不用安装WebPart Template来开发WebPart。下面我具体介绍其过程: 1、创建 Web Control Library 首先浏览C# Project Templates,然后选择Web Control Library,输入“SampleControl”。 要开发WebPart用于SharePoint,就必须引用Microsoft.SharePoint.dll(必须是安装MOSS的服务器上的)。 最后添加引用如下图: 2、编写WebPart的代码 你开发的WebPart,根据自己开发的功能选择相应的SP(SharePoint)命名空间,具体命名空间,请参考SDK。 开发WebPart必须继承WebPart类。

// -------------------------------------------------------------------- //  File: SimpleWebPart.cs // //  Purpose: A sample Web Part that demonstrates how to create a basic //  Web Part. // -------------------------------------------------------------------- using  System; using  System.ComponentModel; using  System.Runtime.InteropServices; using  System.Web.UI; using  System.Web.UI.WebControls; using  System.Xml.Serialization; using  Microsoft.SharePoint; using  Microsoft.SharePoint.WebPartPages; using  Microsoft.SharePoint.Utilities; using  System.Web.UI.HtmlControls; namespace  Xdian.WebParts.SampleControl {     /// <summary>     /// This Web Part changes it's own title and implements a custom property.     /// </summary>     [XmlRoot(Namespace = "MyWebParts")]     public class SimpleWebPart : WebPart     {         private const string defaultText = "hello";         private string text = defaultText;         // Declare variables for HtmlControls user interface elements.         HtmlButton _mybutton;         HtmlInputText _mytextbox;         // Event handler for _mybutton control that sets the         // Title property to the value in _mytextbox control.         public void _mybutton_click(object sender, EventArgs e)         {             this.Title = _mytextbox.Value;             try             {                 this.SaveProperties = true;             }             catch             {                 Caption = "Error Could not save property.";             }         }         // Override the ASP.NET Web.UI.Controls.CreateChildControls          // method to create the objects for the Web Part's controls.               protected override void CreateChildControls()         {             // Create _mytextbox control.             _mytextbox = new HtmlInputText();             _mytextbox.Value = "";             Controls.Add(_mytextbox);             // Create _mybutton control and wire its event handler.             _mybutton = new HtmlButton();             _mybutton.InnerText = "Set Web Part Title";             _mybutton.ServerClick += new EventHandler(_mybutton_click);             Controls.Add(_mybutton);         }         [Browsable(true), Category("Miscellaneous"),         DefaultValue(defaultText),         WebPartStorage(Storage.Personal),         FriendlyName("Text"), Description("Text Property")]         public string Text         {             get             {                 return text;             }             set             {                 text = value;             }         }         protected override void RenderWebPart(HtmlTextWriter output)         {             RenderChildren(output);             // Securely write out HTML             output.Write("<BR>Text Property: " + SPEncode.HtmlEncode(Text));         }     } }

3、编写WebPart定义文件(.dwp) 这个文件是xml文件,任意文本文件更改其扩展名为.dwp就可以了。 我的WebPart的程序集文件为SampleControl.dll。命名空间为Xdian.WebParts.SampleControl。所以我的SampleControl.dwp文件如下:

<? xml version = " 1.0 " ?> < WebPart xmlns = " http://schemas.microsoft.com/WebPart/v2 " >     < Assembly > SampleControl, Version = 1.0 . 0.0 , Culture = neutral,PublicKeyToken = 0e79ac0ff7e9e2cb </ Assembly >     < TypeName > Xdian.WebParts.SampleControl.SimpleWebPart </ TypeName >     < Title > SampleWebPart </ Title >     < Description > Chatterley Create The first WebPart </ Description > </ WebPart >

4、生成强名称程序集合 在这点,我在VS2005下,没有找到解决方法,因为在C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin下没有sn.exe这个工具了。 我跑到VS2003的机子下,在cmd下浏览到VS2003下的bin目录。用sn.exe随机生成了一个key.snk。输入sn.exe -h可以看参数。然后在项目文件下的Assembly.cs下加入这么一行文字: [assembly: AssemblyKeyFile(@"C:/key.snk")] 我是把key.snk放在C:盘下的。最后生成的程序集,可以通过sn.exe -Tp来参看其PublicKeyToken。我采用的方法是用gacutil.exe放入C:/Windows/Assembly下,然后看去属性中的PublicKeyToken。 最终生成的程序集和配置文件如下: 5、修改WebConfig。 我想大家肯定会在那么多的Config混乱,你就把它放入你想添加WebPart到的SharePoint网站下。如下我把它放如8000端口的网站下(某个GUID网站下)。 当然你也可以把你的WebPart放如全局上,那么你就得修改C:/Program Files/Common Files/Microsoft Shared/web server extensions下的相应文件(不推荐)。 在WebConfig下,添加如下的配置信息:

< SafeControls > < SafeControl Assembly = " SampleControl, Version=1.0.0.0, Culture=neutral,PublicKeyToken=0e79ac0ff7e9e2cb "      Namespace = " Xdian.WebParts.SampleControl "      TypeName = " * "   Safe = " True " /> </ SafeControls >
6、添加WebPart到网站的WebPart Gallery中。 你必须要有网站管理员的权限。进入 http://vsts01:8000/下的网站管理。在WebPart管理的列表中,点击“上载”。浏览到我的SampleControl.dwp,然后输入配置信息,上传。最后在WebPart Gallery显示一条信息如下: 7、把WebPart添加到网页显示。 进入编辑网页。找到自己的SampleControl,然后添加(和SPS2003的操作有所不同)。 最后终于把WebPart显示在自己的网站上,不过不要开发太多的WebPart,网站对WebPart的承受能力是有限的。一个网站大概可以承受100个WebPart左右。如果你的网站超过的话,那么你的网站速度会明显变慢。
  相关解决方案