WSS 3.0 オブジェクト モデルを使用して、Sharepoint のユーザー写真でユーザーを取得しようとしています。私は解決策を求めてウェブを閲覧してきましたが、これまでのところ、それを行う方法を見つけることができませんでした. それは可能ですか?
3 に答える
これは、作業を完了するのに役立つコード スニペットです。例外を回避するために、追加の検証を行う必要がある場合があります (プロファイルが実際に存在することを確認する、画像 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
(MOSS ではなく) WSS 3.0 について厳密に話している場合、実際にはグローバル ユーザー プロファイル自体はありませんが、各サイト コレクションには非表示のユーザー情報リストがあります。つまり、Microsoft.Office.Server 名前空間にあるものは何も利用できません。
ただし、ユーザーの画像への URL を知っていれば、ユーザー情報リストをプログラムで更新できます。ある種の昇格された特権で実行している限り、他の SharePoint リストと同じように、このリストを操作できるはずです。このリストはサイト コレクションの範囲にのみ適していることに注意してください。そのため、実際に写真の URL を取得するには、ユーザーがあちこちで同じ更新を行う必要があります。Plus ユーザーは、誰かが何らかの権限を付与するまでユーザー情報リストに登録されないため、ドメイン内のすべてのユーザーがそこに登録されるわけではありません。
これを処理するクリーンな方法は、間違いなくユーザー プロファイル メカニズムが 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}
//
}