Ir al contenido principal

Import user profile pictures to SharePoint using PowerShell

 

Best solution to create Automatic UserProfile Import to SharePoint

  1. Create folder “ImportProfileImages”
  2. Create subfolder “ProfileImages”
  3. Create “Drive:\ImportProfileImages\UpdateUserProfiles.ps1″

 

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
 
#---------------------------------------------------------------------------------
# Default Values
#---------------------------------------------------------------------------------
 
$spNotFoundMsg = "Unable to connect to SharePoint.  Please verify that the site '$siteUrl' is hosted on the local machine.";
 
#-----------------------------------------------------
# Load Assemblies
#-----------------------------------------------------
 
if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") -eq $null)        { throw $spNotFoundMsg; }
if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") -eq $null)    { throw $spNotFoundMsg; }
 
#-----------------------------------------------------
# Functions
#-----------------------------------------------------
 
function ToSimpleString([string]$value, [bool]$trim = $true, [bool]$removeSpaces = $true, [bool]$toLower = $true)
{
    if ($value -eq $null) { return [System.String]::Empty; }
 
    if ($trim)
    {
        $value = $value.Trim();
    }
 
    if ($removeSpaces)
    {
        $value = $value.Replace(" ", "");
    }
 
    if ($toLower)
    {
        $value = $value.ToLower();
    }
 
    return $value;
}
 
function GetSPSite($url)
{
    [Microsoft.SharePoint.SPSite]$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList $url
    return $site;
}
 
function GetSPWeb($url)
{
    [Microsoft.SharePoint.SPSite]$site = GetSPSite -url $url;
    [Microsoft.SharePoint.SPWeb]$web = $site.OpenWeb();
    return $web
}
 
function GetSPList($url, $listName)
{
    $listName = (ToSimpleString -value $listName);
 
    [Microsoft.SharePoint.SPWeb]$web = GetSPWeb -url $url;
    foreach($list in $web.Lists)
    {
        $title = (ToSimpleString -value $list.Title);
        if ($listName -eq $title)
        {
            return $list;
        }
    }
    return $null;
}
 
function GetSPDocumentLibrary($url, $libraryName)
{
    [Microsoft.SharePoint.SPDocumentLibrary]$lib = [Microsoft.SharePoint.SPDocumentLibrary](GetSPList -url $url -listName $libraryName);
    return $lib;
}
 
function GetSPFile($libraryInstance, $fileName)
{
    $fileName = (ToSimpleString -value $fileName -removeSpaces $false);
 
    foreach($file in $libraryInstance.RootFolder.Files)
    {
        $itemName = (ToSimpleString -value $file.Name -removeSpaces $false);
        if ($fileName -eq $itemName)
        {
            return $file;
        }
    }
    return $null;
}
 
function UploadSPFile([string]$url, [string]$libraryName, [string]$filePath, [System.Text.StringBuilder]$verbose = $null)
{
    try
    {
        [Microsoft.SharePoint.SPDocumentLibrary]$lib = (GetSPDocumentLibrary -url $url -libraryName $libraryName);
        if ($lib -eq $null)
        {
            throw (([string]'Cannot find document library "') + ([string]$libraryName) + ([string]'" at url "') + ([string]$url) + ([string]'"!'));
        }
 
        $bytes = [System.IO.File]::ReadAllBytes($filePath);
        $fileName = [System.IO.Path]::GetFileName($filePath);
 
        [Microsoft.SharePoint.SPFile]$file = GetSPFile -libraryInstance $lib -fileName $fileName;
 
        if ($file -eq $null)
        {
            if ($verbose -ne $null)
            {
                [void]$verbose.AppendLine("Uploading File...");
            }
            $file = $lib.RootFolder.Files.Add($fileName, $bytes);
        }
        else
        {
            if ($verbose -ne $null)
            {
                [void]$verbose.AppendLine("File Exists, overwriting...");
            }
            $file.SaveBinary($bytes);
        }
 
        if ($verbose -ne $null)
        {
            [void]$verbose.AppendLine(($bytes.Length.ToString()) + ([string]" bytes written!"));
        }
 
        return $file;
    }
    catch
    {
        if ($verbose -ne $null)
        {
            [void]$verbose.AppendLine(([string]'Error: Upload to document library "') + ([string]$libraryName) + ([string]'" at "') + ([string]$url) + ([string]'" or file "') + ([string]$filePath) + ([string]'" failed!'));
            [void]$verbose.AppendLine([string]'Error: ' + [string]$error[1]);
        }
    }
 
    return $null;
}
 
function GetSpContext($url)
{
    [Microsoft.SharePoint.SPSite]$site = GetSPSite -url $url
    return [Microsoft.Office.Server.ServerContext]::GetContext($site);
}
 
function GetProfileManager($url)
{
    [Microsoft.Office.Server.ServerContext]$ctx = GetSpContext -url $url
    [Microsoft.Office.Server.UserProfiles.UserProfileManager]$upm = New-Object "Microsoft.Office.Server.UserProfiles.UserProfileManager" -ArgumentList $ctx
 
    return $upm;
}
 
function GetSPUser($url, $loginName)
{
   [Microsoft.SharePoint.SPWeb]$web = GetSPWeb -url $url
   [Microsoft.SharePoint.SPUser]$user = $web.AllUsers[$loginName]
   return $user;
}
 
function GetProfilePropertyName($userProfileManager, $propertyName)
{
    $propertyName = (ToSimpleString -value $propertyName);
    $propertyName = $propertyName.Replace("sps-", "");
 
    foreach($prop in $userProfileManager.Properties)
    {
        [string]$n = (ToSimpleString -value $prop.DisplayName);
        $n = $n.Replace("sps-", "");
        if ($propertyName -eq $n) { return $prop.Name.ToString(); }
 
        $n = (ToSimpleString -value $prop.Name);
        $n = $n.Replace("sps-", "");
        if ($propertyName -eq $n) { return $prop.Name.ToString(); }
    }
 
    return $null;
}
 
