C# CreateOrganizationProfile错误对象引用未设置为对象的实例

C# CreateOrganizationProfile错误对象引用未设置为对象的实例,c#,sharepoint,service,profile,C#,Sharepoint,Service,Profile,我正在使用Microsoft代码在用户配置文件服务器中创建一个新组织,如所示 每次调用CreateOrganizationProfile我都会得到以下信息: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Office.Server.UserProfiles.OrganizationProfile.set_Parent(ProfileBase

我正在使用Microsoft代码在用户配置文件服务器中创建一个新组织,如所示

每次调用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;
    }
奇怪的是,有人在这里碰到了这个问题,但问题一直没有解决

我已确认用户配置文件服务正在运行和响应。此外,调用CreateOrganizationProfile时,parentOrg和SubsubName不为null

有人有什么我可以尝试的吗,或者有人能发现问题所在吗


非常感谢

我不是sharepoint专家,但这说明您需要用户配置文件管理权限,这可能暗示您需要用户配置文件管理器权限才能执行您尝试执行的操作

编辑:
由于您的代码试图“编写”某些内容,因此这似乎是相关的

基本上,它表示在调用
SPSecurity.runwithlevatedprivileges
之前,您缺少对
SPUtility.ValidateFormDigest()或
SPWeb.ValidateFormDigest()的调用。我也遇到了同样的问题,我通过更改行来修复它

OrganizationProfile parentOrg = opm.RootOrganization;

GetProfile(1)在我的案例中返回了根组织


希望这对别人有帮助

Yahia-我想你可能知道一些事情。。。我会调查并报告的!谢谢你的回答——如果是的话,我欠你一杯啤酒!任何其他建议也欢迎!如果有最新消息,我也面临同样的问题
OrganizationProfile parentOrg = (OrganizationProfile)opm.GetProfile(1);