Search This Blog

Tuesday, March 22, 2011

GroupHelper

public class GroupHelper
{

public GroupHelper()
{
//
}
private string _GroupName, _LoginGroupTypeID;

public static string LoginUSername
{
get
{


SPSite spSite; SPWeb spWeb;

using (spSite = new SPSite(SPContext.Current.Site.Url))
{
using (spWeb = spSite.OpenWeb())
{
SPUser sUser = spWeb.CurrentUser;
//string str = sUser.Name + sUser.LoginName + sUser.Email + sUser.Groups.Count;
return sUser.LoginName;
}
}



}

}

public static string DisplayName
{
get
{


SPSite spSite; SPWeb spWeb;

using (spSite = new SPSite(SPContext.Current.Site.Url))
{
using (spWeb = spSite.OpenWeb())
{
SPUser sUser = spWeb.CurrentUser;
//string str = sUser.Name + sUser.LoginName + sUser.Email + sUser.Groups.Count;
return sUser.Name;
}
}



}

}


public static string USername
{
get
{
SPSite spSite; SPWeb spWeb;

using (spSite = new SPSite(SPContext.Current.Site.Url))
{
using (spWeb = spSite.OpenWeb())
{
SPUser sUser = spWeb.CurrentUser;
string str = sUser.Name + sUser.LoginName + sUser.Email + sUser.Groups.Count;
string[] name = sUser.Name.Split('\\');
if (name.Count() != 1)
{
return name[1].ToString();
}
else
{
return sUser.Name;
}
}
}



}

}


ArrayList _GroupList;

public ArrayList GroupList
{
get { return _GroupList; }
set { _GroupList = value; }
}
public string LoginGroupTypeID
{
get { return _LoginGroupTypeID; }
set { _LoginGroupTypeID = value; }
}

public string GroupName
{
get { return _GroupName; }
set { _GroupName = value; }
}
private string _ApproverGroupname;

public string ApproverGroupname
{
get { return _ApproverGroupname; }
set { _ApproverGroupname = value; }
}
public string GetCurrentUserGroup()
{
SPSite spSite; SPWeb spWeb;

using (spSite = new SPSite(SPContext.Current.Site.Url))
{
using (spWeb = spSite.OpenWeb())
{
SPUser sUser = spWeb.CurrentUser;

GroupList = new ArrayList();
string str = sUser.Name + sUser.LoginName + sUser.Email + sUser.Groups.Count;
foreach (SPGroup grp in sUser.Groups)
{
GroupName = grp.Name;
GroupList.Add(grp.Name);
}

}
}
return GroupName;
}
public static string GetFullName(string strLogin)
{
string str = "";
string strDomain;
string strName;

// Parse the string to check if domain name is present.
int idx = strLogin.IndexOf('\\');
if (idx == -1)
{
idx = strLogin.IndexOf('@');
}

if (idx != -1)
{
strDomain = strLogin.Substring(0, idx);
strName = strLogin.Substring(idx + 1);
}
else
{
strDomain = Environment.MachineName;
strName = strLogin;
}

DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
object obVal = coll["Display name"].Value;
str = obVal.ToString();
}
catch (Exception ex)
{
str = ex.Message;
}
return str;
}
public static string GetCompleteName(string loginname)
{
string RequestorFullName = string.Empty; ;
//try
//{
// string[] name = loginname.Split('\\');
// if (name.Count() != 1)
// {
// DirectoryEntry de = new DirectoryEntry("WinNT://" + name[0] + "/" + name[1]);
// return de.Properties["fullName"].Value.ToString();
// }
// else
// {
// return name.ToString();
// }
//}
// catch { return null; }

try
{
SPSite spSite; SPWeb spWeb;

using (spSite = new SPSite(SPContext.Current.Site.Url))
{
using (spWeb = spSite.OpenWeb())
{
SPUserCollection us = spWeb.SiteUsers;

foreach (SPUser item in us)
{
if (loginname == item.LoginName)
{
RequestorFullName = item.Name;
}


}

}

}


}

catch
{
return null;
}
return RequestorFullName;
}


public string GetRequestoreApproverGroup(string Groupname)
{
SPSite spSite; SPWeb spWeb; SPList spList;

using (spSite = new SPSite(SPContext.Current.Site.Url))
{
using (spWeb = spSite.OpenWeb())
{
spList = spWeb.Lists["DET_MappingGroup"];
var query = from SPListItem pi in spList.Items
where (pi["RequestorGroup"].ToString() == Groupname.ToString())
select pi["ApprovarGroup"];
if (query != null)
{
if (query.Count() > 0)
{
ApproverGroupname = query.Single().ToString();
}
}

}
}
return ApproverGroupname;
}

public string GetCurrentUserLoginGroup(string Groupname)
{
SPSite spSite; SPWeb spWeb; SPList spList;

using (spSite = new SPSite(SPContext.Current.Site.Url))
{
using (spWeb = spSite.OpenWeb())
{
spList = spWeb.Lists["DET_Semi Group"];
var query = from SPListItem pi in spList.Items
where (pi["GroupName"].ToString() == Groupname.ToString())
select pi;
if (query != null)
{
if (query.Count() > 0)
{
foreach (var item in query)
{
LoginGroupTypeID = item["GroupTypeID"].ToString();
GroupName = item["GroupName"].ToString();
}
}
}

}
}
return LoginGroupTypeID;
}
public bool IsApprover(string groupname, int workorderrowid, bool IsExit)
{
SPSite spSite; SPWeb spWeb; SPList spList;

using (spSite = new SPSite(SPContext.Current.Site.Url))
{
using (spWeb = spSite.OpenWeb())
{
spList = spWeb.Lists["DET_WorkOrders"];
var query = from SPListItem pi in spList.Items
where pi.ID.ToString() == workorderrowid.ToString()
select pi["ApproverGroup"];
if (query != null)
{
if (query.Count() > 0)
{
ApproverGroupname = query.Single().ToString();
if (groupname == ApproverGroupname)
{
IsExit = true;
}
}
}

}
}
return IsExit;
}
}

No comments:

Post a Comment