WSS オブジェクト モデルを使用して SPUser からユーザーの写真を取得する
-
09-06-2019 - |
質問
WSS 3.0 オブジェクト モデルを通じて Sharepoint のユーザー写真上のユーザーを取得しようとしています。解決策を求めて Web を閲覧していますが、今のところ解決する方法が見つかりません。それは可能ですか?可能であればどのようにすればよいですか?
解決
ここに、作業を完了するのに役立つコード スニペットを示します。例外を避けるために、追加の検証を行う必要がある場合があります (プロファイルが実際に存在することの確認、画像 URL が実際に存在することの確認など)。
//get current profile manager
UserProfileManager objUserProfileManager = new UserProfileManager(PortalContext.Current);
//get current users profile
UserProfile profile = objUserProfileManager.GetUserProfile(true);
//get user image URL
string imageUrl = (string)profile[PropertyConstants.PictureUrl];
//do something here with imageUrl
他のヒント
厳密に WSS 3.0 (MOSS ではなく) について話している場合、実際にはグローバル ユーザー プロファイル自体はなく、各サイト コレクションに隠されたユーザー情報リストが存在します。つまり、Microsoft.Office.Server 名前空間にあるものはどれも利用できないということです。
ただし、ユーザーの写真の URL がわかっていれば、プログラムでユーザー情報リストを更新できます。何らかの昇格された特権で実行している限り、次のことができるはずです。 このリストを操作する 他の SharePoint リストと同様に。このリストはサイト コレクションの範囲にのみ適しているため、ユーザーが実際に写真の URL を取得するには、同じ更新をあらゆる場所で行う必要があることに注意してください。さらに、ユーザーは、誰かがユーザーに何らかの権限を割り当てるまでユーザー情報リストには表示されないため、ドメイン内のすべてのユーザーがそこに含まれるわけではありません。
これを処理するクリーンな方法は、間違いなくユーザー プロファイル メカニズムが MOSS であることですが、それがオプションである場合は、MOSS と WSS について質問するように質問を更新する必要があります。
ああ、UserProfileManager クラスを使用する必要があります。詳細はこちら: http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofilemanager.aspx
使用例:
public override void ItemAdded(SPItemEventProperties properties)
{
// Get list item on which the event occurred.
SPListItem item = properties.ListItem;
// Set the Author Image field to the user's PictureURL if it exists.
using (SPWeb web = properties.OpenWeb())
{
// Author: {C32DB804-FF2D-4656-A38A-B0394BA5C931}
SPFieldUserValue authorValue = new SPFieldUserValue(properties.OpenWeb(), item[new Guid("{C32DB804-FF2D-4656-A38A-B0394BA5C931}")].ToString());
UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(web.Site));
UserProfile profile = profileManager.GetUserProfile(authorValue.LookupId);
UserProfileValueCollection values = profile[PropertyConstants.PictureUrl];
if (values.Count > 0)
{
// Author Image: {37A5CA4C-7621-44d7-BF3B-583F742CE52F}
SPFieldUrlValue urlValue = new SPFieldUrlValue(values.Value.ToString());
item[new Guid("{37A5CA4C-7621-44d7-BF3B-583F742CE52F}")] = urlValue.Url;
}
}
item.Update();
// News Text: {7F55A8F0-4555-46BC-B24C-222240B862AF}
//
// Author Image: {37A5CA4C-7621-44d7-BF3B-583F742CE52F}
//
// Publish Date: {45E84B8B-E161-46C6-AD51-27A42E4992B5}
//
}