2

I'm wondering if it's possible to get just one section of the users profile properties.

Since i created a new section with custom properties, i would like to call the whole section (including all of its custom properties) instead of calling them singularly because if im adding or changing properties afterwards i'd have to change the webpart-code and re-deploy it..

Any Suggestions? ProfileSubjectManager or ProfileValueCollectionBase don't seem helpful..

  • I'm also looking into the possibilty to iterate through all properties looking for the corresponding section but can't figure out what shows me the context between property and section. – Peter Sep 23 '11 at 11:58
3

Take a look at my answer provided here: SharePoint 2010 - Change order of User Profile properties

Section is just another profile property and it is not a container of properties. Basically you can go over properties, find desired section property and then read properties until you find another section property.

Addition

I have manage to make one simple code example. It finds for all properties under default Details section:

// Details section 'iternal' Name
string sectionName = "SPS-Section-Details";
// result
List<string> sectionProperties = new List<string>();

using (SPSite site = new SPSite("http://MyCoolSite/"))
{
    SPServiceContext context = SPServiceContext.GetContext(site);
    ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
    ProfileSubtype ps = 
       psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
    ProfileSubtypePropertyManager pspm = ps.Properties;

    ArrayList allProps = pspm.PropertiesWithSection;

    ProfileSubtypeProperty section = pspm.GetSectionByName(sectionName);
    int index = allProps.IndexOf(section) + 1; // next property index

    for (int i = index; i < allProps.Count; i++)
    {
        var prop = (ProfileSubtypeProperty) allProps[i];
        // If property is section break
        if (prop.IsSection) break;
        sectionProperties.Add(prop.Name);
    }
}

Disclaimer: This code snippet is tested on default User Profile Service

Extra

To get property values of specific user put this code into using statement:

UserProfileManager mng = new UserProfileManager(context );
UserProfile profile = mng.GetUserProfile([ACCOUNTNAME]);
foreach(string prop in sectionProperties)
{
    if(profile[prop] != null)
    {
        // Do something with profile[prop].Value.ToString()
    }
}

This code is for example purposes only.

  • If i'm getting this right, im supposed to use UserProfile.GetEnumerator and look for the section property type? – Peter Sep 23 '11 at 12:28
  • I think so. Right now I don't have time to test it and to provide some code :( I posted just to point you in right direction. Also I didn't have any luck with Google on this issue. – Vedran Rasol Sep 23 '11 at 12:32
  • @Peter I have update my answer with code example. – Vedran Rasol Sep 23 '11 at 14:36
  • nearly solved ? – Vedran Rasol Sep 24 '11 at 16:23
  • I tried to implement it and it works really fine.. I can access the whole property which is great. But i also need to get to the entered string or whatever type it is which is stored for each user inside the property. In my old webpart i did it with UserProfile u = profileManager.GetUserProfile(sAccount); and accessed the Value by u[propertyID].Value.ToString(); i'm having problems merging these approaches since i don't know how to get this into the index logic... – Peter Sep 28 '11 at 10:21
0

Thanks, i got it now.. im programming C# and SP since round about 1 1/2 month now and i had troubles getting the code right.. im now using only the for loop like this:

for (int i = index; i < allProps.Count; i++)
            {
                var prop = (ProfileSubtypeProperty)allProps[i];
                // If property is section break
                if (prop.IsSection) break;
                sectionProperties.Add(prop.DisplayName.ToString());
                tempProperty = prop.Name.ToString();
                result = profile[tempProperty].Value.ToString();
                sectionProperties.Add(result);
            } 

thanks a lot though Vedran!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.