I am new to sharepoint and i need to run this Code:

            using (SPSite site = new SPSite("Site URL"))
                using (SPWeb web = site.OpenWeb())

                    SPUser user = web.SiteUsers.GetByEmail("OlduseremailId@email@sunpowercorp.com");
                    user.Email = "NewUserEmilId@email.com";

where and how do i run this code?

  • 2
    Console application in Visual Studio? – eirikb Apr 24 '14 at 13:46
  • Doesn't RWEP run in SharePoint context where the code is elevated to app pool account previliges. I think it doesn't have any impact in console app. – Nadeem Yousuf Apr 24 '14 at 15:02
  • 2
    Please stay away from RWEP. It is not necessary. See schaeflein.net/Pages/… – Paul Schaeflein Apr 24 '14 at 15:54
  • If you want to run this code as it is your can create a aspx page and deploy (wsp) to _layouts folder. Or create a webpart that can host the code. If your "problem" is how to update the email adderess than please specify. there are various options as already mentioned below. – Marco Scheel Apr 25 '14 at 12:03

You could follow eirikb's suggestion and create a Console Application project in VS to run it.

Or you could scrap the whole RWEP bit and just use the SharePoint 20xx Management Shell / PowerShell since it's a one off run.

$site = Get-SPSite "http://yoursite"
$user = $site.RootWeb.SiteUsers.GetByEmail("theemail@domain.com")
$user.Email = "thenewemail@domain.com"
  • Doesn't RWEP run in SharePoint context where the code is elevated to app pool account previliges. I think it doesn't have any impact in console app – Nadeem Yousuf Apr 24 '14 at 15:02
  • @NadeemYousuf It wouldn't have any effect in a console app because it only changes identity where there is a valid SPContext object. It's much better practice to use an impersonated SPSite with the overloaded constructor that takes a user token. Here's the skinny on impersonation: extreme-sharepoint.com/2012/05/30/… – RJ Cuthbertson Apr 24 '14 at 16:00

I agree with RJ's answer - if you need to do it programmatically, Powershell is the easier option. However, if you just need to update the email address on the one user, you could also do it manually in the user's profile via Central Administration (assuming you have access).

Something to keep in mind is that by default the email address is pulled down from Active Directory (or whatever your authentication provider is) when the profile sync runs. If the address is wrong in AD, it will need to be updated there first - otherwise you can update it manually in the SharePoint profile, but it will most likely be overwritten by the incorrect one from AD again.

If it's wrong in both places, just correct it in AD and run a full profile sync. An incremental should catch the change, but in my experience it doesn't always happen.

Note: I'm assuming here that you're a SharePoint Admin trying to update a user's email address. If the code is part of an application or a custom form, then obviously that's a different story altogether.


If it's SharePoint Server (and not Foundation) avoid the code all together and update the user's Profile and let the Profile Sync Timer Jobs run to update the address in the User Info Lists.


If you are on foundation or on server sku with changes not replicating to the site collection you can use this powershell command to "resync" the current values from ad:

Set-SPUser -> http://technet.microsoft.com/de-de/library/ff607827(v=office.15).aspx

#get spuser an snc from ad $web = Get-SPWeb http://thewebapp/sites/therootsite $web.SiteUsers.GetByEmail("OlduseremailId@email@sunpowercorp.com") Set-SPUser $spu -SyncFromAD

If you want a user (not rooted in ad like an extranet) to allow to update his mail address you could create a systempage and deploy the page into the layouts directory (using a WSP). The other option is to use a webpart and place the code here. Make your the user is only able to change its own object. If you are creating an management part where an amin is allowed du edit all users you code is ok but you should check if the user is allowed to do this operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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