Thursday, October 29, 2009

SharePoint coding tips and tricks

The elementary thing that SharePoint developers should consider is, the correct way of using SharePoint object mode, and how to apply general programming techniques (such as caching and threading) to the SharePoint platform specifically. Generally developers who write custom code for developing the SharePoint Application will counter some issues like performance, scalability. This article makes you easier to find and fix your problem areas in the code and can avoid known pitfalls using SharePoint object model.

These are some of the areas that developers should concern
· Disposing of SharePoint Objects.
· Performance concerns related to folders, lists and SPQuery objects.
· Exact way of using SharePoint data and objects efficiently.
· Using web controls and timer jobs.

Disposable SharePoint objects:

1. Developers who are working on the windows SharePoint service object model for developing new SharePoint applications have objects that inherited from the IDisposable interface.
2. This article serves as a guide to the proper procedures for handling and disposing of SharePoint objects that implement IDisposable interface.
3. Some of the primary objects are SPWeb class and SPSite class, created as managed objects.
4. Microsoft .NET requires objects that implement the IDisposable interface to properly cleanup the unmanaged resources by explicitly calling the Dispose () or Close () method when you are finished using them.
5. Internally, SPSite and SPSite both hold references to an "internal class Microsoft.SharePoint.Library.SPRequest" which holds on to unmanaged COM resources.

Coding techniques to ensure Object Dispose:
These are some of the techniques to ensure the Object Dispose
· Dispose Method
· Using Clause
· Try, catch and finally blocks

Dispose vs. Close Method:

Both of them are same, but the dispose method internally calls the close method. For the best practice, use Dispose method instead of using close method because SPWeb and SPSite Objects implements IDisposable interface, and standard .NET framework garbage collector call the dispose method to free any resources associated with the object from memory.
Using Clause

1. Using clause will automatically dispose the SharePoint Objects. You can greatly simplify the code with using clause.
2. Common Language Run Time will convert the using clauses to try, catch and finally block, and any objects that implement the IDisposable interface are disposed for you.

Note: SPContext objects are managed from SharePoint framework and need not be disposing separately.

Try, catch and finally block:

Whenever you need to handle the exceptions we obviously use Try, catch and finally blocks. Please don’t leave the catch block empty. Declare Dispose method in the finally block, and make a note that for best practice check null before disposing the SharePoint objects as shown below.

SPSite spSite = null;
SPWeb spWeb = null;
try
{
spSite = new SPSite ("http://server");
spWeb = spSite.OpenWeb ();
}
catch (Exception e)
{

}
finally
{
if(spSite != null)
{
spSite.Dispose();
}
if (spWeb != null)
{
spWeb.Dispose ();
}
}

Respose.Redirect, with try, catch and finally block and using statements:

1. The finally block executes after calls to Response.Redirect in the try block. Response.Redirect ultimately generates a ThreadAbortException.
2. When this exception is raised, the runtime executes all finally blocks before ending the thread.
3. However, because the finally block can do an unbounded computation or cancel the ThreadAbortException, there is no guarantee that the thread will end.
4. Therefore, before any redirection or transfer of processing can occur, you must dispose of the objects. If your code must redirect, implement it in a way similar to the following code example.
SPSite spSite = null;
SPWeb spWeb = null;
try
{
spSite = new SPSite ("http://server");
spWeb = spSite.OpenWeb ();

if (spSite != null)
{
spSite.Dispose();
}
if (spWeb != null)
{
spWeb.Dispose ();
}
Response.Redirect ("www.google.com");
}

catch (Exception ex)
{

}

finally
{
if (spSite != null)
{
spSite.Dispose();
}
if (spWeb != null)
{
spWeb.Dispose ();
}
}

Performance concerns related to folders, lists and SPQuery objects:

1. To retrieve the all items from list, please don’t use SPList.Items.
2. SPList.Items will retrieve all the items from folders, subfolders including all fields in the list.
3. To eradicate this Problem use SPList.GetItems(Query).
4. Getting Items by identifier, Please use SPList.GetItemById(int) instead of using SPList.Items.GetItemById().
5. To retrieve the list from the site, It is always preferable to get the list based GUID or url i.e. SPWeb.Lists[GUID] or SPWeb.GetList(strUrl) instead of using List Name i.e., SPWeb.Lists[strListName].

Conclusion:
· Please check whether your code dispose the SharePoint objects.
· Please check that your code cache objects properly.
· Please check that your code use thread synchronization when necessary.

If you consider these issues when you write your code, your SharePoint system will run more efficiently and your users will have a much better experience. You can also help to prevent unexpected failures and errors in your system.

Wednesday, October 7, 2009

Installation of ADAM in Microsoft Operating systems

Active Directory Application Mode (ADAM) is an LDAP directory service designed specifically for directory-enabled applications.
If we want to provide access to our SharePoint Site to external parties or clients without providing them a Windows Active Directory Account, ADAM will be crucial.

ADAM in different Microsoft Operating system:

  1. Windows server 2003: By default, w2k3 does not provide any ADAM installed in it.We need download the executeable file at http://www.microsoft.com/downloads/details.aspx?FamilyId=9688F8B9-1034-4EF6-A3E5-2A2A57B5C8E4&displaylang=en
  2. Windows server 2003 r2: We need to follow some steps as shown below
  • Open Add or Remove Programs from Control Panel.
  • Click Add/Remove Windows Components.
  • Click Active Directory Services under Components, and then click Details.
  • Select the Active Directory Application Mode (ADAM) check box under Subcomponents of Active Directory Services, and then click OK.
  • Click Next on the Windows Components page.

