开发者

CreateOrganizationProfile error Object reference not set to an instance of an object

I'm using Microsoft code to create a new Organisation in the User Profile Server, as per http://msdn.microsoft.com/en-us/library/ms545122.aspx.

Every time I call CreateOrganizationProfile I get the following:

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

The exact code I'm using is:

    [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;
    }

Curiously, somebody else ran into the exact problem here http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/7b5101fd-0ea6-4716-82b1-ac4609b9973c/, but it was never resolved.

I've confirmed the User Profile Service is running and responding. Also, parentOrg and s开发者_开发问答ubtypeName are not null when calling CreateOrganizationProfile.

Does anybody have anything I can try, or can anybody spot what might be the problem?

Very grateful!


I had the same problem and I fixed it by changing the line

OrganizationProfile parentOrg = opm.RootOrganization;

To

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

GetProfile(1) returned the RootOrganization in my case.

Hope this helps someone!


I am no sharepoint expert but this http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.organizationprofilemanager.createorganizationprofile.aspx states that you need user profile administrative permissions and this http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.organizationprofile.parent.aspx could be hint that you need user profile manager permission to do what you are trying to do.

EDIT:
since your code try to "write" something this seems relevant http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
basically it says you are missing a call to SPUtility.ValidateFormDigest() or SPWeb.ValidateFormDigest() before calling SPSecurity.RunWithElevatedPrivileges

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