All User Profiles not being returned

Jan 7, 2009 at 9:59 PM
First of all, thanks for writing this web part. It appears to be a nicely constructed component.

I am, however, having some difficulty. It appears that the profiles being returned are only a small subset of the total number of profiles that I have. I have used Reflector and found that the list you are retrieving is SiteUserInfoList. I'm not positive, but I don't believe that will return the items from the User Profile store for the site collection? Do you not need to be accessing the UserProfile objects in the UserProfileManager?

Also, it appears that you must have elevated permissions to view a page with the web control on it (for example, an anonymous user cannot access the page). Are there plans to modify the code to run with elevated permissions to allow display of the data?

Thanks,
Richard
Coordinator
Jan 19, 2009 at 9:52 AM
Edited Jan 19, 2009 at 9:54 AM
Hi Richard,

Thank You for your feedback!! 
I do retrieve the user list from the SiteUserInfoList, but I also use UserProfileManager to get user properties since in the SiteUserInfoList we have just few of them.
I'll check for the user limit problem ...
About the elevated permission it is true, i do not use elevated permission at the moment since, usually, anonymous users should not have access to the user list and properties, anyway I can add a flag in the webpart configuration to enable it and also trap the access denied error in order to keep the page visible if the (run)elevated permission is not used!

Stay tuned, i'll will include this feature in the first beta that i'm going to release at the end of Jan 2009.
Thanks,
Stefano






Jan 19, 2009 at 3:58 PM

Hi Stefano,

I used reflector to get to your source code and replaced GetUsersXml with the following. This let me do what I wanted to do, which was display user profiles. Note that SPSecurity.RunWithElevatedPrivileges requires the following hotfix to work properly with iterating over the UserProfiles: http://support.microsoft.com/kb/952294 .

Thanks again, as the XSLT based control is a really neat solution to my problem.

Regards,
Richard


public string GetUsersXml(string filter)
  {
   XmlDocument document = new XmlDocument();

   SPSecurity.RunWithElevatedPrivileges(delegate()
   {

    using (SPSite elevatedSite = new SPSite(currentSite.ID))
    {
     ServerContext elevatedContext = ServerContext.GetContext(elevatedSite);

     document.LoadXml("<rs:data xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema'></rs:data>");

      UserProfileManager profileManager = new UserProfileManager(elevatedContext);

      foreach (UserProfile userProfile in profileManager)
      {
       XmlElement newChild = document.CreateElement("z", "row", "#RowsetSchema");
       bool flag = false;
       foreach (Property property in profileManager.Properties)
       {
        try
        {
         XmlAttribute attribute2 = document.CreateAttribute(property.Name);
         try
         {
          attribute2.Value = userProfile[property.Name].ToString();
          if (!string.IsNullOrEmpty(filter))
          {
           string str = attribute2.Value.ToLower();
           string str2 = filter.ToLower();
           if (str.Contains(str2))
           {
            flag = true;
           }
          }
          else
          {
           flag = true;
          }
         }
         catch (NullReferenceException)
         {
          attribute2.Value = "";
         }
         newChild.Attributes.Append(attribute2);

         continue;
        }
        catch
        {
         continue;
        }
       }

       if (flag)
       {
        document.DocumentElement.AppendChild(newChild);
       }

      }    

     }
    });

   return document.OuterXml;
  }