惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
N
News | PayPal Newsroom
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
B
Blog
Recorded Future
Recorded Future
U
Unit 42
J
Java Code Geeks
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Scott Helme
Scott Helme
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
T
Threatpost
Last Week in AI
Last Week in AI
Know Your Adversary
Know Your Adversary
Project Zero
Project Zero
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
NISL@THU
NISL@THU
有赞技术团队
有赞技术团队
博客园 - 叶小钗
N
News and Events Feed by Topic
V
V2EX
T
Troy Hunt's Blog
月光博客
月光博客
博客园 - Franky
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
C
Cisco Blogs
The Cloudflare Blog
T
Tor Project blog
Google Online Security Blog
Google Online Security Blog

博客园 - 抽烟的猫

MS SQL开发经典 听许巍 年终总结里的建议 实验部分类的代码 在蹉跎中一路前行(转自Eric Liu(刘如鸿)) javascript小技巧 面向对象的Jscript---转自经典论坛中桃花岛主的文章 在Javascript中使用面向对象的编程(转载) JavaScript中的类继承(翻译 ShiningRay)转自nirvanastudio Ioc模式(又称DI:Dependency Injection)(转载) JBoss 5迎来中间件彻底的可配置时代(转载) Spring AOP(转载) [心情驿站]喝一碗孟婆汤,走一遍奈何桥(flash版)(转载) Castle实践8-AspectSharp (转自叶子) AOP与权限控制实现(转自板桥里人) AOP和AspectJ(转自板桥里人) AOP(转自板桥里人) 今天你多态了吗? 如何在WebPart中访问页面上的其他WebPart (转kaneboy)
学习开发webpart
抽烟的猫 · 2006-08-25 · via 博客园 - 抽烟的猫

一、为什么用webpart
        和普通的.net控件相比,webpart有以下优点:

    1.易组织可复用的页面板块,即插即用的友好用户体验
用过google的朋友都知道,google可以为每一个用户提供自定义布局的功能,同样,在支持web部件的页面中,每一个webpart都可以被设计为板块被管理者或用户通过拖拽的方式来展示和设置
    2.可存储的属性为用户提供个性化设置
 在webpart中,每个属性都可以被指定为共享属性或私人化属性存储起来,点击属性窗口的确定或应用,属性就会被保存,这样,当用户对同一个部件有不同需求时,只要更改私人属性设置就可以看到不一样的webpart了
    3.工具组件
 如果希望开发更强的设置,可以用工具组件来实现,我们甚至可以隐藏掉所有的webpart默认属性,这样我们不会受到webpart属性本身的限制
    4.组件之间的数据连接
 如果在一个网页甚至不同网页中,我们希望一个部件可以为另一个部件提供数据,用webpart就可以更简单的实现
