热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

在创建的时候注意你要有权限。还有如果要使用请注意合理释放资源,因为我是随便写的就没有去考虑资合理问题。首先来写怎么去通过代码创建SPWebApplication&#x

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

首先来写怎么去通过代码创建SPWebApplication,详细介绍我就写进代码注视:

void CreateApp(){this.Cursor = Cursors.WaitCursor;//获取服务器场SPFarm farm =SPWebService.AdministrationService.Farm;SPWebApplicationBuilder webAppBld =new SPWebApplicationBuilder(farm);//设置应用程序IDwebAppBld.Id = Guid.NewGuid();int port=new Random().Next(1000, 30000);//设置端口webAppBld.Port = port;DirectoryInfo rootDirInfo = new DirectoryInfo(@"C:\Inetpub\wwwroot\wss\VirtualDirectories\"+port);webAppBld.RootDirectory = rootDirInfo;webAppBld.ApplicationPoolId ="SharePoint Pool - " + port.ToString();//实例一个密码SecureString appPoolPwd = new SecureString();appPoolPwd.AppendChar('2');appPoolPwd.AppendChar('3');appPoolPwd.AppendChar('5');appPoolPwd.AppendChar('4');appPoolPwd.AppendChar('1');appPoolPwd.AppendChar('2');appPoolPwd.AppendChar('4');appPoolPwd.AppendChar('2');appPoolPwd.MakeReadOnly();webAppBld.IdentityType = IdentityType.SpecificUser;//设置 用户和密码webAppBld.ApplicationPoolUsername = "name";webAppBld.ApplicationPoolPassword = appPoolPwd;// the default is false so that Kerberos auth. is supported. If // you're not using Kerberos, set this to truewebAppBld.UseNTLMExclusively = true;// default is falsewebAppBld.AllowAnonymousAccess = false;// the default is false so that SSL is not used in IIS.// set this to true if the IIS web site hosting this web application// should use SSLwebAppBld.UseSecureSocketsLayer = false;//设置默认备用访问映射Uri defaultZone = new Uri("http://"+System.Windows.Forms.SystemInformation.ComputerName+":"+port);webAppBld.DefaultZoneUri = defaultZone;webAppBld.CreateNewDatabase = true;//服务器地址webAppBld.DatabaseServer = @"JASON-PC\SharePoint";webAppBld.DatabaseName = "SharePoint - "+port;//设置数据库用户名设置为空或空字符串,使用Windows集成验证//如果你想使用SQL身份验证则设置用户名和密码DatabasePasswordwebAppBld.DatabaseUsername = String.Empty;SPWebApplication webApp = webAppBld.Create();webApp.Update();#region MyRegion//SPServer server = new SPServer("OSSRTM");// sharepoint服务器搜索实例//SPSearchService srchService =// SPFarm.Local.Services.GetValue("SPSearch");//SPSearchServiceInstance searchServiceInst =// (SPSearchServiceInstance)srchService.Instances[Guid.NewGuid()];//设置搜索服务实例爬行web应用程序//webAppBld.SearchServiceInstance = searchServiceInst; #endregionthis.Cursor = Cursors.Default;MessageBox.Show("创建应用程序成功!");}

写来写怎么根据一个应用程序创建站点集,这里需要知道的怎么去获取模版,下面图片是我的窗体:

CreateSite

然后附上代码:

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 System.Configuration;namespace TestWF
{
public partial class CreateSite : Form{public Form2 ParentForm { get; set; }public CreateSite(){InitializeComponent();}///

/// Farm场/// public SPFarm Farm{get { return SPFarm.Local; }}/// /// 服务/// public SPWebService SPWebService {get { return Farm.Services.GetValue<SPWebService>(""); }}private void CreateApp_Load(object sender, EventArgs e){#region 加载应用程序List<ComboBoxModel> list &#61; new List<ComboBoxModel>();list.Add(new ComboBoxModel() { ID &#61; "1", Name &#61; "--请选择--" });foreach (SPWebApplication webApp in SPWebService.WebApplications){list.Add(new ComboBoxModel() { ID &#61; webApp.Id.ToString(), Name &#61; webApp.Name });}this.cbbApp.DataSource &#61; list;this.cbbApp.DisplayMember &#61; "Name";this.cbbApp.ValueMember &#61; "Id";this.cbbApp.SelectedValueChanged &#43;&#61; cbbApp_SelectedValueChanged; #endregion#region 加载模板选择//填写管理中心地址string url &#61; ConfigurationSettings.AppSettings["管理中心"].ToString();SPSite site &#61; new SPSite(url);SPWeb web &#61; site.OpenWeb();//获取创建模版集合SPWebTemplateCollection templates &#61; web.GetAvailableWebTemplates(2052);TreeNode node &#61; new TreeNode();node.Text &#61; "协作";BindTemplateTree(node, templates);this.tvTemp.Nodes.Add(node);node &#61; new TreeNode();node.Text &#61; "会议";BindTemplateTree(node, templates);this.tvTemp.Nodes.Add(node);node &#61; new TreeNode();node.Text &#61; "企业";BindTemplateTree(node, templates);this.tvTemp.Nodes.Add(node);node &#61; new TreeNode();node.Text &#61; "发布";BindTemplateTree(node, templates);this.tvTemp.Nodes.Add(node);node &#61; new TreeNode();node.Text &#61; "自定义";BindTemplateTree(node, templates);this.tvTemp.Nodes.Add(node);web.Close();site.Close();#endregion#region 配额模板//获取配额模版集合SPQuotaTemplateCollection spqc &#61; SPWebService.QuotaTemplates;List<ComboBoxModel> model &#61; new List<ComboBoxModel>();model.Add(new ComboBoxModel() { ID &#61; string.Empty, Name &#61; "无配额" });foreach (SPQuotaTemplate temp in spqc){model.Add(new ComboBoxModel() { ID&#61;temp.Value , Name&#61;temp.Name });}this.cbbTemp.DataSource &#61; model;this.cbbTemp.DisplayMember &#61; "Name";this.cbbTemp.ValueMember &#61; "Id";#endregion}/// /// 更改URL/// /// /// void cbbApp_SelectedValueChanged(object sender, EventArgs e){this.lbAppURL.Text &#61; "";if (this.cbbApp.SelectedValue !&#61; null&&this.cbbApp.SelectedValue!&#61;"1"){//获取当前的应用程序的主机头(访问映射)SPWebApplication webApp &#61; SPWebService.WebApplications[new Guid(this.cbbApp.SelectedValue.ToString())];this.cbbApp.Tag &#61; webApp;foreach (var set in webApp.IisSettings){if (set.Key &#61;&#61; SPUrlZone.Default){string HostHeader&#61;string.Empty;SPServerBinding spsb&#61;set.Value.ServerBindings[0];HostHeader&#61;spsb.HostHeader;if(HostHeader&#61;&#61;""){HostHeader&#61;"http://"&#43;System.Windows.Forms.SystemInformation.ComputerName;}this.lbAppURL.Text &#43;&#61; HostHeader &#43; ":" &#43; spsb.Port.ToString();}}}}/// /// 判断是否选择/sites//// /// /// private void checkBox1_CheckedChanged(object sender, EventArgs e){if (this.checkBox1.Checked){this.tbUrl.Text &#61; "/sites/"&#43; this.tbUrl.Text;}else{this.tbUrl.Text &#61; this.tbUrl.Text.Replace("/sites/", "");}}/// /// 创建Site/// /// /// private void button1_Click(object sender, EventArgs e){this.Cursor &#61; Cursors.WaitCursor;if (this.lbtemp.Tag &#61;&#61; null){MessageBox.Show("你未选择模版&#xff01;");return;}if (this.cbbApp.Tag &#61;&#61; null){MessageBox.Show("你未选择应用程序&#xff01;"); return;}//获取选中的模版SPWebTemplate t &#61;this.lbtemp.Tag as SPWebTemplate;//获取选择的SPWebApplicationSPWebApplication webApp &#61; this.cbbApp.Tag as SPWebApplication;//创建SiteSPSite site &#61; webApp.Sites.Add(this.tbUrl.Text.Trim(), this.tbTitel.Text.Trim(), this.tbDesc.Text.Trim(),2052,t.Name , this.tbUserOne.Text.Trim(), this.tbName.Text.Trim(), this.tbMail.Text.Trim());site.Close();this.Cursor &#61; Cursors.Default;this.Close();ParentForm.BindTree();}/// /// 绑定模版树/// /// /// void BindTemplateTree(TreeNode node,SPWebTemplateCollection temp){foreach (SPWebTemplate t in temp){if(node.Text&#61;&#61;t.DisplayCategory){node.Nodes.Add(new TreeNode() { Text &#61; t.Title, Tag &#61; t });}} }private void tvTemp_AfterSelect(object sender, TreeViewEventArgs e){this.lbtemp.Text&#61;"你已经选中&#xff1a;"&#43;e.Node.Text;this.lbtemp.Tag &#61; e.Node.Tag;if (e.Node.Tag !&#61; null){SPWebTemplate t &#61; e.Node.Tag as SPWebTemplate;lbDesc.Text &#61; t.Description;}}}public class ComboBoxModel{public string ID { get; set; }public string Name { get; set; }}
}

这一篇先到这里。下一篇写SPWeb的创建以及WebPart的创建以及动态添加到页面上以及删除。

转:https://www.cnblogs.com/StudyHard/archive/2013/05/10/3070542.html



推荐阅读
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 解决Sharepoint 2013运行状况分析出现的“一个或多个服务器未响应”问题的方法
    本文介绍了解决Sharepoint 2013运行状况分析中出现的“一个或多个服务器未响应”问题的方法。对于有高要求的客户来说,系统检测问题的存在是不可接受的。文章详细描述了解决该问题的步骤,包括删除服务器、处理分布式缓存留下的记录以及使用代码等方法。同时还提供了相关关键词和错误提示信息,以帮助读者更好地理解和解决该问题。 ... [详细]
  • 在Kubernetes上部署JupyterHub的步骤和实验依赖
    本文介绍了在Kubernetes上部署JupyterHub的步骤和实验所需的依赖,包括安装Docker和K8s,使用kubeadm进行安装,以及更新下载的镜像等。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • 本文整理了Java中com.evernote.android.job.JobRequest.getTransientExtras()方法的一些代码示例,展示了 ... [详细]
  • python中安装并使用redis相关的知识
    本文介绍了在python中安装并使用redis的相关知识,包括redis的数据缓存系统和支持的数据类型,以及在pycharm中安装redis模块和常用的字符串操作。 ... [详细]
author-avatar
彭菜菜
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有