3. Windows server 2008: By default, they provided us the ADAM.

Go to start --> Adminstration Tools --> click Active Directory Light Weight Directory setup wizard.

Thats all...

c# difference between the CurrentUser and AllUsers["username"] in sharepoint

When I am trying to retrieve the sharepoint site sign in user details from SharePoint Peoples and user group, I have faced some confusions as discussed below.

For example consider that we need the email address of the current user.According to my requirement i used property shown below

SPContext.Current.Web.CurrentUser.EMail returns only the current Sign in User email address

and CurrentWeb.AllUsers[Properties.UserLoginName].Email also returns email address.

AllUsers returns all users who are either members of the site or who have browsed to the site as authenticatedmembers of the domain group in the site.

CurrentUser returns only the current sign in user details.

Conclusion:

  1. If we want to current user details, we can use both properties as shown above.
  2. If we want the user details of particular user other than current user we need to use the AllUsers property.

Monday, October 5, 2009

Electronic signature in Share Point Document library

DocuSign eSignature services has been helping several industries sign documents electronically, eliminating the need for printing/Faxing.
ESIGN Documents in SharePoint 2007 feature uses DocuSig to enable the SP users sign documents or send signature requests from a document library without downloading a copy.

Friday, September 18, 2009

Configure the FTP services

This step-by-step article describes how to install and configure a File Transfer Protocol (FTP) server for anonymous access

FTP depends on IIS (Internet Information Services), without IIS we cannnot configure FTP to the server.

1)Click Start, point to Control Panel, and then click Add or Remove Programs.
2)Click Add/Remove Windows Components.
3)In the Components list, click Application Server, click Internet Information Services (IIS) (but do not select or clear the check box), and then click Details.
4)Click to select the following check boxes (if they are not already selected):
Common FilesFile Transfer Protocol (FTP) ServiceInternet Information Services Manager
4)Click to select the check boxes next to any other IIS-related service or subcomponent that you want to install, and then click OK.
5)Click Next.
6)When you are prompted, insert the Windows Server 2003 CD-ROM into the computer's CD-ROM or DVD-ROM drive or provide a path to the location of the files, provide the path accordingly and then click OK.
7)Click Finish.
8)Open IIS (Internet Information Services) or else Go to run and type "inetmgr" to open IIS.
9)Expand Server_name, where Server_name is the name of the server.
10)Expand FTP Sites
11)Right-click Default FTP Site, and then click Properties.
12)Click the Security Accounts tab.
13)Click to select the Allow Anonymous Connections check box (if it is not already selected), and then click to select the Allow only anonymous connections check box. When you click to select the Allow only anonymous connections check box, you configure the FTP Service to allow only anonymous connections. Users cannot log on by using user names and passwords.
14)Click the Home Directory tab.
15)Click to select the Read and Log visits check boxes (if they are not already selected), and then click to clear the Write check box (if it is not already cleared).
16)Click OK.

Copy or move the files that you want to make available to the FTP publishing folder for access. The default folder is drive:\Inetpub\Ftproot, where drive is the drive on which IIS is installed.

Hopes this help you a lot....

Tuesday, September 8, 2009

SharePoint 2007 - Import from Spreadsheet to List

Import from Spreadsheet is the missing facet to the SharePoint 2007 List features. SharePoint let's a user create custom list from a spreadsheet, but lacks the ability to import data to an existing SP list. This feature developed in C# .Net and easy to install will do just that.

Here is the link http://spreadsheet2splist.codeplex.com

Friday, September 4, 2009

SharePoint web.config modification programmatically using c#

Here is the sample code to customize the web.config file for the SharePoint site programmatically
spSite = new SPSite(http://intranet.demo.com);
SPWeb spWeb = spSite.OpenWeb();
SPWebService service = SPWebService.ContentService;
SPWebApplication webApp = new SPSite("http://intranet.demo.com").WebApplication;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", spSite.WebApplication.Name);
XmlDocument doc = new XmlDocument();
doc.Load(configFilePath);
AppSettingsSection appSettings = config.AppSettings;
if (appSettings.ElementInformation.IsPresent)
{
XmlNode nodekey = doc.SelectSingleNode("configuration/appSettings/add[@key='key']");
if(nodekey == null)
{
SPWebConfigModification myModification = new SPWebConfigModification();
myModification.Path = "configuration/appSettings";
myModification.Name ="add[@key='userName']";
myModification.Sequence = 0;
myModification.Owner = "User Name";
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
myModification.Value =
";
service.WebConfigModifications.Add(myModification);
}
}
/*Call Update and ApplyWebConfigModifications to save changes*/
service.Update();
service.ApplyWebConfigModifications();
As you can see, it is pretty simple and if you want to apply to the site spWeb.Site.WebApplication.WebConfigModifications.Add(MyModification)
If you want to apply to the whole farm
webApp.Farm.Services.GetValue().ApplyWebConfigModifications();
wenApp.Update();