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

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

[复制链接]

尚未签到

发表于 2015-9-26 12:28:28 | 显示全部楼层 |阅读模式
  在创建的时候注意你要有权限。还有如果要使用请注意合理释放资源,因为我是随便写的 就没有去考虑资合理问题。
  首先我要写的大家怎么去获取SPWeb/SPSiite/SPWebApplication,我会使用一个TreeView展示出来,
  然后里面包括了SPWeb的几个经常使用报表数据和怎么通过代码去设置站点集的使用配额。下面是代码实现:
  详细介绍我就写进代码注视:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.ApplicationPages.WebControls;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Globalization;

namespace TestWF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 获取服务场
/// </summary>
public SPFarm Farm
{
get { return SPFarm.Local; }
}
private void Form1_Load(object sender, EventArgs e)
{
//绑定TreeView
BuildFarmNodes(this.treeView1);
//设置网站集配额Max
this.tbMax.Enabled = false;
//设置网站集配额Min
this.tbMin.Enabled = false;
///设置button
this.button1.Enabled = false;
//报表类别选择
this.comboBox1.Enabled = false;
}
/// <summary>
/// 绑定应用程序
/// </summary>
/// <param name="tree"></param>
public void BuildFarmNodes(TreeView tree)
{
//创建首节点
TreeNode n1 = new TreeNode();
this.treeView1.Nodes.Clear();
n1.Text = "服务器场";
SPWebService service = Farm.Services.GetValue<SPWebService>("");
foreach (SPWebApplication webApp in service.WebApplications)
{
TreeNode node = new TreeNode();
node.Text = "应用程序:" + webApp.Name;
node.Tag = webApp.Id.ToString();
node.ToolTipText = WebType.应用程序.ToString();
//根据应用程序绑定站点集
BindTreeNode(node, webApp);
n1.Nodes.Add(node);
}
n1.ExpandAll();
tree.Nodes.Add(n1);
//报表类别加入常用的值
this.comboBox1.Items.Add(SPUsageReportType.browser);
this.comboBox1.Items.Add(SPUsageReportType.os);
this.comboBox1.Items.Add(SPUsageReportType.refUrl);
this.comboBox1.Items.Add(SPUsageReportType.url);
this.comboBox1.Items.Add(SPUsageReportType.user);
//TreeNode n2 = new TreeNode();
//n2.Text = "功能定义";
//foreach (SPFeatureDefinition definition in Farm.FeatureDefinitions)
//{
//    string strRet = definition.GetTitle(new System.Globalization.CultureInfo(2052));
//    if (String.IsNullOrEmpty(strRet))
//    {
//        strRet = definition.DisplayName;
//    }
//    TreeNode node = new TreeNode();
//    node.Text = strRet;
//    node.Tag = definition.Id;
//    n2.Nodes.Add(node);
//    node.ToolTipText = WebType.功能定义.ToString();
//}
//tree.Nodes.Add(n2);
}
/// <summary>
/// GetFeatureName方法取得功能的名称 (2052代表的是简体中文)
/// </summary>
/// <param name="definition"></param>
/// <returns></returns>
private string GetFeatureName(SPFeatureDefinition definition)
{
string strRet = definition.GetTitle(new System.Globalization.CultureInfo(2052));
if (String.IsNullOrEmpty(strRet))
{
strRet = definition.DisplayName;
}
return strRet;
}
/// <summary>
/// 绑定网站集
/// </summary>
/// <param name="node"></param>
/// <param name="site"></param>
public void BindTreeNode(TreeNode node, SPWebApplication webApp)
{
if (webApp.Sites.Count > 0)
{
foreach (SPSite site in webApp.Sites)
{
TreeNode n = new TreeNode();
n.Text = "网站集:" + site.Url;
n.Tag = site.ID.ToString();
n.ToolTipText = WebType.网站集.ToString();
//根据网站集绑定站点
BindTreeNode(n, site);
node.Nodes.Add(n);
}
}
}
/// <summary>
/// 绑定站定
/// </summary>
/// <param name="node"></param>
/// <param name="site"></param>
public void BindTreeNode(TreeNode node, SPSite site)
{
if (site.AllWebs.Count > 0)
{
foreach (SPWeb web in site.AllWebs)
{
TreeNode n = new TreeNode();
//这里需要注意创建站点集的同时,默认有一个站点 而这个站点的Name是空的
n.Text = "站点:" + (string.IsNullOrEmpty(web.Name) == true ? site.Url : web.Name);
n.Tag = web.ID.ToString();
n.ToolTipText = WebType.站点.ToString();
node.Nodes.Add(n);
}
}
}
/// <summary>
/// 刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btRefresh_Click(object sender, EventArgs e)
{
BuildFarmNodes(this.treeView1);
}

