Tuesday, July 29, 2008

Dataformwebpart Server variables

I found this blog very helpful . its like my reference guide for passing server parameters to a dataformwebpart. Its just the one of the powerful tool I came across for custom sharepoint form development which are simple and easy to maintain.
http://dataformwebpart.com/2007/11/07/spd-2007-data-view-parameters-you-dont-know-about/

Thursday, July 10, 2008

Get Current User Email, Login, Display Name Details

I hit upon a wonderful blog while working on a requirement on getting the current user's Email id in efficient way.
The normal code which microsoft given fails:
SPWeb site = SPContext.Current.Web;
SPUser user = site.CurrentUser;
string DisplayName = user.Name;
string Login = user.LoginName;
string EMail = user.Email;
string User Notes = user.Notes;
Because most of the users who don't have access to All sites won't give details of their email by the code given. If you are wondering what is all about the difference between All users see below:
SPWeb site = SPContext.Current.Web;
SPUserCollection c1 = site.Users;
SPUserCollection c2 = site.AllUsers;
SPUserCollection c3 = site.SiteUsers;

The code gives 3 types of different user collection so i guess the user who logged in and didn't find his email by the code above belongs to one of this group.
The difference between these SPUserCollection is copied from MSDN.:

The Users collection has the smallest membership of these three collections. This collection includes all the external principals that have been explicitly assigned permissions within the current site.
The AllUsers collection includes all members of the Users collection, plus external users that have accessed objects within the site using implicit permissions through group or role membership. For example, imagine a user named Brian with the login of LITWAREINC\BrianC that has never been given explicit permissions to access a site and view a particular list. However, he might still be able to view the list because of his membership within an Active Directory group that has been configured with list view permissions. When Brian first accesses the site or one of its objects (say, a list using implicit permissions), he is added as a member of the AllUsers collection, but he is not added as a member of the Users collection.
The SiteUsers collection is an aggregation that combines membership for each AllUsers collection within the current site collection. The membership of this collection includes all external principals that have been assigned permissions to any object within the site collection as well as all external users that have been granted access to any of the site collection's objects using implicit permissions.

I used a basic way to get the current user using (context of control HTTPCONTEXT) Context.User.Identity.Name or Page.User.Identity.Name which does the same httpcontext.

After we get the current user login i can pass it to the magic of another Class in sharepoint object Model which brings the user details.

using Microsoft.SharePoint.Utilities;

SPWeb osite = SPContext.Current.Web;

SPPrincipalInfo prin = SPUtility.ResolvePrincipal(osite,
Context.User.Identity.Name , SPPrincipalType.All, SPPrincipalSource.All, osite.AllUsers, false);
writer.Write(prin.Email);


using this method u can also search the user by their Email ID, or their Display name. its a cool method who does the search on multiple fields.

Tuesday, July 1, 2008

Use explicit casting instead of DataBinder.Eval

The DataBinder.Eval method uses .NET reflection to evaluate the arguments that are passed in and to return the results. Consider limiting the use of DataBinder.Eval during data binding operations in order to improve ASP.NET page performance.
Consider the following ItemTemplate element within a Repeater control using DataBinder.Eval:
<ItemTemplate>

<tr>


<td><%# DataBinder.Eval(Container.DataItem, "field1") %></td>

<td><%# DataBinder.Eval(Container.DataItem, "field2") %></td>


</tr>


</ItemTemplate>
Using explicit casting offers better performance by avoiding the cost of .NET reflection. Cast the Container.DataItem as a DataRowView:
<ItemTemplate>


<tr>


<td><%# ((DataRowView)Container.DataItem)["field1"] %></td>

<td><%# ((DataRowView)Container.DataItem)["field2"] %></td>


</tr>


</ItemTemplate>

Thread Safe .NET Event Technique

Event Implementation should be done like this :

public event EventHandler Updated = delegate { };

protected void UpdatePrice(string mySymbol, decimal newPrice, long newVolume)
{
_priceList[mySymbol] = newPrice;
_volumeList[mySymbol] = newVolume;

Updated(this, new MarketFeedEventArgs(mySymbol, newPrice, newVolume));
}


Because in case of multithreaded environment if the event updated is not subscribed or if null it. Removes the subscription from all the other event handler. So instead of making a new copy of event each time, we should initialize the events at the first case.

People normally use like this which is performance friendly but not good practice for multithreaded environ
public event EventHandler Updated;

protected void UpdatePrice(string mySymbol, decimal newPrice, long newVolume)
{
_priceList[mySymbol] = newPrice;
_volumeList[mySymbol] = newVolume;

if(Updated != null)
Updated(this, new MarketFeedEventArgs(mySymbol, newPrice, newVolume));
}

How to check email works without using SMTP

This is tip is helpful to check the sending of emails from your code without actually installing SMTP. Maybe in your local development machine.

You can find your mails posted in the directory mentioned below after you send a mail by code.

<system.net>
<mailsettings><smtp deliverymethod="SpecifiedPickupDirectory"> <specifiedpickupdirectory pickupdirectorylocation="c:\Test\">
</smtp>
</mailsettings>
</SYSTEM.NET>

Source of this tip : http://dotnettipoftheday.org/tips/smtp-delivery-methodSpecifiedPickupDirectory.aspx