Moss2010更新用户配置信息
最近有这样一个需求,需要将所有用户图片更新。Moss里的图片是存放的三张大小不同的jpg格式。在这里做下总结。
更新用户图片首先要获取用户图片。读用户配置信息有二种方式:读AD实现,或读用户配置。
先说读用户配置
View Code
1 mySites=newSPSite("http://moss2010/my"); 2 3 myWeb=mySites.OpenWeb(); 4 5 SPFolder subfolderForPictures=myWeb.GetFolder("User Photos\\Profile Pictures"); 6 7 UserProfileManageruserProfileManager=newUserProfileManager(SPServiceContext.GetContext(mySites)); 8 9 10 11 if (userProfileManager.UserExists(strAccountName)) 12 13 { 14 15 returnuserProfileManager.GetUserProfile(strAccountName); 16 17 } 18 19 else 20 21 { 22 23 returnnull; 24 25 } 26 27 /// <summary> 28 /// 获取用户属性 29 /// </summary> 30 /// <param name="accountName"></param> 31 /// <param name="propertyDisplayName">属性显示名称</param> 32 /// <returns></returns> 33 public static object GetUserProperty(string accountName, string propertyDisplayName) 34 { 35 object userPropertyValue = null; 36 UserProfile userProfile = GetUserProfile(accountName); 37 if (userProfile != null) 38 { 39 var profileSubtypeProperty = userProfile.Properties.First(propertyBase => propertyBase.DisplayName == propertyDisplayName); 40 if (profileSubtypeProperty != null) 41 { 42 userPropertyValue = userProfile.GetProfileValueCollection(profileSubtypeProperty.Name).Value; 43 } 44 else 45 { 46 Wicresoft.ITSG.Components.Utilities.LogUtility.CreateLogManager().Debug("用户【" + accountName + "】的属性【" + propertyDisplayName + "】不存在!"); 47 } 48 } 49 return userPropertyValue; 50 }
这样就获取了所有的用户配置信息,根据用户配置信息读取所有用户图片并存在DataTable里面
entity.ImageUrl =SPUserProfileUtility.GetUserProperty(accountName, "图片") == null || SPUserProfileUtility.GetUserProperty(accountName, "图片").ToString() == "" ?/people_small_10.gif" : SPUserProfileUtility.GetUserProperty(accountName, "图片").ToString();//图片URL
再说读AD
//得到网站集合 SPSite mySites = new SPSite(MySiteHostUrl); //得到网站 SPWeb myWeb = mySites.OpenWeb(); SPFolder subfolderForPictures = myWeb.GetFolder("User Photos\\Profile Pictures"); if (subfolderForPictures == null) { return; } UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.GetContext(mySites)); //***********************************读取AD照片信息 DirectoryEntry de = new DirectoryEntry("LDAP://DC=contoso,DC=com", "contoso\\mossadmin", "Passw0rd!", AuthenticationTypes.Secure); //读取AD的配置信息 DirectorySearcher search = new DirectorySearcher(); search.SearchRoot = de; //contoso\\test7照片信息 search.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=test7))"; search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("thumbnailPhoto"); SearchResult user; user = search.FindOne(); if (user == null) { return; } byte[] buffer = (byte[])user.Properties["thumbnailPhoto"][0];
获取到用户图片信息后,将来源图片全部转为JPG文件
View Code
for (int i = 0; i < DT_NOPic.Rows.Count; i++) { try { MAccountName = DT_NOPic.Rows[i]["username"].ToString(); string fileNameWithoutExtension = GetFileNameFromAccountName(MAccountName); accountName = fileNameWithoutExtension.Split(new char[] { '_' }); if (accountName[1].ToString() == oracle_Accountname) { byte[] buffer = oracle_byte_Phono; //图片流 try { int largeThumbnailSize = 0X90; int mediumThumbnailSize = 0X60; int smallThumbnailSize = 0X20; try { using (MemoryStream stream = new MemoryStream(buffer)) { using (Bitmap bitmap = new Bitmap(stream, true)) { //判断下是否为gif //判断下是否为gif if (IsAllowedExtension(buffer, new FileExtension[] { FileExtension.GIF, FileExtension.PNG})) { using (MemoryStream newstream = new MemoryStream()) { bitmap.Save(newstream, System.Drawing.Imaging.ImageFormat.Jpeg); // using (Bitmap newbitmap = new Bitmap(newstream, true)) { CreateThumbnail(newbitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_LThumb.jpg"); CreateThumbnail(newbitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_MThumb.jpg"); CreateThumbnail(newbitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_SThumb.jpg"); } } } if (IsAllowedExtension(buffer, new FileExtension[] { FileExtension.JPG })) { CreateThumbnail(bitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_LThumb.jpg"); CreateThumbnail(bitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_MThumb.jpg"); CreateThumbnail(bitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_SThumb.jpg"); } } } SetPictureUrl(MAccountName, subfolderForPictures, userProfileManager, mySites.Url); writer.WriteLine("用户名为" + DT_NOPic.Rows[i]["name"].ToString() + "工号为" + accountName[1].ToString() + "在 " + System.DateTime.Now.ToString("F") + " 添加图片成功" + row); Console.WriteLine("用户名为" + DT_NOPic.Rows[i]["name"].ToString() + "工号为" + accountName[1].ToString() + "在 " + System.DateTime.Now.ToString("F") + " 添加图片成功" + row); } catch (Exception ex) { writer.WriteLine(ex.Message); writer.WriteLine("用户名为" + DT_NOPic.Rows[i]["name"].ToString() + "工号为" + accountName[1].ToString() + "在 " + System.DateTime.Now.ToString("F") + " 添加图片失败" + row); Console.WriteLine("用户名为" + DT_NOPic.Rows[i]["name"].ToString() + "工号为" + accountName[1].ToString() + "在 " + System.DateTime.Now.ToString("F") + " 添加图片失败" + row); continue; } } catch (Exception ex) { writer.WriteLine(ex.Message); writer.WriteLine("用户名为" + DT_NOPic.Rows[i]["name"].ToString() + "工号为" + accountName[1].ToString() + "在 " + System.DateTime.Now.ToString("F") + " 添加图片失败"); Console.WriteLine("用户名为" + DT_NOPic.Rows[i]["name"].ToString() + "工号为" + accountName[1].ToString() + "在 " + System.DateTime.Now.ToString("F") + " 添加图片失败"); continue; } } } catch (Exception ex) { writer.WriteLine("用户名为" + DT_NOPic.Rows[i]["name"].ToString() + "未能更新图片,错误信息为" + ex.Message); writer.WriteLine("用户名为" + DT_NOPic.Rows[i]["name"].ToString() + "在 " + System.DateTime.Now.ToString("F") + " 添加图片失败"); continue; } row++; } private static SPFile CreateThumbnail(Bitmap original, int idealWidth, int idealHeight, SPFolder folder, string fileName) { SPFile file = null; // Workaround to get the Microsoft.Office.Server.UserProfiles assembly Assembly userProfilesAssembly = typeof(UserProfile).Assembly; // UserProfilePhotos is internal // so there is no visibility from Visual Studio Type userProfilePhotosType = userProfilesAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfilePhotos"); MethodInfo mi_CreateThumbnail = userProfilePhotosType.GetMethod("CreateThumbnail", BindingFlags.NonPublic | BindingFlags.Static); if (mi_CreateThumbnail != null) { try { file = (SPFile)mi_CreateThumbnail.Invoke(null, new object[] { original, idealWidth, idealHeight, folder, fileName }); } catch (TargetInvocationException targetEx) { if (targetEx.InnerException != null) { throw targetEx.InnerException; } } } return file; }
最后更新
View Code
1 private static void SetPictureUrl(string accountName, SPFolder subfolderForPictures, UserProfileManager userProfileManager, string siteUrl) 2 { 3 4 5 UserProfile userProfile = userProfileManager.GetUserProfile(accountName); 6 7 string fileNameWithoutExtension = GetFileNameFromAccountName(accountName); 8 9 string pictureUrl = String.Format("{0}/{1}/{2}_MThumb.jpg", siteUrl, subfolderForPictures.Url, fileNameWithoutExtension); 10 11 userProfile["PictureUrl"].Value = pictureUrl; 12 userProfile.Commit(); 13 }
posted on 2012-08-30 14:22 hardy_Wang 阅读(213) 评论(0) 编辑 收藏 举报