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;
}
OnTaskChanged事件參數可以取的目前使用者
SPUser currentUser = workflowProperties.Web.AllUsers[e.Identity];