设为首页 收藏本站
查看: 597|回复: 0

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

[复制链接]

尚未签到

发表于 2015-9-28 08:25:43 | 显示全部楼层 |阅读模式
  在创建的时候注意你要有权限。还有如果要使用请注意合理释放资源,因为我是随便写的 就没有去考虑资合理问题。
  首先来写怎么去通过代码创建SPweb,SPWeb与Spsite类似所以我就不详细信息注视了
DSC0000.jpg
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 { return  Farm.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[new Guid(this.cbbApp.SelectedValue.ToString())];
#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 = sender  as 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:
DSC0001.jpg DSC0002.jpg
如上图一个添加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 { return  Farm.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[new Guid(this.cbbApp.SelectedValue.ToString())];
#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 = sender  as 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 = sender  as 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[new Guid(cm.SelectedValue.ToString())];
//本来在这里想继续加载站点中的所有页面的,由于暂时没找到怎么获取所有页面所以写死了
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[0].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 = sender  as 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 as  SPFile  ;
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 as  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[new Guid(cm.SelectedValue.ToString())];
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[0].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[new Guid(cbbWeb.SelectedValue.ToString())];
//获取所有的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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-119691-1-1.html 上篇帖子: MOSS & SHAREPOINT ERROR 下篇帖子: 一个简单的SharePoint表单库 + InfoPath应用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表