在创建的时候注意你要有权限。还有如果要使用请注意合理释放资源,因为我是随便写的 就没有去考虑资合理问题。
首先来写怎么去通过代码创建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 CreateSite](https://img7.php1.cn/3cdc5/ca23/3b4/ee74989ee240a590.jpeg)
然后附上代码:
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的创建以及动态添加到页面上以及删除。