从零开始的生活是充实的!
SharePoint搞到牛叉!

如果是要使用自动化部署,将不会在需要手动在页面中添加 webpart,而是完全可以用程序来完成这些操作了。

提供两个方法,用来添加webpart到页面上,其中需要注意的两个地方 是 webpart区域,区域一定是区域的 ID

zoneIndex 是这个webpart的 摆放顺序,从上到下 1,2,3,4,5,6这样排列。

   1:   #region 添加webpart到页面
   2:          private string AddWebPartToPage(SPWeb web, string pageUrl, string webPartName, string zoneID, int zoneIndex)
   3:          {
   4:              using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(
   5:                      pageUrl, PersonalizationScope.Shared))
   6:              {
   7:                  using (System.Web.UI.WebControls.WebParts.WebPart webPart = CreateWebPart(web, webPartName, webPartManager))
   8:                  {
   9:                      SPLimitedWebPartCollection webParts = webPartManager.WebParts;
  10:                      webPartManager.AddWebPart(webPart, zoneID, zoneIndex);
  11:                      return webPart.ID;
  12:                  }
  13:              }
  14:   
  15:          }
  16:   
  17:          private System.Web.UI.WebControls.WebParts.WebPart CreateWebPart(SPWeb web, string webPartName, SPLimitedWebPartManager webPartManager)
  18:          {
  19:              SPQuery qry = new SPQuery();
  20:              qry.Query = String.Format(CultureInfo.CurrentCulture, "<Where><Eq><FieldRef Name=\"FileLeafRef\" /><Value Type=\"File\">{0}</Value></Eq></Where>", webPartName);
  21:   
  22:              SPList webPartGallery = null;
  23:   
  24:              if (null == web.ParentWeb)
  25:              {
  26:                  webPartGallery = web.GetCatalog(
  27:                     SPListTemplateType.WebPartCatalog);
  28:              }
  29:              else
  30:              {
  31:                  webPartGallery = web.Site.RootWeb.GetCatalog(
  32:                     SPListTemplateType.WebPartCatalog);
  33:              }
  34:   
  35:              SPListItemCollection webParts = webPartGallery.GetItems(qry);
  36:   
  37:              XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream());
  38:              string errorMsg;
  39:              System.Web.UI.WebControls.WebParts.WebPart webPart = webPartManager.ImportWebPart(xmlReader, out errorMsg);
  40:   
  41:              return webPart;
  42:          }
  43:          #endregion

使用方法

   1:  AddWebPartToPage(oWeb, oList.DefaultViewUrl, “webpartname.webpart”, "Main", 1);
posted on 2012-02-15 16:08  Yunliang Yu  阅读(918)  评论(0编辑  收藏  举报