二、web部件开发
    1.开发环境
     .net 2003
     microsoft windows sharePoint services
     webpart开发模板
     cabinet manager 2003(部署时用)
    2.建立webpart项目
     在.net2003中选择新建项目-〉c#项目-〉Web Part Library模板
     新建的项目中包含4个文件:AssemblyInfo.cs、Manifest.xml、WebPart1.cs、WebPart1.dwp,其中WebPart1.cs是主程序文件,下面3-5种主要讲述如何在WebPart1.cs中为webpart创建属性,生成展示的html部分
    3.创建属性LoadMode
 打开WebPart1.cs
 定义属性
  [Browsable(false),
  Category(CategoryName),
  DefaultValue("2"),
  WebPartStorage(Storage.Personal),
  FriendlyName("读取方式"),
  Description("读取方式")]
  public string LoadModel
  {
   get
   {
    return loadModel;
   }

   set
   {
    loadModel = value;
   }
  }
 其中,Browsable表示这个属性是否显示,Category为分类名称,DefaultValue是第一次打开webpart时属性的默认值,WebPartStorage是属性的存取方式(共享属性或个人属性),FriendlyName为属性显示的名称,Description是属性的提示信息
 注:当属性为枚举类型时,在属性窗口中会产生一个下拉框,但枚举类型是静态的数据,如果想在属性设置页面中添加的下拉框是动态数据,需要建立toolpart实现
     当属性为bool类型时,在属性窗口中会产生一个复选框

    4.创建子控件
  protected override void CreateChildControls()
  {
   this.expand_butt = new Button();
   this.openFolderUrl = new TextBox();
   this.expand_butt.Attributes.Add("id","reload");
   this.expand_butt.Attributes.Add("style","display:none;");
   this.openFolderUrl.Attributes.Add("id","FolderUrl");
   this.openFolderUrl.Attributes.Add("style","display:none");
   Controls.Add(this.expand_butt);
   Controls.Add(this.openFolderUrl);
  }
    5.产生webpart的html部分
  protected override void RenderWebPart(HtmlTextWriter output)
  {
   this.expand_butt.RenderControl(output);
   this.openFolderUrl.RenderControl(output);  
   html.Append("<script>var eicon='"+this.ExpandIcon+"';var cicon='"+this.CollapseIcon+"';</script>");
   html.Append("<LINK href='"+this.CssFilePath+"' type='text/css' rel='stylesheet'>");
   html.Append("<script src='"+this.jsFilePath+"'></script>");
   output.Write(html)
  }
    6.添加toolpart
 右键点击项目,选择添加一个toolpart,会在项目中添加一个toolpart1.cs文件
 toolpart.cs的构造文件:
  public MyToolPart()
  {
   this.Title = "请选择文档库:";

  }
 toolpart.cs的ApplyChanges方法(当网页属性页点击应用后执行)
  public override void ApplyChanges()
  {
   
   //((SPSTree)this.ParentToolPane.SelectedWebPart).CssFilePath = this.docList.SelectedValue ;
   //((SPSTree)this.ParentToolPane.SelectedWebPart).CssFilePath = this.docList.SelectedValue.Trim();
   ((SPSTree)this.ParentToolPane.SelectedWebPart).DocGuid = this.docList.SelectedValue.Trim();
   //((SPSTree)this.ParentToolPane.SelectedWebPart).LoadModel = this.loadModel.SelectedValue.Trim();

   // Apply property values here.
  }
 创建子控件
  protected override void CreateChildControls()
  {
   this.docList = new DropDownList();
   
   this.Controls.Add(this.docList);
  }
 绘制toolpart
  protected override void RenderToolPart(HtmlTextWriter output)
  {
   output.Write("<span>请选择文档库</span>");
   docList.RenderControl(output);
  }
 在webpart中添加toolpart:(注意,该方法并没有产生默认的webpart属性)
  public override ToolPart[] GetToolParts()
  {
   ToolPart[] toolparts = new ToolPart[2];
   CustomPropertyToolPart custom = new CustomPropertyToolPart();
   toolparts[1] = custom;
   toolparts[0] = new MyToolPart();   //这里并没有使用webpart默认的属性

,而是直接用自己做的toolpart
   
   return toolparts;
  }
7.建立强名
 使用强名工具,在c盘创建包含公钥和私药的密钥文件spstree.snk
  打开.net命令提示,输入: sn -k c:\MyKeyFile.snk
 打开sn -k MyKeyFile.snk
 修改如下:
  [assembly: AssemblyVersion("1.0.0.0")]   //版本号
  [assembly: AssemblyDelaySign(false)] //不允许延迟签名,即不允许在分发部署

时才使用私钥签名
  [assembly: AssemblyKeyFile("c:\\spstree.snk")]   //指定密钥文件,为组件签名

    8.修改WebPart1.dwp
 <?xml version="1.0" encoding="utf-8"?>
 <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
  <Title>文档树</Title>
  <Description>1.0版本</Description>
  <Assembly>SPSTree</Assembly>
  <TypeName>Resoft.SharePoint.WebPart.SPSTree</TypeName>
  <!-- Specify initial values for any additional base class or custom

properties here. -->
 </WebPart>
 其中,title:webpart显示的标题
  Description:提示信息
  Assembly:程序集名称
  TypeName:完整的类名(带命名控件)
 在开发过程中,可以先开发一个最简单的webpart部署上去,成功后把项目的输出路径设为站点

根目录\bin,这样每次修改后重新生成的dll直接覆盖了bin目录下的,直接刷新包含webpart的页面就能看

到结果了:)
三、怎么简化webpart的部署
这里的方法不一定是最好的方法,却是我能找到的最简单的方法了,没办法嘛,我就是个懒人^ ^
1.修改Manifest.xml
 如果按照普通的部署方式,webpart需要经过一系列的部署才能导入到sps部件库中,如果使用

cab部署就会简单很多,而在使用cab打包时,Manifest.xml是最关键的一步
 这是我的spstree部件中的Manifest.xml:
 
 <?xml version="1.0"?>
<!-- You need only one manifest per CAB project for Web Part Deployment.-->
<!-- This manifest file can have multiple assembly nodes.-->
<WebPartManifest xmlns="http://schemas.microsoft.com/WebPart/v2/Manifest">
  <Assemblies>
    <Assembly FileName="SPSTree.dll">
 <!-- Use the <ClassResource> tag to specify resources like image files or JScript

