Ошибка CreateOrganizationProfile Ссылка на объект не установлена ​​на экземпляр объекта - PullRequest
1 голос
/ 25 июля 2011

Я использую код Microsoft для создания новой организации на сервере профилей пользователей в соответствии с http://msdn.microsoft.com/en-us/library/ms545122.aspx.

Каждый раз, когда я звоню CreateOrganizationProfile , я получаю следующее:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Office.Server.UserProfiles.OrganizationProfile.set_Parent(ProfileBase value)

Точный код, который я использую:

    [WebMethod]
    public void CreateOrganisation(string OrganisationName)
    {
       SPSecurity.RunWithElevatedPrivileges(delegate()
       {
            using (SPSite site = new SPSite(_serverName))
            {
                // Removing this will cause the error "Operation is not valid due to the current state of the object".
                HttpContext.Current = null;

                SPServiceContext context = SPServiceContext.GetContext(site);

                ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);

                // choose default organization profile subtype as the subtype
                string subtypeName = ProfileSubtypeManager.GetDefaultProfileName(ProfileType.Organization);
                ProfileSubtype subType = psm.GetProfileSubtype(subtypeName);

                OrganizationProfileManager opm = new OrganizationProfileManager(context);

                // choose Root Organization as the parent
                OrganizationProfile parentOrg = opm.RootOrganization;

                // create an organization profile and set its display name
                OrganizationProfile profile = opm.CreateOrganizationProfile(subType, parentOrg);
                profile.DisplayName = "Test Org1";

                // commit to save changes
                profile.Commit();

            }
        });

        return;
    }

Любопытно, что кто-то еще столкнулся с точной проблемой здесь http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/7b5101fd-0ea6-4716-82b1-ac4609b9973c/,, но она так и не была решена.

Я подтвердил, что служба профилей пользователей работает и отвечает. Кроме того, parentOrg и subtypeName не равны нулю при вызове CreateOrganizationProfile.

У кого-нибудь есть что-нибудь, что я могу попробовать, или кто-нибудь может определить, в чем может быть проблема?

Очень благодарен!

Ответы [ 2 ]

1 голос
/ 02 февраля 2012

У меня была такая же проблема, и я исправил ее, изменив строку

OrganizationProfile parentOrg = opm.RootOrganization;

Для

OrganizationProfile parentOrg = (OrganizationProfile)opm.GetProfile(1);

GetProfile (1) вернул RootOrganization в моем случае.

Надеюсь, это кому-нибудь поможет!

1 голос
/ 25 июля 2011

Я не эксперт по sharepoint, но в этом http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.organizationprofilemanager.createorganizationprofile.aspx указано, что вам нужны административные разрешения для профиля пользователя, и это http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.organizationprofile.parent.aspx может быть намеком на то, что вам нужно разрешение менеджера профилей пользователей, чтобы делать то, что вы пытаетесь сделать .

EDIT:
поскольку ваш код пытается что-то написать, это кажется уместным http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
в основном это говорит о том, что вы пропустили вызов на SPUtility.ValidateFormDigest() или SPWeb.ValidateFormDigest() перед вызовом SPSecurity.RunWithElevatedPrivileges

...