Operation is not valid due to the current state of the object?
I have written following code in visual stuadio 2008 to crete a new web application in sharepoint programmatically and getting following error like "Operation is not valid due to current state of the object" under 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.UseNTLMExclu开发者_运维问答sively = true; // Use NTLM authentication
webAppBuilder.AllowAnonymousAccess = true;
newApplication = webAppBuilder.Create(); // Create new web application
newApplication.Provision(); //Provision it into web farm
});
}
}
Any suggestion?
Try something like this.
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
}
精彩评论