files that your Web Parts use. -->
    <!-- Note that you must use relative paths when specifying resource files. -->
      <!--
      <ClassResources>
        <ClassResource FileName="Resource.jpg"/>
      </ClassResources>
      -->
<ClassResources>
    <ClassResource FileName="spsnode.htc"/>
</ClassResources>
<ClassResources>
    <ClassResource FileName="spstree.css"/>
</ClassResources>
<ClassResources>
    <ClassResource FileName="spstree.js"/>
</ClassResources>

<ClassResources>
    <ClassResource FileName="PLUS.GIF"/>
</ClassResources>
<ClassResources>
    <ClassResource FileName="dot.GIF"/>
</ClassResources>
<ClassResources>
    <ClassResource FileName="MINUS.GIF"/>
</ClassResources>
<ClassResources>
    <ClassResource FileName="folder.gif"/>
</ClassResources>
<ClassResources>
    <ClassResource FileName="doclib.gif"/>
</ClassResources>
      <SafeControls>
        <SafeControl
          Namespace="Resoft.SharePoint.WebPart"
          TypeName="*"
        />
      </SafeControls>
    </Assembly>
  </Assemblies>
  <DwpFiles>
    <DwpFile FileName="WebPart1.dwp"/>
  </DwpFiles>
</WebPartManifest>

说明:ClassResources里都是要一起打包的资源文件,注意,用cab方式打包后,资源文件会被拷贝到inetput/wwwroot/wpresources/程序集名称/下,所以在程序中调用这些资源文件时,一律按照路径"/wpresources/程序集名称/资源文件名"来引用 SafeControl指定安全组件的命名控件,使用cab方式部署,会在服务器的web.config中自动添加SafeControl节点,请注意重新按照自己的命名空间改写Namespace属性,微软这里有个bug,不能自动修改Namespace
 DwpFiles:dwp文件路径
2.生成cab包
 把所有文件(包括资源文件,引用的dll,项目产生的dll,dwp文件,Manifest.xml)拷贝到一个a目录中,打开Cabinet Manager 2003程序,选择菜单“添加”,选择a目录下的所有文件,点确定,选择保存为spstree.cab就可以了
3.制作简单的安装和卸载程序:)
 制作install.bat文件
  新建一个install.bat文件,点击编辑,输入
  set path=%path%;C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN\
  stsadm -o addwppack -filename SPSTree.cab
  (把spstree.cab改为你的cab文件即可,记得执行bat文件时,cab文件必须和它在相同目录下,因为上面的命令是指定在同一目录下的spstree.cab,如果对stsam不了解可以输入stsadm/?)
 制作uninstall.bat文件
  新建一个uninstall.bat文件,点击编辑,输入
  set path=%path%;C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN\
  stsadm -o deletewppack -name SPSTree.cab
这个制作方法非常的cool,去其它服务器上部署,只要点击install就Ok了,而最常规的方法,要经过5,6个步骤,而且还容易出错,用了这种办法,就不用像刚开始部署测试时为繁琐的步骤抓狂了,噢霍霍
4.部署时如果出错...
 检查站点根目录\bin下是否有spstree.dll
 检查资源文件是否拷贝到站点根目录\wpresources\程序集名称\下
 站点根目录下的web.config中是否有spstree的<safecontrol Assembly="SPSTree,…>,如果存

在检查相关的属性Namespace是否正确
 检查信任级别
  信任级别可以直接在web.config中修改trust节点的level值,重启iis就会生效了,在默认情况下level可以是 Full、High、Medium、Low、Minimal、WSS_Medium 和 WSS_Minimal。在这些级别中,只有三个级别允许 Web 部件运行:Full、WSS_Medium 或 WSS_Minimal。其他信任级别适用于 ASP.NET,但是不包括 Web 部件所需的特定权限。所以视webpart的功能而定,可以写为
  <trust level="WSS_Medium" originUrl="" />
  或
  <trust level="Full" originUrl="" />
  这样修改方式会带来潜在的危险,其他webpart可以也具有这样的权限访问整个sharepoint站点,还有一种办法是用策略文件,在web.config中添加该文件
   <securityPolicy>
      <trustLevel name="WSS_Medium" policyFile="C:\Program

Files\Common Files\Microsoft Shared\Web Server Extensions\60\config\wss_mediumtrust.config"

/>
      <trustLevel name="WSS_Minimal" policyFile="C:\Program

Files\Common Files\Microsoft Shared\Web Server Extensions\60\config\wss_minimaltrust.config"

/>
   </securityPolicy>