Question

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();
                    }
                }
            }
        });

}

Was it helpful?

Solution

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)

OTHER TIPS

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();
                }
            }
        }
    });

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top