Sharepoint 2010 - 如何修改分类属性?

时间:2010-08-12 23:44:10

标签: .net sharepoint sharepoint-2010

我想修改Sharepoint 2010中的分类用户配置文件属性。举个例子,假设我想修改Interests属性(又名SPS-Interests或PropertyConstants.Interests)。我尝试使用分号分隔的字符串设置属性值,如下所示:

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

但是这引发了异常。

5 个答案:

答案 0 :(得分: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方法以将更改保存到数据库。

答案 1 :(得分:1)

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

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

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

虽然有限制/优点: 这样就不会省略重复。如果您因此使用Add方法“Value1”,“Value2”,“Value1”。保存的结果是“Value1; Value2”。 使用新的string []方法,它将保存所有这三个(我真的需要这种行为)。

答案 2 :(得分: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中启用了“允许用户编辑此属性的值”&gt; - >用户档案服务 - &gt;用户个人资料属性 - &gt;修改设置

答案 3 :(得分:0)

这不是你问题的完整答案,但是当我在寻找类似的东西并发现如何做到这一点时,我认为对其他人来说也许有趣:

UserProfileValueCollection类在SharePoint 2010中具有各种新的特定于分类法的方法,值得研究:

  • AddTaxonomyTerm
  • GetTaxonomyTerms
  • RemoveTaxonomyTerm

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

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

答案 4 :(得分:-4)

试试这个:

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

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

userprofile.Commit();
相关问题