How to correctly ensure users in Sharepoint
Everyone uses SPUser user = SPWeb.EnsureUser(…) to make sure the user exists in thier sharepoint site. I have probably spent the better part of this afternoon trying to figure out why the Sharepoint administrator can add users but any other admin users get the error “Access denied” when the the user is ensured. I eventually posted my problem on the Sharepoint Developer forums and I was given the following solution.
SPWeb web = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(web.Site.ID))
{
SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID);
SPUser user = elevatedWeb.EnsureUser(UserLoginName);
}
})
Apparently, the sharepoint administrator account is assigned specific privilages that are not granted to any other admin user, and to get them you need to use SPSecurity.RunWithElevatedPrivilages().
It seems strange but there you go, the correct way to ensure your site user.
Editing user permissions in Sharepoint
I have recently taken over support and development of a sharepoint solution for a client, and I have been working my way through a massive list of errors that the client sent me. Now, I don’t know sharepoint from a bar of soap. I know what it is, but that is it.
So I have been learning as I go along, and I came across a weird bug from the client. When they edited their sub site assignments, it would throw the following error: “The user is not unique or does not exist”.
After hours of tearing my hair out, swearing and shouting at anything that moved, i discovered, much to my astonishment, that you can not edit the permissions of the current logged in user. So for me to edit my user permissions, I would need to login with a different administrator account, edit my permissions then log back in. I can understand not allowing the logged in user to be deleted but stopping me from editing my assigned sub sites is a bit stupid.
So when you get that error, first then to check is what are you doing? and is it allowed by sharepoint? even though the error message given is not pointing in that direction!

