Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Wichtig
SharePoint-Add-Ins haben am 2. April 2026 das Ende der Lebensdauer in SharePoint Online erreicht. Sowohl von SharePoint als auch vom Anbieter gehostete Add-Ins werden eingestellt und funktionieren nicht mehr in SharePoint Online, einschließlich Government-Clouds. Die Hinweise zu Add-Ins-Entwicklung, -Bereitstellung und -Verwendung werden als historische Referenz und für anwendbare SharePoint Server-Szenarien beibehalten, nicht für neue SharePoint Online-Lösungen.
SharePoint Server-Add-Ins, die über einen lokalen App-Katalog bereitgestellt werden, werden durch diese Änderung nicht zurückgezogen. Der Erwerb von SharePoint-Add-Ins aus dem öffentlichen Marketplace endete am 2. April 2026 für SharePoint Server.
Mit dieser Einstellung werden SharePoint-REST-APIs, CSOM, JSOM oder SharePoint-Framework (SPFx) nicht außer Betrieb genommen. Verwenden Sie für SharePoint Online SPFx für Benutzeroberflächenerweiterungen und Microsoft Entra ID für die Authentifizierung bei externen Anwendungen. Weitere Informationen finden Sie in der Ankündigung der Einstellung und in der Anleitung zur Add-In-Migration.
Das Core.ProfilePictureUploader-Beispiel zeigt Ihnen, wie Sie einen Massenupload von Benutzerprofildaten aus einer Dateifreigabe oder einer SharePoint Online-URL durchführen und Benutzerprofileigenschaften mit hochgeladenen Bildern verknüpfen.
In diesem Beispiel erfahren Sie, wie man:
- Migrieren der Profilbilder eines Benutzers von SharePoint Server (lokal) zu SharePoint Online.
- Beheben von Problemen, die auftreten, wenn das Azure Active Directory-Synchronisierungstool (dirsync) die Profilbilder eines Benutzers nicht mit SharePoint Online synchronisieren kann.
- Ersetzen von Benutzerprofilbildern schlechter Qualität in SharePoint Online.
In diesem Beispiel wird eine Konsolenanwendung verwendet, um Folgendes zu tun:
- Lesen Sie die Benutzernamen und Bilddateipfade oder URLs aus einer Benutzerzuordnungsdatei.
- Rufen Sie ein oder drei Bilder ab, und laden Sie sie in eine Bildbibliothek auf dem Host "Meine Website" hoch.
- Legen Sie Benutzerprofileigenschaften fest, um die hochgeladenen Bilder mit dem Profil eines Benutzers zu verknüpfen.
- Aktualisieren zusätzlicher (optionaler) Benutzerprofileigenschaften.
Bevor Sie beginnen
Laden Sie zunächst das Core.ProfilePictureUploader-Beispiel-Add-In aus dem Office 365 Developer Patterns and Practices-Projekt auf GitHub herunter.
Hinweis
Der Code in diesem Artikel wird wie besehen und ohne jegliche Garantie zur Verfügung gestellt, gleich ob ausdrücklich oder konkludent, einschließlich jedweder stillschweigenden Gewährleistung der Eignung für einen bestimmten Zweck, Marktgängigkeit oder Nichtverletzung von Rechten.
Vor dem Ausführen dieses Codebeispiels
- Speichern Sie Ihre Benutzerbilder, die Sie in SharePoint Online hochladen möchten, auf einer Dateifreigabe oder einem Webserver.
- Bearbeiten Sie die userlist.csv Datei so, dass sie Folgendes enthält:
- Eine Kopfzeile mit dem Wert "UserPrincipalName, SourceURL".
- Fügen Sie für jeden Benutzer eine neue Zeile hinzu, die das Organisationskonto (oder den Benutzerprinzipalnamen) des Benutzers und den Dateipfad oder die URL des hochzuladenden Bilds enthält.
Ausführen dieses Beispiels in Visual Studio
Konfigurieren Sie das Core.ProfilePictureUploader-Projekt mit den folgenden Befehlszeilenargumenten: -SPOAdmin Username -SPOAdminPassword Password -Configuration filepath Dabei gilt:
- Benutzername ist der Benutzername Ihres Office 365-Administrators.
- Das Kennwort ist das Kennwort Ihres Office 365-Administrators.
- Filepath ist der Dateipfad der configuration.xml Datei.
Festlegen der Befehlszeilenargumente für das Core.ProfilePictureUploader-Projekt
Öffnen Sie im Projektmappen-Explorer das Kontextmenü (Rechtsklick) für die Eigenschaften des Projekts >Core.ProfilePictureUploader.
Wählen Sie Debuggen aus.
Geben Sie unter Befehlszeilenargumente die zuvor aufgeführten Befehlszeilenargumente ein.
Konfigurieren des Uploadvorgangs gemäß Ihren Anforderungen
Bearbeiten Sie die configuration.xml Datei, indem Sie die folgenden Werte eingeben:
Der Name Ihres Office 365-Mandanten im tenantName-Element. Beispiel:
<tenantName>contoso.onmicrosoft.com</tenantName>Der Pfad der Benutzerzuordnungsdatei im pictureSourceCsv-Element . Beispiel:
<pictureSourceCsv>C:\temp\userlist.csv</pictureSourceCsv>Anweisungen zum Hochladen von Bildern mithilfe des thumbs-Elements . Bearbeiten Sie die folgenden Attribute im thumbs-Element :
aupload3Thumbs : Legen Sie den Wert auf "true " fest, wenn Sie für jeden Benutzer drei Bilder hochladen möchten, oder auf "false ", wenn Sie nur ein Bild hochladen möchten.
createSMLThumbs: Legen Sie diesen Wert auf true fest, wenn Sie drei Bilder unterschiedlicher Größe (klein, mittel und groß) des Quellimages erstellen möchten, oder auf "false ", wenn Sie drei Bilder derselben Größe hochladen möchten.
Zusätzliche Eigenschaften, die für das Profil des Benutzers mithilfe des additionalProfileProperties-Elements festgelegt werden können. Der folgende XML-Code gibt beispielsweise eine zusätzliche Benutzerprofileigenschaft namens SPS-PictureExchangeSyncState an, die beim Ausführen des Codebeispiels im Profil des Benutzers auf 0 gesetzt werden sollte.
<additionalProfileProperties> <property name="SPS-PictureExchangeSyncState" value="0"/> </additionalProfileProperties>Der Protokolldateipfad im Logfile-Element , wie im folgenden Beispiel gezeigt.
<logFile path="C:\temp\log.txt" enableLogging="true" loggingLevel="verbose" />Die Uploadverzögerung in Millisekunden zwischen dem Hochladen verschiedener Bilddateien mithilfe des uploadDelay-Elements . Die empfohlene Einstellung für uploadDelay beträgt 500 Millisekunden.
Ändern Sie in der Datei App.config das Wertelement der Einstellung ProfilePictureUploader_UPSvc_UserProfileService , um einen Verweis auf den Benutzerprofildienst in Ihrem SharePoint Online Admin Center aufzunehmen, wie im folgenden Beispiel gezeigt.
<Contoso.Core.ProfilePictureUploader.Properties.Settings>
<setting name="ProfilePictureUploader_UPSvc_UserProfileService"
serializeAs="String">
<value>https://contoso-admin.sharepoint.com/_vti_bin/userprofileservice.asmx</value>
</setting>
</Contoso.Core.ProfilePictureUploader.Properties.Settings>
Wichtig
Durch Herstellen einer Verbindung mit dem Webdienst "userprofileservice.asmx" im SharePoint Online Admin Center können Sie die Benutzerprofileigenschaften anderer Benutzer aktualisieren. Verwenden Sie beim Ausführen dieses Codebeispiels ein Office 365-Administratorkonto, das über Berechtigungen zum Verwalten von Benutzerprofilen verfügt.
Verwenden des Core.ProfilePictureUploader-Beispiel-Add-Ins
Dieses Codebeispiel wird als Konsolenanwendung ausgeführt. Wenn das Codebeispiel ausgeführt wird, führt die Main-Methode in Program.cs Folgendes aus:
Initialisiert die Konsolenanwendung mit SetupArguments und InitializeConfiguration.
Ruft "InitializeWebService " auf, um eine Verbindung mit dem Benutzerprofildienst in SharePoint Online herzustellen.
Durchlaufen die userlist.csv Datei, um den Benutzerprinzipalnamen (UPN) für den Benutzer und den Speicherort der Bilddatei des Benutzers zu lesen.
Ruft das Bild eines Benutzers mithilfe der WebRequest - und WebResponse-Objekte in GetImagefromHTTPUrl ab.
Ruft UploadImageToSpo auf, um das Bild des Benutzers auf SharePoint Online hochzuladen.
Ruft SetMultipleProfileProperties auf, um die Benutzerprofileigenschaften PictureURL und SPS-PicturePlaceholderState für den Benutzer festzulegen.
Ruft SetAdditionalProfileProperties auf, um nach dem Hochladen der Bilddatei zusätzliche Eigenschaften für das Benutzerprofil festzulegen.
static void Main(string[] args)
{
int count = 0;
if (SetupArguments(args)) // Checks if args passed are valid
{
if (InitializeConfiguration()) // Check that the configuration file is valid
{
if (InitializeWebService()) // Initialize the web service end point for the SharePoint Online user profile service.
{
using (StreamReader readFile = new StreamReader(_appConfig.PictureSourceCsv))
{
string line;
string[] row;
string sPoUserProfileName;
string sourcePictureUrl;
while ((line = readFile.ReadLine()) != null)
{
if (count > 0)
{
row = line.Split(',');
sPoUserProfileName = row[0];
sourcePictureUrl = row[1];
LogMessage("Begin processing for user " + sPoUserProfileName, LogLevel.Warning);
// Get source picture from source image path.
using (MemoryStream picturefromExchange = GetImagefromHTTPUrl(sourcePictureUrl))
{
if (picturefromExchange != null) // If we got image, upload to SharePoint Online
{
// Create SharePoint naming convention for image file.
string newImageNamePrefix = sPoUserProfileName.Replace("@", "_").Replace(".", "_");
// Upload source image to SharePoint Online.
string spoImageUrl = UploadImageToSpo(newImageNamePrefix, picturefromExchange);
if (spoImageUrl.Length > 0)// If upload worked:
{
string[] profilePropertyNamesToSet = new string[] { "PictureURL", "SPS-PicturePlaceholderState" };
string[] profilePropertyValuesToSet = new string[] { spoImageUrl, "0" };
// Set these two required user profile properties - path to uploaded image, and pictureplaceholder state.
SetMultipleProfileProperties(_sPOProfilePrefix + sPoUserProfileName, profilePropertyNamesToSet, profilePropertyValuesToSet);
// Set additional user profile properties based on your requirements.
SetAdditionalProfileProperties(_sPOProfilePrefix + sPoUserProfileName);
}
}
}
LogMessage("End processing for user " + sPoUserProfileName, LogLevel.Warning);
int sleepTime = _appConfig.UploadDelay;
System.Threading.Thread.Sleep(sleepTime); // A pause between uploads is recommended.
}
count++;
}
}
}
}
}
LogMessage("Processing finished for " + count + " user profiles", LogLevel.Information);
}
InitializeWebService stellt eine Verbindung zu SharePoint Online her und legt einen Verweis des Benutzerprofildiensts auf eine instance-Variable fest. Andere Methoden in diesem Codebeispiel verwenden diese instance-Variable, um Aktualisierungen an Benutzerprofileigenschaften anzuwenden. Zum Verwalten des Benutzerprofils verwendet dieses Codebeispiel den Webdienst "userprofileservice.asmx" im SharePoint Online Admin Center.
static bool InitializeWebService()
{
try
{
string webServiceExt = "_vti_bin/userprofileservice.asmx";
string adminWebServiceUrl = string.Empty;
// Append the web service (ASMX) URL onto the admin website URL.
if (_profileSiteUrl.EndsWith("/"))
adminWebServiceUrl = _profileSiteUrl + webServiceExt;
else
adminWebServiceUrl = _profileSiteUrl + "/" + webServiceExt;
LogMessage("Initializing SPO web service " + adminWebServiceUrl, LogLevel.Information);
// Get secure password from clear text password.
SecureString securePassword = GetSecurePassword(_sPoAuthPasword);
// Set credentials from SharePoint Client API, used later to extract authentication cookie, so can replay to web services.
SharePointOnlineCredentials onlineCred = new SharePointOnlineCredentials(_sPoAuthUserName, securePassword);
// Get the authentication cookie by passing the URL of the admin website.
string authCookie = onlineCred.GetAuthenticationCookie(new Uri(_profileSiteUrl));
// Create a CookieContainer to authenticate against the web service.
CookieContainer authContainer = new CookieContainer();
// Put the authenticationCookie string in the container.
authContainer.SetCookies(new Uri(_profileSiteUrl), authCookie);
// Set up the user profile web service.
_userProfileService = new UPSvc.UserProfileService();
// Assign the correct URL to the admin profile web service.
_userProfileService.Url = adminWebServiceUrl;
// Assign previously created authentication container to admin profile web service.
_userProfileService.CookieContainer = authContainer;
// LogMessage("Finished creating service object for SharePoint Online Web Service " + adminWebServiceUrl, LogLevel.Information);
return true;
}
catch (Exception ex)
{
LogMessage("Error initiating connection to profile web service in SPO " + ex.Message, LogLevel.Error);
return false;
}
}
Die Main-Methode in Program.cs ruft UploadImageToSpo auf, um das Profilbild des Benutzers in SharePoint Online hochzuladen. Die Profilbilder aller Benutzer werden in einer Bildbibliothek auf dem Host "Meine Website" gespeichert. UploadImageToSpo führt die folgenden Aufgaben aus:
- Stellt eine Verbindung zu SharePoint Online her.
- Bestimmt die Anzahl der Bilder, die für jeden Benutzer hochgeladen werden sollen.
- Wenn Sie die Anwendung so konfiguriert haben, dass ein Bild pro Benutzer hochgeladen wird, wird das Bild in die Bildbibliothek hochgeladen.
- Wenn Sie die Anwendung so konfiguriert haben, dass pro Benutzer drei Bilder hochgeladen werden, überprüft die Anwendung den Wert von createSMLThumbs in der Konfigurationsdatei, um festzustellen, ob drei Bilder unterschiedlicher Größe erforderlich sind. Wenn drei Bilder unterschiedlicher Größe benötigt werden, verwendet die Anwendung ResizeImageSmall und ResizeImageLarge , um drei unterschiedlich große Bilder aus dem Quellbild zu erstellen. Anschließend lädt die Anwendung die Bilder mit der geänderten Größe in die Bildbibliothek hoch. Wenn keine drei Bilder unterschiedlicher Größe benötigt werden, lädt die Anwendung drei Bilder gleicher Größe in die Bildbibliothek hoch.
static string UploadImageToSpo(string PictureName, Stream ProfilePicture)
{
try
{
string spPhotoPathTempate = "/User Photos/Profile Pictures/{0}_{1}Thumb.jpg"; // Path template to picture library on the My Site host.
string spImageUrl = string.Empty;
// Create SharePoint Online Client context to My Site host.
ClientContext mySiteclientContext = new ClientContext(_mySiteUrl);
SecureString securePassword = GetSecurePassword(_sPoAuthPasword);
// Provide authentication credentials using Office 365 authentication.
mySiteclientContext.Credentials = new SharePointOnlineCredentials(_sPoAuthUserName, securePassword);
if (!_appConfig.Thumbs.Upload3Thumbs) // Upload a single input image only to picture library, no resizing necessary.
{
spImageUrl = string.Format(spPhotoPathTempate, PictureName, "M");
LogMessage("Uploading single image, no resize, to " + spImageUrl, LogLevel.Information);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(mySiteclientContext, spImageUrl, ProfilePicture, true);
}
else if (_appConfig.Thumbs.Upload3Thumbs && !_appConfig.Thumbs.CreateSMLThumbs)// Upload three images of the same size.
{
// The following code is not optimal. Upload the same source image three times with different names.
// No resizing of images necessary.
LogMessage("Uploading threes image to SPO, no resize", LogLevel.Information);
spImageUrl = string.Format(spPhotoPathTempate, PictureName, "M");
LogMessage("Uploading medium image to " + spImageUrl, LogLevel.Information);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(mySiteclientContext, spImageUrl, ProfilePicture, true);
ProfilePicture.Seek(0, SeekOrigin.Begin);
spImageUrl = string.Format(spPhotoPathTempate, PictureName, "L");
LogMessage("Uploading large image to " + spImageUrl, LogLevel.Information);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(mySiteclientContext, spImageUrl, ProfilePicture, true);
ProfilePicture.Seek(0, SeekOrigin.Begin);
spImageUrl = string.Format(spPhotoPathTempate, PictureName, "S");
LogMessage("Uploading small image to " + spImageUrl, LogLevel.Information);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(mySiteclientContext, spImageUrl, ProfilePicture, true);
}
else if (_appConfig.Thumbs.Upload3Thumbs && _appConfig.Thumbs.CreateSMLThumbs) //Generate 3 different sized images.
{
LogMessage("Uploading threes image to SPO, with resizing", LogLevel.Information);
// Create three images based on recommended sizes for SharePoint Online.
// Create small-sized image.
using (Stream smallThumb = ResizeImageSmall(ProfilePicture, _smallThumbWidth))
{
if (smallThumb != null)
{
spImageUrl = string.Format(spPhotoPathTempate, PictureName, "S");
LogMessage("Uploading small image to " + spImageUrl, LogLevel.Information);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(mySiteclientContext, spImageUrl, smallThumb, true);
}
}
// Create medium-sized image.
using (Stream mediumThumb = ResizeImageSmall(ProfilePicture, _mediumThumbWidth))
{
if (mediumThumb != null)
{
spImageUrl = string.Format(spPhotoPathTempate, PictureName, "M");
LogMessage("Uploading medium image to " + spImageUrl, LogLevel.Information);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(mySiteclientContext, spImageUrl, mediumThumb, true);
}
}
// Create large-sized image. This image is shown when you open the user's OneDrive for Business.
using (Stream largeThumb = ResizeImageLarge(ProfilePicture, _largeThumbWidth))
{
if (largeThumb != null)
{
spImageUrl = string.Format(spPhotoPathTempate, PictureName, "L");
LogMessage("Uploading large image to " + spImageUrl, LogLevel.Information);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(mySiteclientContext, spImageUrl, largeThumb, true);
}
}
}
// Return URL of the medium-sized image to set properties on the user profile.
return _mySiteUrl + string.Format(spPhotoPathTempate, PictureName, "M");
}
catch (Exception ex)
{
LogMessage("User Error: Failed to upload thumbnail picture to SPO for " + PictureName + " " + ex.Message, LogLevel.Error);
return string.Empty;
}
}
SetMultipleProfileProperties legt mehrere Benutzerprofileigenschaften in einem einzigen Methodenaufruf fest. In diesem Codebeispiel legt SetMultipleProfileProperties die folgenden Benutzerprofileigenschaften für einen Benutzer fest:
PictureURL: Festgelegt auf die URL des mittelgroßen hochgeladenen Bilds in der Bildbibliothek auf dem Host für "Meine Website".
SPS-PicturePlaceholderState: Auf Null festgelegt, um anzugeben, dass SharePoint Online das hochgeladene Bild für den Benutzer anzeigen soll.
static void SetMultipleProfileProperties(string UserName, string[] PropertyName, string[] PropertyValue)
{
LogMessage("Setting multiple SPO user profile properties for " + UserName, LogLevel.Information);
try
{
int arrayCount = PropertyName.Count();
UPSvc.PropertyData[] data = new UPSvc.PropertyData[arrayCount];
for (int x = 0; x < arrayCount; x++)
{
data[x] = new UPSvc.PropertyData();
data[x].Name = PropertyName[x];
data[x].IsValueChanged = true;
data[x].Values = new UPSvc.ValueData[1];
data[x].Values[0] = new UPSvc.ValueData();
data[x].Values[0].Value = PropertyValue[x];
}
_userProfileService.ModifyUserPropertyByAccountName(UserName, data);
// LogMessage("Finished setting multiple SharePoint Online user profile properties for " + UserName, LogLevel.Information);
}
catch (Exception ex)
{
LogMessage("User Error: Exception trying to update profile properties for user " + UserName + "\n" + ex.Message, LogLevel.Error);
}
}
SetAdditionalProfileProperties legt alle zusätzlichen Benutzerprofileigenschaften fest, die Sie nach dem Hochladen der Imagedateien aktualisieren möchten. Sie können zusätzliche Eigenschaften angeben, die in der configuration.xml Datei aktualisiert werden sollen.
static void SetAdditionalProfileProperties(string UserName)
{
if (_appConfig.AdditionalProfileProperties.Properties == null) // If there are no additional properties to update.
return;
int propsCount = _appConfig.AdditionalProfileProperties.Properties.Count();
if (propsCount > 0)
{
string[] profilePropertyNamesToSet = new string[propsCount];
string[] profilePropertyValuesToSet = new string[propsCount];
// Loop through each property in configuration file.
for (int i = 0; i < propsCount; i++)
{
profilePropertyNamesToSet[i] = _appConfig.AdditionalProfileProperties.Properties[i].Name;
profilePropertyValuesToSet[i] = _appConfig.AdditionalProfileProperties.Properties[i].Value;
}
// Set all properties in a single call.
SetMultipleProfileProperties(UserName, profilePropertyNamesToSet, profilePropertyValuesToSet);
}
}