#This function is VERY different from [System.IO.Path]::Combine
function CombineUrls([string]$baseUrl, [string]$relUrl)
{
    [System.Uri]$base = New-Object System.Uri($baseUrl, [System.UriKind]::Absolute);
    [System.Uri]$rel = New-Object System.Uri($relUrl, [System.UriKind]::Relative);
 
    return (New-Object System.Uri($base, $rel)).ToString();
}
 
function Update-SPProfilePictures([string]$webUrl, [string]$picLibraryName, [string]$localFolderPath, [string]$domain)
{
    #Get web and picture library folder that will store the pictures
    $web = Get-SPWeb $webUrl
    $picFolder = $web.Folders[$picLibraryName]
    if(!$picFolder)
    {
        Write-Host "Picture Library Folder not found"
        return
     }
 
    #Attach to local folder and enumerate through all files
    $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object {
 
        $username = [IO.Path]::GetFileNameWithoutExtension($_.FullName);
 
        #Create file stream object from file
        $fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
        $contents = new-object byte[] $fileStream.Length
        $fileStream.Read($contents, 0, [int]$fileStream.Length);
        $fileStream.Close();
 
        write-host "Copying" $_.Name "to" $picLibraryName "in" $web.Title "..."
 
        #Add file
        $spFile = $picFolder.Files.Add($picFolder.Url + "/" + $_.Name, $contents, $true)
        $spItem = $spFile.Item
 
        $upm = GetProfileManager -url $webUrl
        $up = $null;
        $up = $upm.GetUserProfile("$domain\$username");
 
        $picturePropertyName = GetProfilePropertyName -UserProfileManager $upm -PropertyName "PictureUrl";
 
        if($up -ne $null)
        {
            if (-not [System.String]::IsNullOrEmpty($picturePropertyName))
            {
                $PortraitUrl = CombineUrls -baseUrl $spFile.Web.Url -relUrl $spFile.ServerRelativeUrl;
                Write-Host $PortraitUrl
                $up.get_Item($picturePropertyName).Value = $PortraitUrl;
                $up.Commit();
            }
        }
 
    }
 
    Write-Host "Updating User Profile Photo Store..." -foregroundcolor yellow
    Update-SPProfilePhotoStore –MySiteHostLocation $webUrl
    Write-Host "Done" -foregroundcolor green
}

 


4. Run


Update-SPProfilePictures "http://my-sites-host-url" "User Photos""c:\ImportProfileImages\ProfileImages" "Your_Domain"

Comentarios

Entradas populares de este blog

Get SharePoint Online Site and SubSites permission using PowerShell

The below PowerShell script retrieves the following for the given SharePoint Online Site All the Sub-site's URL Security group attached with each Sub-site with their permission level Prerequisites: This PowerShell script uses the latest version of SharePoint Online PnP Module. Download the installer from https://github.com/SharePoint/PnP-PowerShell/releases  Install-Module SharePointPnPPowerShellOnline  Install-Module - Name ' SharePointPnP.PowerShell.Commands.Files.Recurse ' function  connect - site( $webs , $creds ){    Connect - PNPonline  - Url  $webs   - Credentials  $cred     }    function  get - sitepermission( $web , $cred ){    $rec =@()    connect - site  - webs  $web   - creds  $cred     if ( $web   - eq  $parentsitename )  {  #Write-Host "Parent site permission" $web   $Pgroups =G...

O365 - Forms - Transferir la propiedad de un formulario

Fuente :  https://support.office.com/es-es/article/transferir-la-propiedad-de-un-formulario-921a6361-a4e5-44ea-bce9-c4ed63aa54b4 Si ha creado una encuesta, una prueba o un sondeo, puede moverlos fácilmente a un grupo para que todos los miembros del grupo se conviertan en propietarios de ese formulario. Transferir el formulario a un grupo En el explorador Web, vaya a  Forms.Office.com . En la pestaña  mis formularios  , busque el formulario que desea transferir. Haga clic en  más acciones de formulario    y, a continuación, seleccione  mover . Nota:  Solo puede mover el formulario si es el propietario de ese formulario. No puede transferir la propiedad de un formulario que está compartido con usted. Seleccione el grupo al que desea transferir el formulario y, a continuación, haga clic en  mover . El formulario que ha movido aparecerá en la pestaña  formularios de grupo  . ¿Qué ocurre con el libr...

Event ID 8031 The uri endpoint information may be stale

An exception occurred while updating addresses for connected app {6783ce5e-c88h-4021-8d5b-12614875cbfa_b79f19ab-1d40-4824-9911-3466cf8b070a}. The uri endpoint information may be stale. System.InvalidOperationException: The requested application could not be found.    at Microsoft.SharePoint.SPTopologyWebServiceApplicationProxy.ProcessCommonExceptions(Uri endpointAddress, String operationName, Exception ex, SPServiceLoadBalancerContext context)    at Microsoft.SharePoint.SPTopologyWebServiceApplicationProxy.ExecuteOnChannel(String operationName, CodeBlock codeBlock)    at Microsoft.SharePoint.SPTopologyWebServiceApplicationProxy.GetEndPoints(Guid serviceId)    at Microsoft.SharePoint.SPConnectedServiceApplicationAddressesRefreshJob.Execute(Guid targetInstanceId) After de-commissioning some SharePoint servers, you might notice the above error on other WFEs /Application server’s event viewer . It appears that the SharePoint still has a reference...