Wednesday, June 24, 2009

The two ways of developing custom webparts using .Net

Sharepoint comes up with some out-of-the box (OOB) webparts that can be used in sharepoint applications. In addition sharepoint is flexible enough for custom webpoarts. We can create custom webparts using .net and these webparts can be used in sharepoint pages.

There are two ways to create custom webpart-
1. using User control
2. using class library project

One may ask that which method is better. As far as I am concern, when you need a complex UI in your webpart then use user control otherwise you may use class library project.

Creating webpart using User control

We need to follow few steps-

1. Create an ASP.Net web application and add an user control into it. Give some interesting name to it.
2. create your UI and write your code and build your application.
You can deploy it in two way-
a. Using smartpart- a third party tool to add user control to your sharepoint site
b. by deploying the DLL into GAC and putting your ASCX file into
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES directory.

Smartpart can be downloaded from here

Deployment using smartpart
  • Take the DLL and put it in the bin folder of your web application. i.e. inside C:\Inetpub\wwwroot\wss\VirtualDirectories\port number\BIN\ directory.

  • Create a folder named 'UserControls' inside the web application ( inside the C:\Inetpub\wwwroot\wss\VirtualDirectories\port number\ directory) and put the ascx file in that folder.

  • Open the web.config. Increase the trust level to Full. we dont need safecontrol entry for user control as it will be there for smartpart.

  • Do an IIS reset. your user control is ready to use.

  • To use it, Open the page where you want to add webpart, in edit mode. Click on 'add a webpart' in webpart zone. select smartpart from webpart zone. open the tool pane and select the user control. click on Ok. Its done now.

Deployment without Smartpart

  • Sign your web Application with strong name and build it again.

  • take your DLL and put it in GAC.

  • Take the ASCX file and put it on C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES directory.

  • Add a SafeControls entry into web.config.

  • Do an IIS reset.

  • Go to webpart gallery on your site and click on New tab.

  • Select your webpart and populate the gallery with it. Now the webpart will be in webpart gallery for use.


Creating webpart using class library project

Follow the below steps-
  • Open visual studio and create class library project.

  • write your custom code for webparts and rendering controls using CreatChildControls() and Render() methods.

  • Sign your project with strong name.

  • Build the project.

  • Put the DLL into the GAC.

  • Add the SafeControl entry into the web.config file specifying the namespace and assembly.

  • Do an IIS reset.

  • Go to webpart gallery on your site and click on New tab.

  • Select your webpart and populate the gallery with it. Now the webpart will be in webpart gallery for use.


Thats It.
Hopes it would be helpful for everyone.

Regards
Deewaker

Monday, June 1, 2009

OWA-Sharepoint Integration- A tricky solution

Hi everyone, In my recent project I had to show mails, calendar and task from exchange server on sharepoint portal. I used OWA webparts to do the same. But the problem is- OWA webparts does not show the mails, calendar and tasks instead they were showing OWA login screen. After configuring Integrated window authentication on Exchange server the problem has been resolved but within the domain. when the user logs in from outside of the domain, it doesn't works. After spending few weeks on OWA-sharepoint integration and after experiencing every alternative, I came to some tricky solution by using the below blog-

http://blogs.msdn.com/tconte/archive/2007/01/17/owa-web-part-with-single-sign-on.aspx

I used Single-sign-on solution from this blog. In this an HTML file has been used which is using an IFRAME control for OWA login form and submitting the credential automatically to the OWA login form. To enable SSO, I used below blog.

http://www.thorprojects.com/blog/archive/2008/08/02/moss-single-sign-on-setup-step-by-step.aspx

With the help of network administrator I was able to enable the SSO on sharepoint server.As we are authenticating the user from AD, we need to put all the user credentials into SSO database once. later when u will update the credential from AD, it will automatically be updated in SSO database. The problem with this solution is-the credentials will be passed as clear text in querystring. to resolve this I wrote some custom encryption method. As here the encryption will me server side and the decryption will be on client side so I did not tried to use built-in encryption provided in .Net. Instead I followed custom encryption as below-

public string EncryptCredentials(string Cred)
{
try
{
string EncryptedCred = string.Empty;
string[] arr = new string[10];
for (int i = 0; i < Cred.Length; i++)
{
arr[i] = Cred.Substring(i, 1);
}
for (int i = 0; i < Cred.Length; i++)
{
switch (arr[i])
{
case "A":
{
arr[i] = "!@#$%";
break;
}
......... and so on.
}
}
}
}
Now we need to decrypt the credentials on client side to pass it on OWA login form. I wrote some javascript decryption code to decrypt the credentials as below-

function getPassword(pwd)
{
var len=pwd.toString().length;
var arr=new Array();
var i;
var ClearPwd="";
for(i=0;i < len;i+=5)
{
arr[i]=pwd.toString().substring(i,i+5);
}
for(i=0;i < len;i+=5)
{
switch (arr[i])
{
case "!@#$%":
{
arr[i] = "A";
break;
} .......
n so on
}
ClearPwd+=arr[i];
}
}
Here the encrypted credential has been taken into array of string each having length five. This will again decrypt the credentials and that will be passed to OWA login form. Now the credential is being decrypted and can be passed to login form of OWA. Also, it will not be visible to anyone as a clear text. Its done Now. you may use the webpart now. hopes it will help. :)

Regards
Deewaker