Here another way to create virtual directory in IIS site.
Everything is possible programmatically.
I created one SharePoint Feature to create virtual directory in IIS’s SharePoint site.
string _serverName = string.Empty;
string _userName = string.Empty;
string _password = string.Empty;
string _webSiteName = string.Empty;
string _physicalPath = string.Empty;
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWeb tempweb;
SPSite tempsite;
if (properties.Feature.Parent is SPWeb)
{
tempweb = properties.Feature.Parent as SPWeb;
tempsite = tempweb.Site;
}
else
{
tempsite = properties.Feature.Parent as SPSite;
tempweb = tempsite.RootWeb;
}
GetWebServiceParamFromConfig(tempweb.Url);
if (!_serverName.Equals(string.Empty) && !_userName.Equals(string.Empty) && !_password.Equals(string.Empty) && !_webSiteName.Equals(string.Empty) && !_physicalPath.Equals(string.Empty))
{
string webSiteId = GetIISWebsiteID(_serverName, _webSiteName);
String strRootSubPath = "/W3SVC/" + webSiteId + "/Root";
String strSchema = "IIsWebVirtualDir";
DirectoryEntry objRootDirectoryEntry = new DirectoryEntry("IIS://" + _serverName + strRootSubPath);
objRootDirectoryEntry.Username = _userName;
objRootDirectoryEntry.Password = _password;
objRootDirectoryEntry.RefreshCache();
DirectoryEntry objNewVDir = objRootDirectoryEntry.Children.Add("CustomWebService", strSchema);
objNewVDir.Properties["Path"].Insert(0, _physicalPath);
objNewVDir.CommitChanges();
deRoot.CommitChanges();
objNewVDir.Invoke("AppCreate", true);
objNewVDir.CommitChanges();
objRootDirectoryEntry.CommitChanges();
objNewVDir.Close();
deRoot.Close();
}
else
{
Utility.ErrorLog("During WebService Vir. Dir. Create in IIS - Param. Empty", System.Diagnostics.EventLogEntryType.Error, "CreateWebServiceVirDir - FeatureActivated()");
}
}
catch (Exception ex)
{
Utility.ErrorLog("Error during WebService Vir. Dir. Create in IIS - " + ex.Message, System.Diagnostics.EventLogEntryType.Error, "CreateWebServiceVirDir - FeatureActivated()");
}
}
public void GetWebServiceParamFromConfig(string webUrl)
{
DataSet ds = GetDataFromList(webUrl);
DataTable dtWebServiceParam = ds.Tables["WebService"];
if (dtWebServiceParam != null && dtWebServiceParam.Rows.Count > 0)
{
_serverName = dtWebServiceParam.Rows[0]["ServerName"].ToString();
_userName = dtWebServiceParam.Rows[0]["Username"].ToString();
_password = dtWebServiceParam.Rows[0]["Password"].ToString();
_webSiteName = dtWebServiceParam.Rows[0]["WebSiteName"].ToString();
_physicalPath = dtWebServiceParam.Rows[0]["PhysicalPath"].ToString();
}
}
public string GetIISWebsiteID(String server, String websiteName)
{
DirectoryEntry w3svc = new DirectoryEntry("IIS://" + server + "/w3svc");
String webId = "";
foreach (DirectoryEntry de in w3svc.Children)
{
if (de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
{
webId = de.Name;
}
}
return webId;
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
SPWeb tempweb;
SPSite tempsite;
if (properties.Feature.Parent is SPWeb)
{
tempweb = properties.Feature.Parent as SPWeb;
tempsite = tempweb.Site;
}
else
{
tempsite = properties.Feature.Parent as SPSite;
tempweb = tempsite.RootWeb;
}
GetWebServiceParamFromConfig(tempweb.Url);
if (!_serverName.Equals(string.Empty) && !_userName.Equals(string.Empty) && !_password.Equals(string.Empty) && !_webSiteName.Equals(string.Empty) && !_physicalPath.Equals(string.Empty))
{
string webSiteId = GetIISWebsiteID(_serverName, _webSiteName);
String strRootSubPath = "/W3SVC/" + webSiteId + "/Root";
String strSchema = "IIsWebVirtualDir";
DirectoryEntry objRootDE = new DirectoryEntry("IIS://" + _serverName + strRootSubPath);
objRootDE.Username = _userName;
objRootDE.Password = _password;
objRootDE.RefreshCache();
DirectoryEntry deChild = new DirectoryEntry("IIS://" + _serverName + strRootSubPath + "/CustomWebService");
objRootDE.Children.Remove(deChild);
}
else
{
Utility.ErrorLog("During WebService Vir. Dir. Delete from IIS - Param. Empty", System.Diagnostics.EventLogEntryType.Error, "CreateWebServiceVirDir - FeatureDeactivating()");
}
}
catch (Exception ex)
{
Utility.ErrorLog("Error during WebService Vir. Dir. Remove from IIS - " + ex.Message, System.Diagnostics.EventLogEntryType.Error, "CreateWebServiceVirDir - FeatureDeactivating()");
}
}
Happy Coding !!!
No comments:
Post a Comment