1.获得当前用户
SPUser spuser = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context).CurrentUser;
SPUser currentUser = Microsoft.Sharepoint.SPContext.Current.Web.CurrentUser
2.获得当前站点集
SPUser spuser = Microsoft.SharePoint.WebControls.SPControl.GetContextSite();
3.spsite与spweb
spsite是服务器场上的站点集合,包括顶级站点和所有的子站点。
spweb是一个web站点,在大多数时候,是通过SPWeb对象来获取其他各种对象模型。SPWeb下包含有丰富的对象模型,包括几大部分:列表、文件系统、用户权限系统等。
http://wystec.blog.163.com/blog/static/296144472009630115746720/
第一种方法:
以下是代码片段:
|
这种方法不用获取当前的Site和Web信息,是全局使用的帐号信息。此方法获取的帐号信息是带域的。
第二种方法:
以下是代码片段:
|
这种方法可以获取指定站点指定网站下的当前登录的用户名。
http://winsystem.ctocio.com.cn/85/11664585.shtml
in infopath vsta code:
//string currentUser = System.Web.HttpContext.Current.User.Identity.Name.ToString();
//SPUser spuser = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(SPContext).CurrentUser;
SPUser spuser = SPContext.Current.Web.CurrentUser;//.Users[currentUser];
//SPUser spuser = SPContext.Current.Web.AllUsers[this.Application.User.LoginName];
get SPUser from Login Name string
SPWeb web = SPContext.Current.Web;
SPUser user=web.EnsureUser("loginname");
1. Issue:Can't get object SPUser with code behind people picker inInFopath anyway check .
currentWeb.EnsureUser(userName);
currentWeb.AllUsers[userName];
Whole 2 method are Exception .
3. Solution:
1. Add reference to dll microsoft.office.workflow.tasks (search ở C )
2. using function under to getobject SPUser from data input is login name (ex: fsdn\vinhtv; vinhtv-g5\admin..)
///
/// Gets the user.
///
/// The web.
/// Name of the user.
///
public SPUser GetUser(SPWeb web, string userName)
{
SPUser newUser = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite site = new SPSite(web.Site.ID, web.Site.SystemAccount.UserToken))
{
using (SPWeb currentWeb = site.OpenWeb())
{
Contact contact = Contact.FromName(userName, currentWeb);
// if wthe user has already accessed teh site then id will be >=0
if (contact.PrincipalID < 0)
{
currentWeb.AllowUnsafeUpdates = true;
SPPrincipal p = contact.GetPrincipal(currentWeb);
}
newUser = currentWeb.SiteUsers[contact.LoginName];
currentWeb.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception ex)
{
throw ex;
}
return newUser;
}
http://topic.csdn.net/u/20090909/16/675212fc-0577-4c1d-a24e-bff33779514c.html
OnTaskChanged事件参数可以取的当前用户
SPUser currentUser = workflowProperties.Web.AllUsers[e.Identity];