Showing posts with label get SPUser từ Login Name string. Show all posts
Showing posts with label get SPUser từ Login Name string. Show all posts

get SPUser from Login Name string

0 nhận xét
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;
}


Rource:VinhTv