2010-08-12 34 views
1

我想修改Sharepoint 2010中的分类用户配置文件属性。举个例子,假设我想修改Interests属性(又名SPS-Interests或PropertyConstants.Interests)。我试着用分号分隔的字符串设置属性值,就像这样:Sharepoint 2010 - 如何修改分类学属性?

var property = profile[PropertyConstants.Interests]; 
if (property != null) 
{ 
    property.Value = "sharepoint; stackoverflow; scotch";      
} 

但抛出异常。

回答

2

我不知道SharePoint 2010,但在2007年,profile [propertyName]是一个UserProfileValueCollection,因此它可以包含多个值。

这可能工作:

profile[PropertyConstants.Interests].Clear(); 
profile[PropertyConstants.Interests].Add("sharepoint"); 
profile[PropertyConstants.Interests].Add("stackoverflow"); 
profile[PropertyConstants.Interests].Add("scotch"); 

在2007年,你还需要呼吁USERPROFILE Commit方法将更改保存到数据库。

+0

卫生署!这是一个很好的额头sla子手。谢谢,汤姆。 – 2010-08-13 15:18:35

1

不知道是否是相关的了,但这里是一个快速和肮脏的方法:

property.Value = new string[] {"sharepoint; stackoverflow; scotch"}; 

就像一个魅力!清除以前的值并替换您的字符串数组。

虽然有限制/优势: 这种方式不会忽略重复。如果您因此将“添加”方法用于“值1”,“值2”和“值1”。保存的结果是“Value1; Value2”。 随着新的字符串[]方法,它将保存所有三个(我真的需要这种行为)。

0

这是不是一个完整的回答你的问题,但我一直在寻找类似的东西,并发现了如何做到这一点,我想这可能是为其他人也很有趣:

的UserProfileValueCollection类有各种新的特定分类的方法在SharePoint 2010是值得探讨:

  • AddTaxonomyTerm
  • GetTaxonomyTerms
  • RemoveTaxonomyTerm

http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofilevaluecollection_methods.aspx

我想这是“正确”的方式来访问分类为基础的用户配置文件属性。

1
internal void updateUserProfileExperienceTags(string psLabelGuidValuePairs, string psDomainUser) 
    { 
     try 
     { 
      System.Security.PermissionSet ps = new System.Security.PermissionSet 
      (
       System.Security.Permissions.PermissionState.Unrestricted 
      ); 
      ps.Assert(); 
      SPSecurity.RunWithElevatedPrivileges(delegate() 
      { 
       SPServiceContext serviceContext = SPServiceContext.Current; 
       UserProfileManager upm = new UserProfileManager(serviceContext); 
       ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties; 
       UserProfile profile = upm.GetUserProfile(psDomainUser); 
       TaxonomyFieldValueCollection _TaxonomyFieldValueCollection = new TaxonomyFieldValueCollection(String.Empty); 
       _TaxonomyFieldValueCollection.PopulateFromLabelGuidPairs(psLabelGuidValuePairs); 
       TaxonomySession session = new TaxonomySession(SPContext.Current.Site); 
       TermStore termStore = session.TermStores["Managed Metadata Service"]; 
       TermSetCollection termSets = termStore.GetTermSets("Technology", 1033); 
       TermSet sTerms = termSets[0]; 
       profile["Skills-Experience"].Clear(); 
       for (int ni = 0; ni < _TaxonomyFieldValueCollection.Count; ni++) 
       { 
        Guid guid = new Guid(_TaxonomyFieldValueCollection[ni].TermGuid); 
        Term sTerm = sTerms.GetTerm(guid); 
        profile["Skills-Experience"].AddTaxonomyTerm(sTerm); 
       } 
       profile.Commit(); 
      }); 
     } 
     catch (Exception ex) 
     { 
      this.Controls.Add(new Literal() { Text = "updateUserProfileExperienceTags: " + ex.ToString() }); 
     } 
     finally 
     { 
      System.Security.CodeAccessPermission.RevertAssert(); 
     } 
    } 

这对我很好。确保用户“允许用户为这个属性编辑值”启用>在CA - >用户配置文件服务 - >用户配置文件属性 - >编辑设置

+1

当我到达'profile [“Skills-Experience”]行时,AddTaxonomyTerm(sTerm);'在我的代码中出现错误:'索引超出范围。必须是非负值,并且小于集合的大小。“我的房产是单一价值。我该怎么办? – 2014-02-04 05:52:43

+0

@MohemmadK我有同样的例外!你解决了吗? – IDeveloper 2015-05-15 11:46:37

+1

首先你根据我的代码使用语句来设置属性值:'profile [“Skills-Experience”]。Value = sTerm。标签;'然后将语句放在'profile [“技能 - 体验”]下方。AddTaxonomyTerm(sTerm);'。让我知道它是否有效。 @IDeveloper – 2015-05-15 11:48:08

-4

试试这个:

userprofile["propertyname"].Add(" "); 

userprofile["propertyname"].AddTaxonomyTerm(taxonomyterm); 

userprofile.Commit();