The SiteAdministrators collection of the SPWeb object returns a SPUserCollection of all the Site Collection Administrators for the site in question.
Unfortunately, it doesn’t appear to actually allow you to “ADD” new users to the Site Collection Administrators via this collection like you can do with any other SPUserCollection for a user.
Whether it’s a bug, or not, a simple way around this is to do the following:
- Add the user to the site collection users list
- Retrieve the user you just added
- Set the IsSiteAdmin property for the user to true.
- Call SPUser.Update() for the user.
Sample code:
someSPWeb.AllUsers.Add(“domain\\user”,”user@domain.com”,”The User”,null);
SPUser spUser = someSPWeb.AllUsers[“domain\\user”];
spUser.IsSiteAdmin = true;
spUser.Update();
Hope this helps
– Keith
Hi.
Thanks for this short but very usefull peace of code.
Any idea why the spuser.update would throw an exception but the update is still completed?
Thanx, it works and give no error at all.
Can this be achieved without code? i.e. via stsadm command line?
The purpose of this was for software I was writing at the time.
STSADM -o siteowner only supports adding a primary and secondary owner (Site collection admin)
Thank you very much.
i was hitting my head with SpWeb.SiteAdministrators.Add()
My sharepoint experience is limited but I can follow clear instructions.
Can someone explain step by step how to do this without assuming any knowledge of sharepoint objects etc?
Thanks.
this is a Great Post. I found it quickly and did not have to waste too much time trying to debug siteadministrators.add. Thanks!!
Its Great helped me also,Thanks for the post
it saved my day..,Thanks for the post