Tuesday, December 30, 2008

Windows Identity Impersonation

Sometime in window application it is required that it run in context of user account other than current login account.Few condition that required this is

1) Window Application that authenticate and authorize user by his/her user account.

2) SharePoint sometime required this to gain access of resource that is not granted for SharePoint users.

You can use following code to impersonate particular user.

[System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(
String lpszUsername,
String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);
        [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);

        static void Main(string[] args)

            IntPtr usertoken = new IntPtr(0);
            bool b = LogonUser("test", "local", "123", 2, 0, ref usertoken);
            if (b == true)
                System.Security.Principal.WindowsIdentity iden = new System.Security.Principal.WindowsIdentity(usertoken);
                System.Security.Principal.WindowsImpersonationContext con = iden.Impersonate();

// You Code that required impersonation




Note : Impersonation can be used in any type of application. (Window Application , Web Application , Class Library).

In Web Application web.config contains specific <identity> attribute for impersonation.

Linq to SharePoint List. (Where , Join )

This article give you information regarding how to use Liinq with SharePoint List to fetch data from SPList.

For Example there are three SharePoint List . Customers , Orders , OrderDetails. (Please See image below). In Orders list CustomerID is lookup field and in OrderDetails list OrderID is lookup field.



Order Details

Now Get All Customer From Customer List using Linq.

SPSite site = new SPSite("http://localhost:45833"); 
SPWeb web = site.OpenWeb();
SPList lstCustomer = web.Lists["Customers"];

var customers = from SPListItem customer in lstCustomer.Items
                       select customer;

by use of SPListItem (As we use select customer in Linq query)
foreach (SPListItem item in customers)
Console.WriteLine(item.ID.ToString() + "-" +item["CustomerName"].ToString());

or by using ananymous type

var customers = from SPListItem customer in lstCustomer.Items
                       select new {ID = customer.ID.ToString(), Name= customer[“CustomerName”].ToString()};
foreach (var item in customers)
Console.WriteLine(item.ID + "-" + item.Name);

use of where clause with this

var customers = from SPListItem customer in lstCustomer.Items
                       where customer.ID == 1
                       select new { ID = customer.ID.ToString(), Name = customer["CustomerName"].ToString() };

Join Three List to get Related Data

SPList lstCustomer = web.Lists["Customers"];
SPList lstorders = web.Lists["Orders"];
SPList lstorderdetails = web.Lists["OrderDetails"];
var customerorders =

from SPListItem customer in lstCustomer.Items
join SPListItem order in lstorders.Items on customer.ID.ToString() equals order["CustomerID"].ToString().Split(new string[]{";#"},StringSplitOptions.None)[0]
join SPListItem orderdetail in lstorderdetails.Items on order.ID.ToString() equals orderdetail["OrderID"].ToString().Split(new string[] { ";#" }, StringSplitOptions.None)[0]                                
select new {Product = orderdetail["Product"].ToString() , Price = Convert.ToDouble(orderdetail["Price"].ToString()) , Qty = Convert.ToInt32(orderdetail["Qty"].ToString()) , OrderID = order.ID , OrderDate = order["OrderDate"].ToString() , CustomerID = customer.ID.ToString(), CustomerName = customer["CustomerName"].ToString() };

foreach (var item in customerorders)
                   Console.WriteLine(item.Product + "  " + item.Price.ToString() + "," + item.Qty.ToString() + ","+ (item.Price * item.Qty).ToString() + ","+ item.OrderID.ToString() + "," + item.OrderDate + "," + item.CustomerName);

                  Note :In above code, I used split operation on string in order to compare LookUp Field ID Value with ID of parent List. There is also posibility to join more than three tables.

For that you have to use nested this join inside other. Please give your comment or advice on this.

Wednesday, December 17, 2008

MCTS 70-528 Book update (MCTS Self-Paced Training Kit (Exam 70-528))

MCTS book update. If you have book called MCTS Self-Paced Training Kit (Exam 70-528): Microsoft .NET Framework 2.0 Web-Based Client Development, then you have to look at following correction.



Monday, December 15, 2008

MCTS Self-Paced Training Kit Exam 70-536 Correction and Updates link.

MCTS 70-536 Self-Paced Training kit contains some wrong information. Microsoft support site provides updates in book and materials associated with book. (Like CD, Practice Test)

Part 1 : http://support.microsoft.com/kb/923018

Part 2 : http://support.microsoft.com/kb/935218

Part 3 : http://support.microsoft.com/kb/949730/

Part 4 : http://support.microsoft.com/kb/949734/

Friday, December 5, 2008

New controls for .NET Framework 3.5 SP1

This is really a nice thing that Microsoft did in .NET Framework 3.5 SP1, Introduce really cool Chart Controls. This chart controls is for Windows as well as Web Application.

You can download it from Microsoft Site.

Link : Download Chart Control

In related download you can find document and VS 2008 Add-on for this control.