Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How to get current Login Name.I'm getting "Sharepoint\System" as Login Name for every user. I need to get the name of the user.

public override void ItemAdded(SPItemEventProperties properties)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(properties.Web.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Sample"];
                    SPListItem item = properties.ListItem;
                    web.AllowUnsafeUpdates = true;
                    if (!item.HasUniqueRoleAssignments)
                    {
                        item.BreakRoleInheritance(false);
                    }
                    SPUser user = web.CurrentUser;//I'm getting "SharePoint\\System" as Login Name for every user
                    SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
                    roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Read"]);
                    item.RoleAssignments.Add(roleAssignment);
                    item.Update();
                    }
                }
            }
        });

}

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

Because you define the web object in the elevated mode, you'll impersonate as a system account. This means you'll always get the same person... Maybe you can store the user before you run the elevated code from SPContext.Current.Web.CurrentUser and use this user inside the elevated code (or in your case properties.Web.CurrentUser) I'm not sure this will work within an eventreceiver on a list, since these are triggered asynchonously... If you use an eventreceiver, I would get the properties of the current item... (which contains the user who last modified)

share|improve this answer
    
thanx a lot.i got a current user by SPUser user=properties.Web.CurrentUser; –  varna Oct 30 '12 at 12:23
add comment

When you RunWithElevatedPrivileges the executed code runs with Elevated Privileges, and under other Account, usually Application Pool user.

If you need to pass user account to the method, insert into a variable before calling the delegate.

  string userId = Context.CurrentWeb.RootWeb.CurrentUser; (This code is j
  SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(properties.Web.Site.ID))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists["Sample"];
                SPListItem item = properties.ListItem;
                web.AllowUnsafeUpdates = true;
                if (!item.HasUniqueRoleAssignments)
                {
                    item.BreakRoleInheritance(false);
                }
                SPUser user = web.CurrentUser;//I'm getting "SharePoint\\System" as Login Name for every user
                SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
                roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Read"]);
                item.RoleAssignments.Add(roleAssignment);
                item.Update();
                }
            }
        }
    });
share|improve this answer
add comment

You are reading the user info from an elevated SPWeb istance, so the reported user will be the one used by the application pool - this is why you are getting the "System" user name.

Just use properties.Web.CurrentUser. That should give you the non-elevated SPWeb object and thus the real current user.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.