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);  
           }  
         }  
       }  
     }  
 }  

fredag den 5. december 2014

SharePoint Sites and Pages Powershell Warmup Script

Very nice little script i created to warmup all PWA pages and workspaces.
Simply replace the SiteURL and the SitePages with your own values and place the code in a file named "SharePointWarmup.ps1".
Run the script in Powershell or Task Scheduler.

 $SiteURL = "http://intra/PWA/"  
 $SitePages =   
 "default.aspx",  
 "projects.aspx",  
 "ProjectBICenter/_layouts/15/xlviewer.aspx?id=/PWA/ProjectBICenter/Sample%20Reports/Projectum/DailyCommentsReport%20v7%20-%20PowerView.xlsx&Source=http%3A%2F%2Fintra%2FPWA%2FProjectBICenter%2FSample%2520Reports%2FForms%2FAllItems%2Easpx%3FRootFolder%3D%252FPWA%252FProjectBICenter%252FSample%2520Reports%252FProjectum%26FolderCTID%3D0x012000271EBEF22B819D4AA417BE3C68ADBA34%26View%3D%257B857402E5%252DF616%252D4DC2%252DA880%252D5CFC03D52EFA%257D",   
 "Project%20Detail%20Pages/Project%20Information.aspx",  
 "project%20detail%20pages/schedule.aspx",  
 "project%20detail%20pages/sdc%20-%20budget.aspx"  
 Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue   
 $timeout = 240000 #=4 min   
 Add-Type -ReferencedAssemblies "System.Net" -TypeDefinition @"   
 using System.Net;   
 public class WarmupWebClient:WebClient  
 {  
   private int timeout = 60000;   
   public WarmupWebClient(int timeout) {this.timeout = timeout;}  
   protected override WebRequest GetWebRequest(System.Uri webUrl)   
   {  
     WebRequest requestResponse = base.GetWebRequest(webUrl);   
     requestResponse.Timeout = this.timeout;   
     return requestResponse;   
   }   
 }   
"@  
 function HitPage($url)  
 {  
   try   
   {   
     Write-Host "Hitting page: $url"   
     $wc = New-Object WarmupWebClient($timeout)   
     $wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials   
     $ret = $wc.DownloadString($url)   
   }  
   catch [Exception]   
   {   
     Write-Host "Failed to hit page. Message was: $($_.Exception.Message)"   
   }   
 }  
 foreach ($site in $SitePages)   
 {   
   $warmupSite = $SiteURL + $site  
   HitPage($warmupSite)  
 }  
 Write-Host "All sites have been hit"   

Of cause you want this script to be run on a timely schedule so remember to check out my other post on how to do exactly that Run Powershell from Task Scheduler .

torsdag den 4. december 2014

Run powershell from CMD or Task Scheduler


If you want to run a Powershell script against SharePoint on a timely schedule the Windows Task Scheduler is fast an simple approach.

A simple example of this could be a SharePoint warm-up script which hits a number of sites on a regular schedule. However it is maybe not as straight forward to figure out the syntax for the Powershell command as you might think.

Run in CMD



The first step is to verify you can run the script from a command prompt. (you might want to start the command prompt as the account that is going to run the scheduled task).

1. Click start and type CMD, press enter.

2. Paste the following command and replace the script-path with the location of you own script.
    Powershell -file "C:\PathToFile\SharePointWarmupScript.ps1"

3. Press enter and see the script being executed successfully.

Create Task Schedule


The next step is to create the scheduled task.

1. Click start and type Task Scheduler, press enter.

2. Left click Task Scheduler and click Create Task.



 3. Give the task a name, ex. SharePoint Warmup Task.



4. Select your desired options on the Triggers tab.

5. On the Actions tab click New and type the following.
    Program/Script: Powershell
    Add arguments (optional): -file "C:\PathToFile\SharePointWarmupScript.ps1"



6. Click OK to create the task.

The scheduled Powershell task is now created and you can test it out in the Task Scheduled Library if you like.