2011-07-25 222 views
1

我正在使用Microsoft代碼在用戶配置文件服務器中創建新組織,如http://msdn.microsoft.com/en-us/library/ms545122.aspxCreateOrganizationProfile錯誤對象引用未設置爲對象的實例

每次我打電話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/ ,但它從未解決。

我已確認用戶配置文件服務正在運行並進行響應。另外,調用CreateOrganizationProfile時,parentOrg和subtypeName不爲空。

有沒有人有我可以嘗試的東西,或者任何人都可以發現什麼可能是問題?

非常感謝!

+0

有任何更新,我面臨同樣的問題 –

回答

1

我不是共享點專家,但此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可能暗示您需要用戶配置文件管理員權限才能執行您正在嘗試執行的操作。

編輯:
因爲你的代碼嘗試「寫」的東西,這似乎相關http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
基本上它說你缺少對SPUtility.ValidateFormDigest()SPWeb.ValidateFormDigest()通話主叫SPSecurity.RunWithElevatedPrivileges

+0

Yahia - 我想你可能會做些什麼......我會調查並報告回來!謝謝你的回答 - 如果這是對的,我會欠你一杯啤酒!任何其他建議仍然歡迎! – ben

1

之前,我有同樣的問題,我固定它通過改變線

OrganizationProfile parentOrg = opm.RootOrganization; 

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

GetProfile(1)在我的情況下返回RootOrganization。

希望這可以幫助別人!

相關問題