SharePoint - How to get current user - Asked By Bookworm on 11-Jan-12 04:34 AM

Hi, i'm developing a web part
and i need to take the current user and to compare with the user that created the item?

any idea pls?????????
kalpana aparnathi replied to Bookworm on 11-Jan-12 04:46 AM
hi,
Try this code:

SPContext currentContext=SPContext.Current;
string uName=string.Empty;
 
if (currentContext != null && currentContext.Web.CurrentUser != null)
{
    SPWeb web = SPControl.GetContextWeb(currentContext);    
    uName = web.CurrentUser.LoginName;
}
 
else
{
    uName = System.Web.HttpContext.Current.User.Identity.Name;
}
D Company replied to Bookworm on 11-Jan-12 04:48 AM
Hello BookWorm,

to get the current user you can use the below code snippet


Should return a current logged in user.

SPUser cuser = SPControl.GetContextWeb(Context).CurrentUser;

Now you second requirement is compare with user who has created the item.
for this you need to explan how you have creted the items for your webpart and what kind of items are created.and what is the purpose of comparison does it for access rights.

i can suggest you here to compare

//if you want to compare user group
bool IsMember(string groupName) 
   
{ 
       
try 
       
{ 
           
return SPContext.Current.Web.SiteGroups[groupName].ContainsCurrentUser; 
       
} 
       
catch 
       
{ 
           
return false; 
       
} 
   
} 
Note: same thing you can do for a user also in palce ogf group pass user

let us know in case you need more help on this.

Regards
D

Bookworm replied to kalpana aparnathi on 11-Jan-12 05:08 AM
thnx i solve it
Bookworm replied to D Company on 11-Jan-12 05:08 AM
thnx
Asked By Bookworm on 11-Jan-12 05:09 AM
thnx
kalpana aparnathi replied to Bookworm on 11-Jan-12 05:14 AM
your welcome!
D Company replied to Bookworm on 11-Jan-12 06:28 AM
Hello Friend,

you are most welcome.



Regards
D