次の方法で共有


Update email addresses of all users in all sites in a SharePoint Farm in a single shot (code sample for both V2 & V3)

Recently I got a case from one of customer and their requirement was, since their domain for the email accounts has been modified to an another name, they wanted to update email addresses of all users in all the sites in their SharePoint farm. It will be tedious job if we update the email address of each users in all sites manually.

Here we will get the help of SharePoint object model J !

I have developed a small tool for updating the user’s email domains in all the sites using SharePoint object model. Here I am giving a sample code snippet for accomplishing that requirement in both SharePoint server 2003 & in MOSS.

These codes are for .NET console based application. Once if we execute the code it logs the old and new email addresses in a text file inside bin\Debug directory. There you can find out updated email address.

Remember you need to execute this code with a service account which has the permission rights to do that. (For eg: Application pool identity)

 

Code for SharePoint Portal server 2003 / WSS V 2.0.

<code>

using System;

using System.IO;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

namespace UpdateAllEmails

{

    class Program

    {

        static void Main(string[] args)

        {

            SPGlobalAdmin spGlobalAdmin = new SPGlobalAdmin();

              

            TextWriter reportfile = new StreamWriter("EmailUpdateReport.txt");

            foreach (SPVirtualServer oWebApp in spGlobalAdmin.VirtualServers)

            {

                if (oWebApp.State == SPVirtualServerState.Ready)

                {

                    SPSiteCollection oSites = oWebApp.Sites; // Retrieving all sites for a particular web application

                    if (oSites != null && oSites.Count > 0)

                    {

                        foreach (SPSite oSite in oSites)

                        {

                            SPWebCollection oWebCollection = oSite.AllWebs; // Retrieving all websites for a particular site collection

                            foreach (SPWeb oWeb in oWebCollection)

                            {

                                SPUserCollection oUsers = oWeb.AllUsers; // Retrieving all the users in this particular website

                                foreach (SPUser oUser in oUsers)

                                {

                                    string strOldEmail = oUser.Email;

          if (strOldEmail != string.Empty)

                                    {

                                        string[] strSplittedEmail = strOldEmail.Split('@');

                                        string strNewEmail = strSplittedEmail[0].ToString() + "@newDomain.com";

                                        oUser.Email = strNewEmail;

                                        oUser.Update();// replacing the email domain address with the new one

                                  reportfile.WriteLine("WebAppliction : " + oWebApp.Url + " >> Site Collection :" + oSite.Url + " >> Web Site :" + oWeb.Url + " >> User Name :" + oUser.Name + " >> this user's old email ID was :" + strOldEmail + " >> new email ID is :" + oUser.Email);

                                    }

                                }

                            }

                        }

                    }

                }

            }

            reportfile.Close();

        }

    }

}

</code>

Code for MOSS 2007 / WSS V 3.0

 

<code>

using System;

using System.IO;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

namespace UpdateAllEmails

{

    class Program

    {

        static void Main(string[] args)

        {

            TextWriter reportfile = new StreamWriter("EmailUpdateReport.txt");

            SPWebApplicationCollection oWebApps = SPWebService.ContentService.WebApplications; // Retrieving all web applications from the Farm

            foreach (SPWebApplication oWebApp in oWebApps)

            {

                SPSiteCollection oSites = oWebApp.Sites; // Retrieving all sites for a particular web application

                foreach (SPSite oSite in oSites)

                {

                    SPWebCollection oWebCollection = oSite.AllWebs; // Retrieving all websites for a particular site collection

                    foreach (SPWeb oWeb in oWebCollection)

                    {

                        SPUserCollection oUsers = oWeb.AllUsers; // Retrieving all the users in this particular website

                        foreach (SPUser oUser in oUsers)

                        {

                            string strOldEmail = oUser.Email;

                            if (strOldEmail != string.Empty)

                            {

                                string[] strSplittedEmail = strOldEmail.Split('@');

                                string strNewEmail = strSplittedEmail[0].ToString() + "@newDomain.com";

                                oUser.Email = strNewEmail;

                                oUser.Update();// replacing the email domain address with the new one

                                reportfile.WriteLine("WebAppliction : " + oWebApp.Name + " >> Site Collection :" + oSite.Url + " >> Web Site :" + oWeb.Url + " >> User Name :"+ oUser.Name + " >> this user's old email ID :" + strOldEmail + " >> new email ID :" + oUser.Email );

                                                              

                            }

                        }

                        oWeb.Update();

          }

                }

            }

            reportfile.Close();

        }

    }

}

</code>

Happey coding J