我在visual stuadio 2008中编写了以下代码,以编程方式在sharepoint中创建一个新的Web应用程序,并在invalidOperationException下收到“由于对象的当前状态导致操作无效”等错误。
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);
SPWebApplication newApplication;
int myPort = 20002;
webAppBuilder.Port = myPort;
webAppBuilder.RootDirectory = new System.IO.DirectoryInfo("C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\" + myPort);
webAppBuilder.ApplicationPoolId = "AppPool1"; // application pool
webAppBuilder.ApplicationPoolUsername = "Anant_Raj";
System.Security.SecureString password = new System.Security.SecureString();
string strName = "#ana123";
char[] pass = strName.ToCharArray();
foreach (char c in pass)
password.AppendChar(c);
webAppBuilder.ApplicationPoolPassword = password;
webAppBuilder.CreateNewDatabase = true; // Create new database
webAppBuilder.DatabaseName = "wss_site2011_content"; // database name
webAppBuilder.DatabaseServer = webAppBuilder.DefaultZoneUri.Host; //Host name/computer name
webAppBuilder.UseNTLMExclusively = true; // Use NTLM authentication
webAppBuilder.AllowAnonymousAccess = true;
newApplication = webAppBuilder.Create(); // Create new web application
newApplication.Provision(); //Provision it into web farm
});
}
}
有什么建议吗?
答案 0 :(得分:0)
尝试这样的事情。
protected void Page_Load(object sender, EventArgs e)
{
SPWebApplicationBuilder webAppBuilder;
SPWebApplication newApplication;
// Only the objects creation in the RunWithElevatedPrivileges block
SPSecurity.RunWithElevatedPrivileges(delegate()
{
webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);
});
// other statement outside the RunWithElevatedPrivileges block
int myPort = 20002;
webAppBuilder.Port = myPort;
webAppBuilder.RootDirectory = new System.IO.DirectoryInfo("C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\" + myPort);
webAppBuilder.ApplicationPoolId = "AppPool1"; // application pool
webAppBuilder.ApplicationPoolUsername = "Anant_Raj";
System.Security.SecureString password = new System.Security.SecureString();
string strName = "#ana123";
char[] pass = strName.ToCharArray();
foreach (char c in pass)
password.AppendChar(c);
webAppBuilder.ApplicationPoolPassword = password;
webAppBuilder.CreateNewDatabase = true; // Create new database
webAppBuilder.DatabaseName = "wss_site2011_content"; // database name
webAppBuilder.DatabaseServer = webAppBuilder.DefaultZoneUri.Host; //Host name/computer name
webAppBuilder.UseNTLMExclusively = true; // Use NTLM authentication
webAppBuilder.AllowAnonymousAccess = true;
newApplication = webAppBuilder.Create(); // Create new web application
newApplication.Provision(); //Provision it into web farm
}