开发者

Using binary type of a UserProperty

开发者 https://www.devze.com 2022-12-17 06:32 出处:网络
For some reason I need to save some big strings into user profiles. Because a property with type string has a limit to 400 caracters I decited to try with binary type (PropertyDataType.Binary) that al

For some reason I need to save some big strings into user profiles. Because a property with type string has a limit to 400 caracters I decited to try with binary type (PropertyDataType.Binary) that allow a length of 7500. My ideea is to convert the string that I have into binary and save to property.

I create the property using the code :

            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();

The problem is that when I try to provide the value of byte[] type to the property I receive the error "Unable to cast object of type 'System.Byte' to type 'System.String'.". If I try to provide a string value I receive "Invalid Binary Value: Input must match binary byte[] data type." Then my question is how to use this binary type ?

The code that I have :

SPUser user = elevatedWeb.CurrentUser; ServerContext context = ServerContext.GetContext(HttpContext.Current); UserProfileManager profileManager = new UserProfileManager(context); UserProfile profile = GetUserProfile(elevatedSite, currentUserLoginName); UserProfileValueCollection myProperty= profile[PropertyName];

myProperty.Value = StringTo开发者_JAVA技巧Binary(GenerateBigString());

and the functions for test :

    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;
    }


Have you tried with smaller strings? Going max on the first test might hide other behaviors. When you inspect the generated string in the debugger, it fits the requirements? (7500 byte[])


For those, who are looking for answer. You must use Add method instead:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号