/// <summary>
/// 绑定内容区
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
SPSite site = null;
SPWeb web = null;
SPWebApplication app = null;
switch (e.Node.ToolTipText.ToString())
{
///当选择为站点的时候会自动去显示报表数据
case "站点":
site = new SPSite(new Guid(e.Node.Parent.Tag.ToString()));
web = site.OpenWeb(new Guid(e.Node.Tag.ToString()));
label1.Text = "当前选中站点:" + (string.IsNullOrEmpty(web.Name) == true ? site.Url : web.Name);
//绑定GirdView
BindDataGirdView(web, e.Node.ToolTipText);
break;
case "网站集":
site = new SPSite(new Guid(e.Node.Tag.ToString()));
label1.Text = "当前选中站点集:" + site.Url;
//获取站点集的配额
BindDataGirdView(site, e.Node.ToolTipText);
break;
case "应用程序":
SPWebService service = Farm.Services.GetValue<SPWebService>("");
app = service.WebApplications[new Guid(e.Node.Tag.ToString())];
this.label1.Text = "当前选中应用程序:" + app.Name;
//BindDataGirdView(app, e.Node.ToolTipText);
break;
//case "功能定义":
//    this.label1.Text = "当前选中功能定义:" + e.Node.Text;
//    break;
}

}
/// <summary>
/// 绑定GirdView或者获取站点集的配额
/// </summary>
/// <param name="obj"></param>
/// <param name="type"></param>
void BindDataGirdView(object obj, string type)
{
switch (type)
{
case "站点":
this.tbMax.Enabled = false;
this.tbMax.Text = "";
this.tbMin.Text = "";
this.tbMin.Enabled = false;
this.button1.Enabled = false;
this.comboBox1.Enabled = true;
//根据站点绑定站点一些常用报表
SPWeb web = obj as SPWeb;
DataTable dt = web.GetUsageData(SPUsageReportType.user, SPUsagePeriodType.lastMonth);
this.comboBox1.Tag = web;
this.btnSet.DataSource = dt;
break;
case "网站集":
this.tbMax.Enabled = true;
this.tbMin.Enabled = true;
this.button1.Enabled = true;
this.comboBox1.Enabled = false;
//根据SPite获取网站集的配额
SPSite site = obj as SPSite;
this.tbMax.Text = (site.Quota.StorageMaximumLevel / 1024 / 1024).ToString();
this.tbMin.Text = (site.Quota.StorageWarningLevel / 1024 / 1024).ToString();

this.button1.Tag = site;
break;
case "应用程序":
this.tbMax.Text = "";
this.tbMin.Text = "";
this.tbMax.Enabled = false;
this.tbMin.Enabled = false;
this.button1.Enabled = false;
this.comboBox1.Enabled = false;
break;
}
}
/// <summary>
/// 根据选择设置不同的报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.Tag != null)
{
SPUsageReportType sput = SPUsageReportType.user;
switch (this.comboBox1.Text)
{
case "user":
sput = SPUsageReportType.user;
break;
case "browser":
sput = SPUsageReportType.browser;
break;
case "os":
sput = SPUsageReportType.os;
break;
case "refUrl":
sput = SPUsageReportType.refUrl;
break;
case "url":
sput = SPUsageReportType.url;
break;

}
SPWeb web = comboBox1.Tag as SPWeb;
DataTable dt = web.GetUsageData(sput, SPUsagePeriodType.lastMonth);
this.comboBox1.Tag = web;
this.btnSet.DataSource = dt;
}
}
//根据SPite获取网站集的配额
private void button1_Click(object sender, EventArgs e)
{
try
{
if (this.button1.Tag != null)
{
//根据SPite获取网站集的配额
SPSite site = this.button1.Tag as SPSite;
site.Quota.StorageMaximumLevel = Convert.ToInt32(this.tbMax.Text) * 1024 * 1024;
site.Quota.StorageWarningLevel = Convert.ToInt32(this.tbMin.Text) * 1024 * 1024;
MessageBox.Show("设置配额成功");
}
}
catch (ArgumentException ex)
{
MessageBox.Show("输入最大值必须大于最小值!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public enum WebType
{
站点,
网站集,
应用程序,
功能定义
}
}

  以上的代码就是获取SPWeb/SPSiite/SPWebApplication的获取以及站点的常用数据与网站的配额,下一篇将写怎么去通过代码创建SPWeb/SPSiite/SPWebApplication以及怎么获取站点集的模版与配额模版、站点的模版等 拆入一些效果图
  
DSC0000.jpg DSC0001.jpg DSC0002.jpg DSC0003.jpg

运维网声明 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-119096-1-1.html 上篇帖子: 如何在SharePoint中配置和自定义Content Query Web Part(一) 下篇帖子: SharePoint 2013 App 开发—App开发概述
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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