Sunday, September 1, 2013

Synchronize User profile pictures in SharePoint


שלום חברים,
היום ביצעתי סנכרון תמונות משתמש לרכיב הפרופילים בחוות SharePoint של הארגון. הסנכרון התבצע בכמה שלבים :
1.       בחירת מקור התוכן ממנו SharePoint ייבא את התמונה.
a.       אני בחרתי לייבא את התמונה מה-Active Directory .
b.       ב-AD היה נתיב לתמונה ששמורה בכונן רשת.
c.       העתקת התמונות לשרת SP
                                                               i.      העתקתי את התמונות תחת הנתיב הבא :
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\EmployeePictures

                                                             ii.      הבחירה נעשתה בגלל הסיבה שתיקיית ה-Layouts ממופה ב-IIS וניתן לגשת אל התוכן שבה דרך הדפדפן, כאשר נרשום בדפדפן :
נוכל לראות את התמונה בדפדפן. (במידה וחשקה נפשך בתיקייה אחרת/נתיב אחר יש לוודא שיש מיפוי לנתיב Relative בIIS תחת ה-Site  הרלוונטי)

2.       יצירת Profile Property חדש שנקרא PictureStr שמקבל את הערך ה-Stringי של נתיב התמונה.





a.       בשלב היצירה יש לבחור את הערך מה-AD שייובא לתוך SP.


3.       ביצוע סנכרון מול AD.
4.       השדה Picture בפרופיל ב-SP הינו מ-Type URL , לכן מה שנדרשתי לעשות הוא להמיר את הנתיב שסונכרן  ל-URL, עשיתי זאת ע"י כתיבת Script ב-PowerShell שמשרשר את כתובת ה-SiteCollection עם שם הקובץ (תמונת המשתמש ע"י מזה חד-חד-ערכי של מס' עובד).


5.       עדכון השדה Picture בפרופיל עם ערך URL שמציג את תמונת המשתמש.

להלן ה-Script :
[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server") | out-null
$site=new-object Microsoft.SharePoint.SPSite("http://ServerName:Port") #Central Admin site
$servercontext=[Microsoft.Office.Server.ServerContext]::GetContext($site)
$site.Dispose() # clean up
$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($servercontext)

$pc = $upm.GetEnumerator()
$counter = 0
$counter2 = 0

foreach($p in $pc)
{
 
  if(($p["PictureStr"].Value) -eq $null)
  {
    Write-Host "No Picture for $($p["AccountName"].Value)" -foregroundcolor Red
    $counter++
  }
           
  else
  {
    # $p["PictureUrl"].Value
    # $p["PictureStr"].Value

    $StringPic = $p["PictureStr"].ToString()
    Write-Host $StringPic -foregroundcolor Cyan
    $pArr = $StringPic.trim("\\Picture's Path")
    Write-Host $pArr -foregroundcolor Cyan


    if($StringPic -eq "\\Picture's Path")
    {
      Write-Host "$($StringPic)" -foregroundcolor Yellow
      Write-Host "No Picture for $($p["AccountName"].Value)" -foregroundcolor Yellow
      $picURL = $null
    }
    else
    {    
      $picURL = "http://ServerName:port/SiteCollection/_layouts/PaisEmployeePictures/$pArr"  # Change the ServerName:Port and the SiteCollection, Example :" http://SharePointPortal/_layouts/PaisEmployeePictures/$pArr"
    }


    if(($p["PictureUrl"].Value) -eq $null)
     {
      $p["PictureUrl"].Value = $picURL
      $p.Commit()
      Write-Host $p["PictureUrl"].Value -foregroundcolor Magenta
      Write-Host "The PictureURL Attribute for $($p["AccountName"].Value) Updated " -foregroundcolor Green
     }
    
    $counter2++
  }
 
}

Write-Host "The Number of Users that doesn't have Picture is : " $counter -foregroundcolor Yellow
Write-Host "The Number of Users that has Picture is : " $counter2 -foregroundcolor Cyan




והרי התוצאה לפניכם :


בהצלחה !
רון נס.




  




============================================================================================================================================================================================================================================================













Hello Friends,
Today I've sync  photos to Profiles Component on SharePoint farm.
 Synchronization occurs in several stages:
1.     Choosing a the content source to Import  images for SharePoint.
a. I chose to import the picture from Active Directory.
b. In AD the Images was stored some Attribute that his value was the path of the image on a network drive.
c. Copy images to the SharePoint server
i. I copied the pictures under the following path:
C: \ Program Files \ Common Files \ Microsoft Shared \ Web Server Extensions \ 14 \ TEMPLATE \ LAYOUTS \ EmployeePictures

ii. The choice was made because of the reason the Layouts folder mapped in IIS and can be accessed through the browser which content when we write in the browser:
http://ServerName:Port/SiteCollection/_layouts/EmployeePictures/ID.JPG
We can see the image in the browser.
(If you like fancy another folder / path then make sure that Relative path mapping in IIS under the relevant Site)

2.     Create a new Profile Property called 'PictureStr' that equals the String the path of the image. (String Property )






a. At work there is to choose the value what-AD will be imported into the SP.



3.     Synchronizing with AD.
4.     The 'Picture'  field in Profile Component is  an URL Type, so what I was required to do is convert the path has been synchronized to 'PictureStr'  to Legal URL, I did this by writing a PowerShell  script in concatenate the address SiteCollection with the file name (the image by than one - one - the values ​​of a number of works).


5.     Update Picture field with a URL that displays the value of the image.

The following script:

[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server") | out-null
$site=new-object Microsoft.SharePoint.SPSite("http://ServerName:Port") #Central Admin site
$servercontext=[Microsoft.Office.Server.ServerContext]::GetContext($site)
$site.Dispose() # clean up
$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($servercontext)

$pc = $upm.GetEnumerator()
$counter = 0
$counter2 = 0

foreach($p in $pc)
{
 
  if(($p["PictureStr"].Value) -eq $null)
  {
    Write-Host "No Picture for $($p["AccountName"].Value)" -foregroundcolor Red
    $counter++
  }
           
  else
  {
    # $p["PictureUrl"].Value
    # $p["PictureStr"].Value

    $StringPic = $p["PictureStr"].ToString()
    Write-Host $StringPic -foregroundcolor Cyan
    $pArr = $StringPic.trim("\\Picture's Path")
    Write-Host $pArr -foregroundcolor Cyan


    if($StringPic -eq "\\Picture's Path")
    {
      Write-Host "$($StringPic)" -foregroundcolor Yellow
      Write-Host "No Picture for $($p["AccountName"].Value)" -foregroundcolor Yellow
      $picURL = $null
    }
    else
    {    
      $picURL = "http://ServerName:port/SiteCollection/_layouts/PaisEmployeePictures/$pArr"  # Change the ServerName:Port and the SiteCollection, Example :" http://SharePointPortal/_layouts/PaisEmployeePictures/$pArr"
    }


    if(($p["PictureUrl"].Value) -eq $null)
     {
      $p["PictureUrl"].Value = $picURL
      $p.Commit()
      Write-Host $p["PictureUrl"].Value -foregroundcolor Magenta
      Write-Host "The PictureURL Attribute for $($p["AccountName"].Value) Updated " -foregroundcolor Green
     }
    
    $counter2++
  }
 
}

Write-Host "The Number of Users that doesn't have Picture is : " $counter -foregroundcolor Yellow
Write-Host "The Number of Users that has Picture is : " $counter2 -foregroundcolor Cyan



And the result before you:





Good Luck :)
Ron Ness.

No comments:

Post a Comment