Saturday, November 7, 2015

Convert datatable to List collection in C#.net


public static List<T> ToCollection<T>( DataTable dt)
{
List<T> lst = new System.Collections.Generic.List<T>();
Type tClass = typeof(T);
PropertyInfo[] pClass = tClass.GetProperties();
List<DataColumn> dc = new List<DataColumn>();
dc=  dt.Columns.Cast<DataColumn>().ToList();
T cn;
foreach (DataRow item in dt.Rows)
{
cn = (T)Activator.CreateInstance(tClass);
foreach (PropertyInfo pc in pClass)
{
// Can comment try catch block.
try
{
DataColumn d = dc.Find(c => c.ColumnName == pc.Name);
if (d != null)
pc.SetValue(cn, item[pc.Name], null);
}
catch
{
}
}
lst.Add(cn);
}
return lst;
}

Simple way to validate a Sharepoint 2010 pepoleeidtor control by required filed validator.


<SharePoint:PeopleEditor ID=”peBuyerName” runat=”server” AllowEmpty=”false” MultiSelect=”false”
ValidatorEnabled=”true” Width=”250px” SelectionSet=”User,SPGroup” ShowCreateButtonInActiveDirectoryAccountCreationMode=”true” />

: here is the validator
<asp:RequiredFieldValidator EnableClientScript=”true” ID=”RequiredFieldValidator1″
runat=”server” ControlToValidate=”peBuyerName” ErrorMessage=”Please enter a valid buyer name!”
ToolTip=”” ValidationGroup=”btnsubmit”></asp:RequiredFieldValidator>

……………enjoy

Get user name from a peoplegroup column “Sharepoint 2010”


public List<string> getUserNameFromPeopleGroup(SPListItem ListItem, string PeopleGroup, SPWeb objWeb)
{
List<string> strLoginName = new List<string>();
SPFieldUserValueCollection users = ListItem[PeopleGroup] as SPFieldUserValueCollection;
if (users != null)
{
foreach (SPFieldUserValue userVal in users)
{
if (userVal.User != null)
{
SPUser user = userVal.User;
strLoginName.Add(user.Name);
}
}
}
return strLoginName;
}

simple way to disable sharepoint 2010 ribbon.

public void HideRibbon(System.Web.UI.Page page)
{
SPRibbon current = SPRibbon.GetCurrent(page);
if (current != null)
{
current.CommandUIVisible = false;
current.Visible = false;
}
}


And call this function:
HideRibbon(this.Page);