SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (一)...
发布日期:2022-03-15 04:11:20 浏览次数:71 分类:技术文章

本文共 10584 字,大约阅读时间需要 35 分钟。

在创建的时候注意你要有权限。还有如果要使用请注意合理释放资源,因为我是随便写的 就没有去考虑资合理问题。

首先我要写的大家怎么去获取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();        }        ///         /// 获取服务场        ///         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;        }        ///         /// 绑定应用程序        ///         ///         public void BuildFarmNodes(TreeView tree)        {            //创建首节点            TreeNode n1 = new TreeNode();            this.treeView1.Nodes.Clear();            n1.Text = "服务器场";            SPWebService service = Farm.Services.GetValue
(""); 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); } ///
/// GetFeatureName方法取得功能的名称 (2052代表的是简体中文) /// ///
///
private string GetFeatureName(SPFeatureDefinition definition) { string strRet = definition.GetTitle(new System.Globalization.CultureInfo(2052)); if (String.IsNullOrEmpty(strRet)) { strRet = definition.DisplayName; } return strRet; } ///
/// 绑定网站集 /// ///
///
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); } } } ///
/// 绑定站定 /// ///
///
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); } } } ///
/// 刷新 /// ///
///
private void btRefresh_Click(object sender, EventArgs e) { BuildFarmNodes(this.treeView1); } ///
/// 绑定内容区 /// ///
///
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
(""); 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; } } ///
/// 绑定GirdView或者获取站点集的配额 /// ///
///
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; } } ///
/// 根据选择设置不同的报表 /// ///
///
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以及怎么获取站点集的模版与配额模版、站点的模版等 拆入一些效果图

 

转载于:https://www.cnblogs.com/StudyHard/archive/2013/05/10/SharePoint.html

转载地址:https://blog.csdn.net/weixin_30536513/article/details/99805400 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java二维码生成技术
下一篇:jQuery 遍历

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月29日 04时09分51秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

《中国债券市场》精髓:中国债券市场由政府主导,其最重要的目的是为国家建设筹集资金 2019-04-26
《极简GDP史》精髓:GDP虽有诸多局限性,但是对于社会经济发展仍然有举足轻重的作用 2019-04-26
《经济学是什么》精髓:如何用经济学家的眼光理解个人选择和市场经济? 2019-04-26
《卧底经济学》书中精髓:我们如何正确理解“稀缺”这件事儿? 2019-04-26
《学会花钱》书中精髓:花钱如何掌握分寸,以及如何避开花钱误区 2019-04-26
《定投十年财务自由》书中精髓:我们如何通过定投获得更高的收益? 2019-04-26
《海龟交易法则》精髓:制定对自己有利的交易规则,在风险可控的前提下,当机会出现,你要坚定不移的机械性执行交易 2019-04-26
《彼得·林奇教你理财》书中精髓:如何开始投资,以及我们到底该投资什么? 2019-04-26
《货币简史》书中的精髓:货币产生的起源是什么?货币又是如何发展起来的? 2019-04-26
《摩根财团》精髓:摩根财团与时俱进,在不同时代扮演不同角色,始终走在时代的前列 2019-04-26
《朝贡贸易与仗剑经商》精髓:古代中国朝廷不保护商人,将中国商人置于西方势力的仗剑经商之下 2019-04-26
《华尔街之狼》精髓:摔倒并不是坏事,就怕你因此放弃。 2019-04-26
《微观动机与宏观行为》精髓:个人的微观动机,是如何影响宏观行为结果的? 2019-04-26
《国富论》精髓:亚当·斯密提出的对后世影响深远的三个经济学理论:劳动分工理论、生产要素理论和宏观调控理论 2019-04-26
《动荡的世界》精髓:什么是动物精神?动物精神又是怎么影响2008年全球经济危机的,以及我们该如何预防动物精神,避免危机再次发生。 2019-04-26
《投资最重要的事》精髓:利用逆向思维,掌握既冷静又勇猛的投资方法,成为投资界真正厉害的人。 2019-04-26
《周期》书中的精髓:如何利用周期,掌握世界的发展趋势,实现财富积累。 2019-04-26
《伟大的博弈》书中的精髓:华尔街是如何从一条小街,一步步发展为世界金融中心的。 2019-04-26
《逃不开的经济周期》书中的精髓:经济周期是推动创新变革和经济增长以及复兴的关键力量。 2019-04-26
《朋友还是对手》书中的精髓:奥地利学派和芝加哥学派两派共识远多于分歧,两派首先是朋友,其次才是对手。 2019-04-26