fredag den 19. december 2014

Project Server to Sharepoint Email sync program

Today I got a support case where alerts in SharePoint was not triggered, I quickly realized the problem was that the email address on the users was wrong in SharePoint and the admins only updated the emails in Project Server. Why the emails were not correct i do not know and did not look further into it.
To solve the difference between SharePoint and Project Server I created this small C# script/program which synchronize user emails from Project Server into SharePoint.
You will typically need a tool like this when you are creating your users manually in Project Server and the emails are synchronized from a inconsistent Active Directory.

The program can be found here:
Project Server -> SharePoint Emails sync
The code for the program/script is as follows:
 static void Main(string[] args)  
 {  
   string pwa = "";  
   string webApp = "";  
   if (args.Length <= 0)  
   {  
     WriteLine("WRONG ARGS, PWA");  
     return;  
   }  
   pwa = args[0];  
   PS.Resource r = new PS.Resource();  
   r.Url = pwa + "/_vti_bin/psi/resource.asmx";  
   r.UseDefaultCredentials = true;  
   PS.ResourceDataSet resDS = r.ReadResources("", false);  
   Dictionary<string, string> resourcesEmail = new Dictionary<string, string>();  
   foreach (PS.ResourceDataSet.ResourcesRow resrow in resDS.Resources)  
   {  
     try  
     {  
       if (!resrow.IsWRES_EMAILNull())  
       {  
         resourcesEmail.Add(resrow.WRES_ACCOUNT, resrow.WRES_EMAIL);  
         WriteLine("Adding account: " + resrow.WRES_ACCOUNT + " - Email: " + resrow.WRES_EMAIL + "(Name: " + resrow.RES_NAME + ")");  
       }  
       else  
       {  
         WriteLine("Account email is null: (Name: " + resrow.RES_NAME + ")");  
       }  
     }  
     catch (Exception ex)  
     { }  
   }  
   using (SPSite siteCol = new SPSite(pwa))  
     {  
       using (SPWeb web = siteCol.OpenWeb())  
       {  
         foreach (SPUser user in web.SiteUsers)  
         {  
           try  
           {  
             WriteLine("Processing User: " + user.LoginName + " - " + user.Email);  
             if (resourcesEmail.ContainsKey(user.LoginName))  
             {  
               string email = resourcesEmail[user.LoginName];  
               if (email != user.Email)  
               {  
                 string oldemail = user.Email;  
                 user.Email = email;  
                 user.Update();  
                 WriteLine("Updating email for: " + user.Name + " (old: " + oldemail + ", new: " + email + ")");  
               }  
             } else  
             {  
               WriteLine("Account not found: " + user.LoginName);  
             }  
           }   
           catch (Exception ex)  
           {  
             WriteLine("Could not update: " + user.LoginName + ". Error: " + ex.Message + " at " + ex.StackTrace);  
           }  
         }  
       }  
     }  
 }  

Ingen kommentarer:

Send en kommentar