Wednesday, March 16, 2011

The request failed. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

I tried to retrive data from Exchange Server 2010 using Exchange API 1.0 but below error fire

Error:
The request failed. The underlying connection was closed: Could not establish trust relationship  for the SSL/TLS secure channel.

Solution:
Add below code before access web service
ServicePointManager.ServerCertificateValidationCallback += delegate(object sender,
                X509Certificate certificate,
                X509Chain chain,
                SslPolicyErrors sslPolicyErrors)
            {
                return true;
            };
This Error occur when you access Secure Web Service.

Happy Coding !!!

Friday, March 11, 2011

Begin/End Async WebService Proxy Methods Not Generated in Web Application Projects

Last week I worked with Web Method's Async call from Application page on button click.


One more interesting thing, this web method takes more than 15 min to complete migrate data so End User will not sit front off screen.

Due to that reason I had to implement Async web service call.

As per my knowledge I can call Async web method using Begin-End Proxy method but this is limited for Web Site and SharePoint's application page is create using Web Application Project.

So it is difficult to implement in my Web Application Project. Then after some research & googling I found one solution from Microsoft Team.

Solution:
Open Web Application Project's .csproj file in note pad & Add below property in PropertyGroup Tag





... 

true





Then save .csproj file & update you web service in Web Application Project.


Now you can see the Begin/End Proxy methods in web application Project.

Reference here.

Happy Coding !!!

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 !!!

Create virtual directory using command prompt

If you can’t access IIS Manager (INet Manager) & want to create virtual directory then run below command for create virtual directory.
There is one Window Script File for Manage Virtual Directory IISVDir.vbs located in C:\Windows\System32\iisVdir.vbs
To Create Virtual Directory run below
Iisvdir /create “IIS Web Site Name” VirtualDirName C:\PhysicalLocation