Showing posts with label SharePoint Feature. Show all posts
Showing posts with label SharePoint Feature. Show all posts

Thursday, March 10, 2011

Using SharePoint Feature Receiver create Virtual Directory in IIS

As per my previous post you create virtual directory from command line.
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 !!!

Tuesday, January 18, 2011

Error occurred in deployment step 'Add Solution': Failed to extract the cab file in the solution.

Error Message: Error occurred in deployment step 'Add Solution': Failed to extract the cab file in the solution.

One day I tried to deploy Images in _layouts folder using VS 2010. But I couldn't deploy Images because above error message fired in VS2010's error window. After half and hour I found one file named with blue_button (2).gif and renamed with blue_button.gif and deploy solution. Its working fine.

Resolution: To work around this problem, remove any parentheses in the names of SharePoint project items.

This link help me to solve the problem http://msdn.microsoft.com/en-us/library/ee330922.aspx.

Happy Coding !!!

Friday, December 31, 2010

Register Javascript using SharePoint Feature

You can register JavaScript and CSS without change your SharePoint sites master page.Follow below steps to register JavaScript in master page.

  • Open VS 2010 & create one blank SharePoint 2010 Project.

  • Then Add New Item & select SharePoint 2010’s “Empty Element” and give name “JQueryRegisterElement” as below.

  • Then right click on project and add “SharePoint Mapped Folder” & select “Control Templates” as below.Then add one Usercontrol in this Control Templates folder.

  • Now open Element.xml and add below code for register JavaScript.










Then open UserControl



  • Then Right click on project, Deploy it & open SharePoint Site. You will see below popup.

Happy Coding !!!