【问题标题】:Using binary type of a UserProperty使用 UserProperty 的二进制类型
【发布时间】:2011-01-07 17:48:16
【问题描述】:

出于某种原因,我需要将一些大字符串保存到用户配置文件中。因为字符串类型的属性限制为 400 个字符,所以我决定尝试使用允许长度为 7500 的二进制类型 (PropertyDataType.Binary)。我的想法是将我拥有的字符串转换为二进制并保存到属性。

我使用代码创建属性:

            context = ServerContext.GetContext(elevatedSite);
            profileManager = new UserProfileManager(context);
            profile = profileManager.GetUserProfile(userLoginName);
            Property newProperty = profileManager.Properties.Create(false);
            newProperty.Name = "aaa";
            newProperty.DisplayName = "aaa";

            newProperty.Type = PropertyDataType.Binary;
            newProperty.Length = 7500;                

            newProperty.PrivacyPolicy = PrivacyPolicy.OptIn;
            newProperty.DefaultPrivacy = Privacy.Organization;
            profileManager.Properties.Add(newProperty);
            myProperty = profile["aaa"];
            profile.Commit();

问题是,当我尝试将 byte[] 类型的值提供给属性时,我收到错误“无法将 'System.Byte' 类型的对象转换为 'System.String' 类型。”。如果我尝试提供一个字符串值,我会收到“无效的二进制值:输入必须匹配二进制字节 [] 数据类型”。 那么我的问题是如何使用这种二进制类型?

我的代码:

SPUser 用户 = 提升的Web.CurrentUser; ServerContext 上下文 = ServerContext.GetContext(HttpContext.Current); UserProfileManager profileManager = new UserProfileManager(context); UserProfile profile = GetUserProfile(elevatedSite, currentUserLoginName); UserProfileValueCollection myProperty= profile[PropertyName];

myProperty.Value = StringToBinary(GenerateBigString());

以及测试功能:

    private static string GenerateBigString()
    {
        StringBuilder sb = new StringBuilder();            
        for (int i = 0; i < 750; i++) sb.Append("0123456789");
        return sb.ToString();
    }

    private static byte[] StringToBinary(string theSource)
    {
        byte[] thebytes = new byte[7500];  
        thebytes = System.Text.Encoding.ASCII.GetBytes(theSource);
        return thebytes;
    }

【问题讨论】:

    标签: sharepoint binary properties user-profile


    【解决方案1】:

    您是否尝试过使用较小的字符串?在第一次测试中达到最大值可能会隐藏其他行为。当您在调试器中检查生成的字符串时,它是否符合要求? (7500 字节[])

    【讨论】:

      【解决方案2】:

      对于那些正在寻找答案的人。您必须改用Add 方法:

      var context = ServerContext.GetContext(elevatedSite);
      var profileManager = new UserProfileManager(context);
      var profile = profileManager.GetUserProfile(userLoginName);
      profile["MyPropertyName"].Add(StringToBinary("your cool string"));
      profile.Commit();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        • 1970-01-01
        • 2014-11-01
        相关资源
        最近更新 更多