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


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

    首先我要写的大家怎么去获取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以及怎么获取站点集的模版与配额模版、站点的模版等 拆入一些效果图

  • 相关阅读:
    脚本化css 脚本化内联样式 脚本化css类
    jquery插件,表单验证validation plugin的使用
    跨域
    自己做的一个可以用在pc端移动端上点星星评论
    优化Jquery,提升网页加载速度
    编写灵活、稳定、高质量的 HTML 和 CSS 代码的规范
    eclipse编辑js很慢
    sortable bootstrap
    draggable,droppable
    eclipse配置
  • 原文地址:https://www.cnblogs.com/StudyHard/p/SharePoint.html
Copyright © 2020-2023  润新知