solo_882 发表于 2015-9-28 08:25:43

SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (三)

  在创建的时候注意你要有权限。还有如果要使用请注意合理释放资源,因为我是随便写的 就没有去考虑资合理问题。
  首先来写怎么去通过代码创建SPweb,SPWeb与Spsite类似所以我就不详细信息注视了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
namespace TestWF
{
public partial class CreateWeb : Form
{
public Form2 ParentForm { get; set; }
public CreateWeb()
{
InitializeComponent();
}
/// <summary>
/// Farm场
/// </summary>
public SPFarm Farm
{
get { return SPFarm.Local; }
}
/// <summary>
/// 服务
/// </summary>
public SPWebService SPWebService {
get { returnFarm.Services.GetValue<SPWebService>(&quot;&quot;); }
}
private void CreateApp_Load(object sender, EventArgs e)
{
#region 加载应用程序
List<ComboBoxModel> list = new List<ComboBoxModel>();
list.Add(new ComboBoxModel() { ID = &quot;1&quot;, Name = &quot;--请选择--&quot; });
foreach (SPWebApplication webApp in SPWebService.WebApplications)
{
list.Add(new ComboBoxModel() { ID = webApp.Id.ToString(), Name = webApp.Name });
}
this.cbbApp.DataSource = list;
this.cbbApp.DisplayMember = &quot;Name&quot;;
this.cbbApp.ValueMember = &quot;Id&quot;;
this.cbbApp.SelectedValueChanged += cbbApp_SelectedValueChanged;
#endregion
#region 加载模板选择
//填写管理中心地址
SPSite site = new SPSite(&quot;http://jason-pc:5901/&quot;);
SPWeb web = site.OpenWeb();
//获取所有模版
SPWebTemplateCollection templates = web.GetAvailableWebTemplates(2052);
TreeNode node = new TreeNode();
node.Text = &quot;协作&quot;;
BindTemplateTree(node, templates);
this.tvTemp.Nodes.Add(node);
node = new TreeNode();
node.Text = &quot;会议&quot;;
BindTemplateTree(node, templates);
this.tvTemp.Nodes.Add(node);

node = new TreeNode();
node.Text = &quot;企业&quot;;
BindTemplateTree(node, templates);
this.tvTemp.Nodes.Add(node);
node = new TreeNode();
node.Text = &quot;发布&quot;;
BindTemplateTree(node, templates);
this.tvTemp.Nodes.Add(node);
node = new TreeNode();
node.Text = &quot;自定义&quot;;
BindTemplateTree(node, templates);
this.tvTemp.Nodes.Add(node);
web.Close();
site.Close();
#endregion
}
/// <summary>
/// 更改URL
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
void cbbApp_SelectedValueChanged(object sender, EventArgs e)
{
if (this.cbbApp.SelectedValue != null&&this.cbbApp.SelectedValue!=&quot;1&quot;)
{
            SPWebApplication webApp = SPWebService.WebApplications;
#region 加载站点集
List<ComboBoxModel> sites = new List<ComboBoxModel>();
sites.Add(new ComboBoxModel() { ID = &quot;1&quot;, Name = &quot;--请选择--&quot; });
foreach (SPSite site in webApp.Sites)
{
sites.Add(new ComboBoxModel() { ID = site.ID.ToString(), Name = site.Url });
}
this.cbSites.DataSource = sites;
this.cbSites.DisplayMember = &quot;Name&quot;;
this.cbSites.ValueMember = &quot;Id&quot;;
this.cbSites.SelectedValueChanged += cbSites_SelectedValueChanged;
#endregion
}
}
void cbSites_SelectedValueChanged(object sender, EventArgs e)
{
ComboBox cm = senderas ComboBox;
if (cm.SelectedValue != null&&cm.SelectedValue!=&quot;1&quot;)
{
using (SPSite site = new SPSite(new Guid(cm.SelectedValue.ToString())))
{
cm.Tag = site;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
if (this.lbtemp.Tag == null)
{
MessageBox.Show(&quot;你未选择模版!&quot;);
}
if (this.cbSites.Tag == null)
{
MessageBox.Show(&quot;你未选择站点集!&quot;);
}
try
{
SPWebTemplate t = this.lbtemp.Tag as SPWebTemplate;
SPSite site = this.cbSites.Tag as SPSite;
SPWeb web = site.AllWebs.Add(this.tbUrl.Text.Trim(), this.tbTitel.Text.Trim(), this.tbDesc.Text.Trim(), 2052,t, false, false);
web.Close();
site.Close();
}
catch (Exception ex)
{
MessageBox.Show(&quot;创建站点失败:&quot; + ex.Message);
}
this.Cursor = Cursors.Default;
ParentForm.BindTree();
}
void BindTemplateTree(TreeNode node,SPWebTemplateCollection temp)
{
foreach (SPWebTemplate t in temp)
{
if(node.Text==t.DisplayCategory)
{
node.Nodes.Add(new TreeNode() { Text = t.Title, Tag = t });
}
}   
}
private void tvTemp_AfterSelect(object sender, TreeViewEventArgs e)
{
this.lbtemp.Text=&quot;你已经选中:&quot;+e.Node.Text;
this.lbtemp.Tag = e.Node.Tag;
if (e.Node.Tag != null)
{
SPWebTemplate t = e.Node.Tag as SPWebTemplate;
lbDesc.Text = t.Description;
}
}
}
}
后面写怎么去页面上添加与删除WebPart:

如上图一个添加WebPart一个删除已经有的WebPart,这里需要注意的是我目前是把页面的地址写死了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using System.Xml;
namespace TestWF
{
public partial class AddWebPart : Form
{
public Form2 ParentForm { get; set; }
public AddWebPart()
{
InitializeComponent();
}
/// <summary>
/// Farm场
/// </summary>
public SPFarm Farm
{
get { return SPFarm.Local; }
}
/// <summary>
/// 服务
/// </summary>
public SPWebService SPWebService {
get { returnFarm.Services.GetValue<SPWebService>(&quot;&quot;); }
}
private void CreateApp_Load(object sender, EventArgs e)
{
#region 加载应用程序
List<ComboBoxModel> list = new List<ComboBoxModel>();
list.Add(new ComboBoxModel() { ID = &quot;1&quot;, Name = &quot;--请选择--&quot; });
foreach (SPWebApplication webApp in SPWebService.WebApplications)
{
list.Add(new ComboBoxModel() { ID = webApp.Id.ToString(), Name = webApp.Name });
}
this.cbbApp.DataSource = list;
this.cbbApp.DisplayMember = &quot;Name&quot;;
this.cbbApp.ValueMember = &quot;Id&quot;;
this.cbbApp.SelectedValueChanged += cbbApp_SelectedValueChanged;
#endregion
}
/// <summary>
/// 加载站点集
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
void cbbApp_SelectedValueChanged(object sender, EventArgs e)
{
if (this.cbbApp.SelectedValue != null&&this.cbbApp.SelectedValue!=&quot;1&quot;)
{
SPWebApplication webApp = SPWebService.WebApplications;
#region 加载站点集
List<ComboBoxModel> sites = new List<ComboBoxModel>();
sites.Add(new ComboBoxModel() { ID = &quot;1&quot;, Name = &quot;--请选择--&quot; });
foreach (SPSite site in webApp.Sites)
{
sites.Add(new ComboBoxModel() { ID = site.ID.ToString(), Name = site.Url });
}
this.cbSites.DataSource = sites;
this.cbSites.DisplayMember = &quot;Name&quot;;
this.cbSites.ValueMember = &quot;Id&quot;;
this.cbSites.SelectedValueChanged += cbSites_SelectedValueChanged;
#endregion
}
}
/// <summary>
/// 加载站点
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
void cbSites_SelectedValueChanged(object sender, EventArgs e)
{
ComboBox cm = senderas ComboBox;
if (cm.SelectedValue != null&&cm.SelectedValue!=&quot;1&quot;)
{
using (SPSite site = new SPSite(new Guid(cm.SelectedValue.ToString())))
{
cm.Tag = site;
#region 加载站点
List<ComboBoxModel> webs = new List<ComboBoxModel>();
webs.Add(new ComboBoxModel() { ID = &quot;1&quot;, Name = &quot;--请选择--&quot; });
foreach (SPWeb web in site.AllWebs)
{
webs.Add(new ComboBoxModel() { ID = web.ID.ToString(), Name = web.Title });
}
this.cbbWeb.DataSource = webs;
this.cbbWeb.DisplayMember = &quot;Name&quot;;
this.cbbWeb.ValueMember = &quot;Id&quot;;
this.cbbWeb.SelectedValueChanged += cbbWeb_SelectedValueChanged;
#endregion
}
}
}
/// <summary>
/// 选择站点
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
void cbbWeb_SelectedValueChanged(object sender, EventArgs e)
{
ComboBox cm = senderas ComboBox;
if (cm.SelectedValue != null && cm.SelectedValue != &quot;1&quot;&& this.cbSites.Tag!=null)
{
using (SPSite site = new SPSite(new Guid(this.cbSites.SelectedValue.ToString())))
{
SPWeb web=site.AllWebs;
//本来在这里想继续加载站点中的所有页面的,由于暂时没找到怎么获取所有页面所以写死了
this.cbbPage.SelectedValueChanged += cbbPage_SelectedValueChanged;
cm.Tag = web;
this.Cursor = Cursors.WaitCursor;
try
{
this.tvTemp.Nodes.Clear();
//根据站点获取所有WebPart并且加入树中
SPLimitedWebPartManager manger = web.GetLimitedWebPartManager(&quot;SitePages/Home.aspx&quot;, PersonalizationScope.Shared);
for (int i = 0; i < manger.WebParts.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = manger.WebParts.Title;
node.Tag = manger.WebParts.ID;
this.tvTemp.Nodes.Add(node);
}
}
catch (Exception ex)
{
MessageBox.Show(&quot;错误:&quot;+ex.Message);
}
this.Cursor = Cursors.Default;
}
}
}
/// <summary>
/// 本来在这里想继续加载站点中的所有页面的,由于暂时没找到怎么获取所有页面所以写死了
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
void cbbPage_SelectedValueChanged(object sender, EventArgs e)
{
ComboBox cm = senderas ComboBox;
if (cm.SelectedValue != null && cm.SelectedValue != &quot;1&quot; && this.cbbWeb.Tag != null)
{
SPWeb web = this.cbbWeb.Tag as SPWeb;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
if (this.tvTemp.Tag == null)
{
MessageBox.Show(&quot;你未选择WebPart!&quot;);
return;
}
if (this.cbbWeb.Tag == null)
{
MessageBox.Show(&quot;你未选择站点集!&quot;);
return;
}
try
{
string ID= this.tvTemp.Tag.ToString();
SPWeb web = this.cbbWeb.Tag as SPWeb;
try
{
///删除选中的一个WebPart
SPLimitedWebPartManager manger = web.GetLimitedWebPartManager(&quot;SitePages/Home.aspx&quot;, PersonalizationScope.Shared);
for (int i = 0; i < manger.WebParts.Count; i++)
{
if (manger.WebParts.ID == ID)
{
manger.DeleteWebPart(manger.WebParts);
}
}
MessageBox.Show(&quot;删除WebPart成功!&quot;);
}
catch (Exception ex)
{
MessageBox.Show(&quot;错误:&quot; + ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(&quot;失败:&quot; + ex.Message);
}
this.Cursor = Cursors.Default;
ParentForm.BindTree();
}
private void tvTemp_AfterSelect(object sender, TreeViewEventArgs e)
{
this.tvTemp.Tag = null;
if (e.Node.Tag != null)
{
this.tvTemp.Tag =e.Node.Tag;
this.lbPart.Text = &quot;你已经选中了:&quot; + e.Node.Text;

}
}
private void button3_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
if (this.tvTemp.Tag == null)
{
MessageBox.Show(&quot;你未选择WebPart!&quot;);
return;
}
if (this.cbbWeb.Tag == null)
{
MessageBox.Show(&quot;你未选择站点集!&quot;);
return;
}
try
{
//获取选中的WebPart列表中的其中一个SPFile
SPFile file =this.tvTemp.Tag asSPFile;
SPWeb web = this.cbbWeb.Tag as SPWeb;
try
{
//获取一个SPLimitedWebPartManager管理类
SPLimitedWebPartManager manger = web.GetLimitedWebPartManager(&quot;SitePages/Home.aspx&quot;, PersonalizationScope.Shared);
//创建一个WebPart 部件
XmlReader xmlReader = new XmlTextReader(file.OpenBinaryStream());
string errorMessage;
System.Web.UI.WebControls.WebParts.WebPart webPart = manger.ImportWebPart(xmlReader, out errorMessage);
//将WebPart加载到页面上 其中后两个参数是设置页面位置的
manger.AddWebPart(webPart,&quot;Left&quot;,2);
MessageBox.Show(&quot;添加WebPart成功!&quot;);
}
catch (Exception ex)
{
MessageBox.Show(&quot;错误:&quot; + ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(&quot;失败:&quot; + ex.Message);
}
this.Cursor = Cursors.Default;
ParentForm.BindTree();
}
/// <summary>
/// 加载页面已经有的WebPart列表
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
this.tvTemp.Nodes.Clear();
ComboBox cm = this.cbbWeb asComboBox;
if (cm.SelectedValue != null && cm.SelectedValue != &quot;1&quot; && this.cbSites.Tag != null)
{
using (SPSite site = new SPSite(new Guid(this.cbSites.SelectedValue.ToString())))
{
SPWeb web = site.AllWebs;
this.cbbPage.SelectedValueChanged += cbbPage_SelectedValueChanged;
cm.Tag = web;
this.Cursor = Cursors.WaitCursor;
try
{
//根据站点获取所有WebPart并且加入树中
SPLimitedWebPartManager manger = web.GetLimitedWebPartManager(&quot;SitePages/Home.aspx&quot;, PersonalizationScope.Shared);
for (int i = 0; i < manger.WebParts.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = manger.WebParts.Title;
node.Tag = manger.WebParts.ID;
this.tvTemp.Nodes.Add(node);
}
}
catch (Exception ex)
{
MessageBox.Show(&quot;错误:&quot; + ex.Message);
}
this.Cursor = Cursors.Default;
}
}
}
/// <summary>
/// 加载WebPart列表
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (this.cbbWeb.Tag != null&& this.cbSites.Tag != null)
{
this.Cursor = Cursors.WaitCursor;
using (SPSite site = new SPSite(new Guid(this.cbSites.SelectedValue.ToString())))
{
SPWeb web = site.AllWebs;
//获取所有的WebPart的SPList列表 注意这里返回的还是不是WebPart
SPList list = web.GetCatalog(SPListTemplateType.WebPartCatalog);
for (int i = 0; i < list.ItemCount; i++)
{
TreeNode node = new TreeNode();
node.Text = list.Items.File.Title;
node.Tag = list.Items.File;
this.tvTemp.Nodes.Add(node);
}

}
this.Cursor = Cursors.Default;
}
}
}
}


  以上就是怎么去删除和添加WebPart ,至于移动WebPart也差不多我就没写。
  如果有更高的方法请大家告诉我。
页: [1]
查看完整版本: SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (三)