SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (一)...
在創(chuàng)建的時(shí)候注意你要有權(quán)限。還有如果要使用請(qǐng)注意合理釋放資源,因?yàn)槲沂请S便寫的 就沒有去考慮資合理問題。
首先我要寫的大家怎么去獲取SPWeb/SPSiite/SPWebApplication,我會(huì)使用一個(gè)TreeView展示出來,
然后里面包括了SPWeb的幾個(gè)經(jīng)常使用報(bào)表數(shù)據(jù)和怎么通過代碼去設(shè)置站點(diǎn)集的使用配額。下面是代碼實(shí)現(xiàn):
詳細(xì)介紹我就寫進(jìn)代碼注視:
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>/// 獲取服務(wù)場(chǎng)/// </summary>public SPFarm Farm{get { return SPFarm.Local; }}private void Form1_Load(object sender, EventArgs e){//綁定TreeViewBuildFarmNodes(this.treeView1);//設(shè)置網(wǎng)站集配額Maxthis.tbMax.Enabled = false;//設(shè)置網(wǎng)站集配額Minthis.tbMin.Enabled = false;///設(shè)置buttonthis.button1.Enabled = false;//報(bào)表類別選擇this.comboBox1.Enabled = false;}/// <summary>/// 綁定應(yīng)用程序/// </summary>/// <param name="tree"></param>public void BuildFarmNodes(TreeView tree){//創(chuàng)建首節(jié)點(diǎn)TreeNode n1 = new TreeNode();this.treeView1.Nodes.Clear();n1.Text = "服務(wù)器場(chǎng)";SPWebService service = Farm.Services.GetValue<SPWebService>("");foreach (SPWebApplication webApp in service.WebApplications){TreeNode node = new TreeNode();node.Text = "應(yīng)用程序:" + webApp.Name;node.Tag = webApp.Id.ToString();node.ToolTipText = WebType.應(yīng)用程序.ToString();//根據(jù)應(yīng)用程序綁定站點(diǎn)集BindTreeNode(node, webApp);n1.Nodes.Add(node);}n1.ExpandAll();tree.Nodes.Add(n1);//報(bào)表類別加入常用的值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代表的是簡(jiǎn)體中文)/// </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>/// 綁定網(wǎng)站集/// </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 = "網(wǎng)站集:" + site.Url;n.Tag = site.ID.ToString();n.ToolTipText = WebType.網(wǎng)站集.ToString();//根據(jù)網(wǎng)站集綁定站點(diǎn)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();//這里需要注意創(chuàng)建站點(diǎn)集的同時(shí),默認(rèn)有一個(gè)站點(diǎn) 而這個(gè)站點(diǎn)的Name是空的n.Text = "站點(diǎn):" + (string.IsNullOrEmpty(web.Name) == true ? site.Url : web.Name);n.Tag = web.ID.ToString();n.ToolTipText = WebType.站點(diǎn).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>/// 綁定內(nèi)容區(qū)/// </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()){///當(dāng)選擇為站點(diǎn)的時(shí)候會(huì)自動(dòng)去顯示報(bào)表數(shù)據(jù)case "站點(diǎn)":site = new SPSite(new Guid(e.Node.Parent.Tag.ToString()));web = site.OpenWeb(new Guid(e.Node.Tag.ToString()));label1.Text = "當(dāng)前選中站點(diǎn):" + (string.IsNullOrEmpty(web.Name) == true ? site.Url : web.Name);//綁定GirdViewBindDataGirdView(web, e.Node.ToolTipText); break;case "網(wǎng)站集":site = new SPSite(new Guid(e.Node.Tag.ToString()));label1.Text = "當(dāng)前選中站點(diǎn)集:" + site.Url;//獲取站點(diǎn)集的配額BindDataGirdView(site, e.Node.ToolTipText);break;case "應(yīng)用程序":SPWebService service = Farm.Services.GetValue<SPWebService>("");app = service.WebApplications[new Guid(e.Node.Tag.ToString())];this.label1.Text = "當(dāng)前選中應(yīng)用程序:" + app.Name;//BindDataGirdView(app, e.Node.ToolTipText);break;//case "功能定義":// this.label1.Text = "當(dāng)前選中功能定義:" + e.Node.Text;// break;}}/// <summary>/// 綁定GirdView或者獲取站點(diǎn)集的配額/// </summary>/// <param name="obj"></param>/// <param name="type"></param>void BindDataGirdView(object obj, string type){switch (type){case "站點(diǎn)":this.tbMax.Enabled = false;this.tbMax.Text = "";this.tbMin.Text = "";this.tbMin.Enabled = false;this.button1.Enabled = false;this.comboBox1.Enabled = true;//根據(jù)站點(diǎn)綁定站點(diǎn)一些常用報(bào)表SPWeb web = obj as SPWeb;DataTable dt = web.GetUsageData(SPUsageReportType.user, SPUsagePeriodType.lastMonth);this.comboBox1.Tag = web;this.btnSet.DataSource = dt;break;case "網(wǎng)站集":this.tbMax.Enabled = true;this.tbMin.Enabled = true;this.button1.Enabled = true;this.comboBox1.Enabled = false;//根據(jù)SPite獲取網(wǎng)站集的配額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 "應(yīng)用程序":this.tbMax.Text = "";this.tbMin.Text = "";this.tbMax.Enabled = false;this.tbMin.Enabled = false;this.button1.Enabled = false;this.comboBox1.Enabled = false;break;}}/// <summary>/// 根據(jù)選擇設(shè)置不同的報(bào)表/// </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;}}//根據(jù)SPite獲取網(wǎng)站集的配額 private void button1_Click(object sender, EventArgs e){try{if (this.button1.Tag != null){//根據(jù)SPite獲取網(wǎng)站集的配額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("設(shè)置配額成功");}}catch (ArgumentException ex){MessageBox.Show("輸入最大值必須大于最小值!");}catch (Exception ex){MessageBox.Show(ex.Message);}}}public enum WebType{站點(diǎn),網(wǎng)站集,應(yīng)用程序,功能定義} }以上的代碼就是獲取SPWeb/SPSiite/SPWebApplication的獲取以及站點(diǎn)的常用數(shù)據(jù)與網(wǎng)站的配額,下一篇將寫怎么去通過代碼創(chuàng)建SPWeb/SPSiite/SPWebApplication以及怎么獲取站點(diǎn)集的模版與配額模版、站點(diǎn)的模版等 拆入一些效果圖
?
轉(zhuǎn)載于:https://www.cnblogs.com/StudyHard/archive/2013/05/10/SharePoint.html
總結(jié)
以上是生活随笔為你收集整理的SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (一)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux cp 强制覆盖
- 下一篇: 商场专柜私收银现象治理要点