Saturday, April 7, 2012

Get Root Organization from Organization Profile

Leave a Comment

When user profile service application is created. Organization profile architecture creates Default organization which is Root Organization.  Logically every organization needs to have at least one department. So having Root Organization makes easy to extend by creating child organization or immediate members

To get Root Organization  one can use following code block

SPSite site = new SPSite("site url");
SPServiceContext serviceContext = SPServiceContext .GetContext(site);
OrganizationProfileManager organizationProfileManager = new OrganizationProfileManager(serviceContext);
OrganizationProfile orgProfile = organizationProfileManager .RootOrganization;

But when you modify name of the Root Organization, property returns null.

So when Root Organization returns null, how to get the Root Organization. ?

OrganizationProfileManager has method GetOrganizationProfile .  Root Organization is default organization which has ID 1. So you can get OrganizationProfile instance by ID 1.

OrganizationProfile orgProfile = organizationProfileManager GetOrganizationProfile(1);

Other way to get Organization Profile would be to Search all the ProfileBase .

For a case I want to search Organization Profile "SharePoint"
 var results = organizationProfileManager.Search("SharePoint");

      OrganizationProfile orgProfile;
      foreach (var profile in results)
        if(profile is OrganizationProfile)
          if(profile.DisplayName == "SharePoint")
            orgProfile = profile as OrganizationProfile;