Delen via


Add domain user to all sites and sub-sites as secondary administrator

If you have to add a site collection administrator, then first option is through user interface. Go to Central Administration site and Click on the “Application Management” tab. Click an option “Site collection administrator” under the “SharePoint Site Management” tab.

SharePointSiteManagement

 

You will get redirect to “Site Collection Administrators Page :-

SiteCollectionAdministrator

 

go ahead and add your domain user as a secondary administrator.

 

Second option is through Stsadm utility. Here is the command you have to run :-

AddSecondaryOwner

and go and refresh the Site Collection administrator Page.

Result

 

But if you have to add the secondary administrator for all the site and sub-sites .  Here is the code snippet :-

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Text;
    4: using Microsoft.SharePoint;
    5: using Microsoft.SharePoint.Administration;
    6: using System.Diagnostics;
    7:  
    8: namespace AddAdministrator
    9: {
   10:     class Program
   11:     {
   12:         static void Main(string[] args)
   13:         {
   14:             try
   15:             {
   16:                 SPWebApplicationCollection webApplications = null;
   17:                 webApplications = SPWebService.ContentService.WebApplications;
   18:  
   19:                 foreach (SPWebApplication application in webApplications)
   20:                 {
   21:                     Console.WriteLine("Application Pool Identity ");
   22:                     SPApplicationPool appPool = application.ApplicationPool;
   23:                     Console.WriteLine("Application Pool Name :- " + appPool.Name + " Application Pool  UserName " + appPool.Username + "Application pool Password " + appPool.Password);
   24:                     if (!application.IsAdministrationWebApplication)
   25:                     {
   26:                         Console.WriteLine("Application Name " + application.Name);
   27:                         SPSiteCollection sites = application.Sites;
   28:                         for (int i = 0; i < sites.Count; i++)
   29:                         {
   30:                             
   31:                             using (SPSite site = sites[i])
   32:                             {
   33:                                 Console.WriteLine("Site URL " + site.Url);
   34:                                 
   35:                                 // Uncomment this code if you would like to add a seconday administrator.
   36:                                 
   37:                                 /*Program obj = new Program();
   38:                                 obj.stsadmCommand("-o siteowner -url " + site.Url.ToString() + " -secondarylogin " + "NAVD1533981\\vishalm");
   39:                                 Console.WriteLine("Secondar Administrator Added");*/
   40:  
   41:                                 foreach (SPWeb web in site.AllWebs)
   42:                                 {
   43:                                     Console.WriteLine("Web URL " + web.Url);
   44:                                     
   45:                                     SPGroupCollection groupCollection = web.SiteGroups;
   46:                                     SPUser adminUser = web.SiteAdministrators.GetByEmail("vishalm@microsoft.com");
   47:                                     if (adminUser == null)
   48:                                     {
   49:                                         web.SiteAdministrators.Add("NAVD1533981\\vishalm", "vishalm@microsoft.com", "Vishal Madan", "A administrator has been added.");
   50:                                     }
   51:  
   52:                                     SPUser user = web.SiteAdministrators.GetByEmail("vishalm@microsoft.com");
   53:                                     if (web.IsRootWeb)
   54:                                     {
   55:                                         SPUserCollection userCollection = web.SiteAdministrators;
   56:                                         bool userIsAdmin = false;
   57:                                         foreach (SPUser spUser in userCollection)
   58:                                         {
   59:                                             if (spUser.ID == user.ID)
   60:                                             {
   61:                                                 userIsAdmin = true;
   62:                                                 break;
   63:                                             }
   64:                                         }
   65:                                         if (userIsAdmin == false)
   66:                                         {
   67:                                             user.IsSiteAdmin = true;
   68:                                             user.Update();
   69:                                         }
   70:                                     }
   71:  
   72:                                     web.Update();
   73:                                     web.Dispose();
   74:                                 }
   75:                              
   76:                             }
   77:                         }
   78:                     }
   79:                 }
   80:           
   81:             }
   82:             catch (NullReferenceException)
   83:             {
   84:                 Console.WriteLine("Cannot access web applications.");
   85:                 
   86:             }
   87:             catch (Exception exception)
   88:             {
   89:                 Console.WriteLine("Error occurred: " + exception.Message);
   90:                 Console.WriteLine("See the application event log for more information.");
   91:             }
   92:  
   93:             Console.ReadKey();
   94:         }
   95:         StringBuilder sortOutput = null;
   96:         private string stsadmCommand(string Arguments)
   97:         {
   98:             string stsadmPath = @"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\STSADM.EXE";
   99:  
  100:             System.Diagnostics.Process sortProcess = new System.Diagnostics.Process();
  101:             sortProcess.StartInfo.FileName = stsadmPath;
  102:             sortProcess.StartInfo.CreateNoWindow = true;
  103:             sortProcess.StartInfo.Arguments = Arguments;
  104:             sortProcess.StartInfo.UseShellExecute = false;
  105:             sortProcess.StartInfo.RedirectStandardOutput = true;
  106:             sortOutput = new StringBuilder("");
  107:             sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
  108:             sortProcess.StartInfo.RedirectStandardInput = true;
  109:             sortProcess.Start();
  110:             sortProcess.BeginOutputReadLine();
  111:             sortProcess.WaitForExit();
  112:             sortProcess.Close();
  113:             return sortOutput.ToString();
  114:         }
  115:  
  116:         private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
  117:         {
  118:             // Add the text to the collected output.
  119:             sortOutput.Append(outLine.Data);
  120:         }
  121:     
  122:     }
  123: }