SharePoint 2010 -代码操作个人站点下的照片

需要引用如下DLL
image
代码如下:
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
//设置站点图片
using (SPSite site = new SPSite("siteUrl"))
{
   SPServiceContext context = SPServiceContext.GetContext(site);
   UserProfileManager myUserProfileManager = new UserProfileManager(context);

   try
   {
      foreach (UserProfile aUser in myUserProfileManager)
      {
         string origUrl = (string)aUser[PropertyConstants.PictureUrl].Value;
         string newUrl = origUrl.Replace("http://mysite", "https://mysite");
         aUser[PropertyConstants.PictureUrl].Value = newUrl;
         aUser.Commit();
      }
   }
   catch (System.Exception ex)
   {
      Console.WriteLine(ex.Message);
   }
}
 
//得到个人站点下的照片
//得到当前站点
using (SPSite aSite = SPContext.Current.Site)
{
         if (aSite != null)
         {
                 SPWeb web = SPContext.Current.Web;
                 if (web != null)
                  {
                       SPServiceContext context = SPServiceContext.GetContext(aSite);
                       UserProfileManager myUserProfileManager = new UserProfileManager(context);
                       string userName = web.CurrentUser.LoginName; //Environment.UserDomainName + "\\" + Environment.UserName;
                       if (!string.IsNullOrEmpty(userName))
                       {
                           //管理员为administrator必须特殊处理
                               if (userName.ToLower() == "sharepoint\\system")
                            {
                                 userName = string.Format("{0}\\administrator",this.DomainName);
                            }
                      }
                      UserProfile currentUser = myUserProfileManager.GetUserProfile(userName);
                      string origUrl = (string)currentUser[PropertyConstants.PictureUrl].Value;
                      //如果没有照片即可设置默认值
                         this.Image_MyPhoto.ImageUrl = string.IsNullOrEmpty(origUrl) == true ? "/_layouts/Images/MySitePictureWebPart/14nophoto.png" : origUrl;
                  }
           }
 }
posted @ 2012-02-28 16:57  love007  阅读(474)  评论(0编辑  收藏  举报