public void UploadPhoto(string url, string accountName, byte[] image)
{
  using (SPSite site = new SPSite(url))
  {
    using (SPWeb web = site.OpenWeb())
    {
     ProfileImagePicker profileImagePicker = new ProfileImagePicker();
     InitializeProfileImagePicker(profileImagePicker, web);
     SPFolder subfolderForPictures = GetSubfolderForPictures(profileImagePicker);

     string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);

     int largeThumbnailSize = 120;
     int mediumThumbnailSize = 90;
     int smallThumbnailSize = 30;

     using (MemoryStream stream = new MemoryStream(image))
     {
       using (Bitmap bitmap = new Bitmap(stream, true))
       {
        CreateThumbnail(bitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_LThumb.jpg");
        CreateThumbnail(bitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_MThumb.jpg");
        CreateThumbnail(bitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_SThumb.jpg");
       }
      }
     }
   }
  }

  string GetFileNameFromAccountName(string accountName)
  {
    string result = accountName;
    string charsToReplace = @"\/:*?""<>|";
    Array.ForEach(charsToReplace.ToCharArray(), charToReplace => result = result.Replace(charToReplace, '_'));
    return result;
  }

  void InitializeProfileImagePicker(ProfileImagePicker profileImagePicker, SPWeb web)
  {
    Type profileImagePickerType = typeof(ProfileImagePicker);

    FieldInfo fieldInfo = profileImagePickerType.GetField("m_objWeb", BindingFlags.NonPublic | BindingFlags.Instance);
    fieldInfo.SetValue(profileImagePicker, web);

    MethodInfo miLoadPictureLibraryInternal= profileImagePickerType.GetMethod("LoadPictureLibraryInternal", BindingFlags.NonPublic | BindingFlags.Instance);

    if (miLoadPictureLibraryInternal!= null)
    {
       miLoadPictureLibraryInternal.Invoke(profileImagePicker, new object[] { });
    }
   }

   SPFile CreateThumbnail(Bitmap original, int idealWidth, int idealHeight, SPFolder folder, string fileName)
   {
     SPFile file = null;
     Assembly userProfilesAssembly = typeof(UserProfile).Assembly;
     Type userProfilePhotosType = userProfilesAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfilePhotos");

     MethodInfo miCreateThumbnail = userProfilePhotosType.GetMethod("CreateThumbnail", BindingFlags.NonPublic | BindingFlags.Static);

     if (miCreateThumbnail != null)
     {
        file = (SPFile)miCreateThumbnail.Invoke(null, new object[] { original, idealWidth, idealHeight, folder, fileName });
     }

     return file;
   }

   SPFolder GetSubfolderForPictures(ProfileImagePicker profileImagePicker)
   {
     SPFolder folder = null;
     Type profileImagePickerType = typeof(ProfileImagePicker);

     MethodInfo miGetSubfolderForPictures = profileImagePickerType.GetMethod("GetSubfolderForPictures", BindingFlags.NonPublic | BindingFlags.Instance);

     if (miGetSubfolderForPictures != null)
     {
        folder = (SPFolder)mi_GetSubfolderForPictures.Invoke(profileImagePicker, new object[] { });
     }

     return folder;
 }
Tagged with:  

We can create, delete, edit profiles by using:

  • Microsoft.Office.Server.UserProfiles.UserProfileManager
  • Web service (http://your_sharepoint_server_url/_vti_bin/UserProfileService.asmx)

Via Web Service

public string GetUserProfileAccountName(int profileIndex)
{
   UserProfileServiceRef.UserProfileService clntUserProfile = new UserProfileServiceRef.UserProfileService();
   clntUserProfile.Credentials = new NetworkCredential("spsadmin", "*****");

   var propertyDataCollection = clntUserProfile.GetUserProfileByIndex(profileIndex).UserProfile;

   if(propertyDataCollection !=null)
   {
    UserProfileServiceRef.PropertyData accountNamePropertyData = propertyDataCollection.GetValue(1) as UserProfileServiceRef.PropertyData;
    string accountName = accountNamePropertyData.Values[0].Value.ToString();
    return accountName;
   }
   return accountName;
 }

 public string GetUserProfileProperty(string accountName, string propertyName)
 {
   UserProfileServiceRef.UserProfileService clntUserProfile = new UserProfileServiceRef.UserProfileService();
   clntUserProfile.Credentials = new NetworkCredential("spsadmin", "*****");

   UserProfileServiceRef.PropertyData data = clntUserProfile.GetUserPropertyByAccountName(accountName, propertyName) as UserProfileServiceRef.PropertyData;

   if (data.Values != null && data.Values.Length > 0)
   {
      return data.Values[0].Value.ToString();
   }

  return null;
}

public void SetUserProfileProperty(string accountName,string propertyName,object value)
{
  UserProfileServiceRef.UserProfileService clntUserProfile = new UserProfileServiceRef.UserProfileService();
  clntUserProfile.Credentials = new NetworkCredential("spsadmin", "*****");

  UserProfileServiceRef.PropertyData[] data = new UserProfileServiceRef.PropertyData[1];
  data[0] = new UserProfileServiceRef.PropertyData();
  data[0].Name = propertyName;
  data[0].IsValueChanged = true;
  data[0].Values = new UserProfileServiceRef.ValueData[1];
  data[0].Values[0] = new UserProfileServiceRef.ValueData();
  data[0].Values[0].Value = value;

  clntUserProfile.ModifyUserPropertyByAccountName(accountName, data);
}

Continue reading »

Tagged with:  

Let’s build a dashbord that consists of RemainingWork as Report, CompletedWork as Scorecard and a time filter.

Step 1. To Create Data Connections (Tfs_Analysis_DS)

  • Data Connections right-click -> New Data Source -> Analysis Services

  • “Time” tab :

Continue reading »

Assume that you have a running User Profile synchronization Service, configured with an Active Directory connection and you want to get additional user information from a database/service, which requires a Business Data Catalog (BDC or BCS) connection.

Step 1. Create A New User Profile Service Application (My User Profile Service Application)

(Skip this step if you have already configured an User Profile Service with an Active Directory Connection.)

Central Administration -> Manage service applications -> New-> User Profile Service Application

Step 2. Start If The Profile Service Is Stopped

(Skip this step if you have already configured an User Profile Service with an Active Directory Connection.)

Central Administration -> Manage services on server -> User Profile Service -> Start
Central Administration -> Manage services on server -> User Profile Synchronization Service -> Start

Step 3. Configure The Profile Service Application

Create Active Directory Connection (MyADConnection)

(Skip this connection if you have already configured an Active Directory Connection.)

Central Administration -> Manage service applications -> My User Profile Service Application -> Synchronization -> Configure Synchronization Connections -> Create New Connection

Select your user by clicking to the ”Populate Containers” button.

Create BDC Connection (MyBDCConnection)

Central Administration -> Manage service applications -> My User Profile Service Application -> Synchronization -> Configure Synchronization Connections -> Create New Connection

In this scenerio, assume that, previously you have developed an external content type , MyBDCService, which is designed to read data from an employee table and WorkMail column is unique. Also this external content type consists of a ReadItem operation (accepts WorkMail as a parameter and returns an employee) and a ReadList operation (returns all employees).

Step 4. Map Properties

Central Administration -> Manage service applications -> My User Profile Service Application -> People -> Manage User  Properties -> New Property

Name: Company
Add New Mapping -> Source Data Connection : MyBDCConnection | Attribute: Company | Direction: Import

Be sure that WorkEmail property is mapped with MyADConnection.

Step 5. Include existing BCS connections for synchronization

Central Administration -> Manage service applications -> My User Profile Service Application -> Synchronization -> Configure Synchronization Settings -> Check “Include existing BCS connections for synchronization?”

Step 6. Start Synchronization

Central Administration -> Manage service applications -> My User Profile Service Application -> Synchronization -> Start Profile Synchronization

After the sync you will see that related employee company property is filled.

If you want to navigate through all folders and files to change a property’s value automatically, you can use a code like this:

EditListProperty(“http://yourwebsiteurl/”, “Shared Documents”, null, “Author”, “burcak”);

 public static void EditListProperty(string siteUrl, string listName, string folderName, string propertyName, object propertyValue)
 {
    using (SPSite site = new SPSite(siteUrl))
    {
     using (SPWeb web = site.OpenWeb())
     {
      SPList list = web.Lists[listName];
      SPListItemCollection items = list.Items;

      if (!string.IsNullOrEmpty(folderName))
      {
       SPQuery query = new SPQuery();
       query.Folder = web.GetFolder(folderName);
       items = list.GetItems(query);
      }

      foreach (SPListItem item in items)
      {
       NavigateTroughItems(list, item, propertyName, propertyValue);
      }
    }
  }
}

void NavigateTroughItems(SPList list, SPListItem item, string propertyName, propertyValue)
{
 if (item.FileSystemObjectType == SPFileSystemObjectType.File)
 {
    EditListItemProperty(item, propertyName, propertyValue);
 }

 else if (item.FileSystemObjectType == SPFileSystemObjectType.Folder)
 {
  SPQuery query = new SPQuery();
  query.Folder = item.Folder;

  SPListItemCollection items = list.GetItems(query);

  foreach (SPListItem newItem in items)
  {
    NavigateTroughItems(list, newItem, propertyName, propertyValue);
  }
 }
}

void EditListItemProperty(SPListItem item, string propertyName, object propertyValue)
{
 try
 {
  item.File.CheckOut();
  item[propertyName]= propertyValue;
  item.Update();
  string comment = string.Format("file {0} is changed",propertyName);
  item.File.CheckIn(comment);
 }

 catch (Exception ex)
 {
   item.File.UndoCheckOut();
 }
}
Tagged with:  
iBlog by PageLines