views:

92

answers:

1

Hello, 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 = StringToBinary(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;
    }
A: 

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[])

F.Aquino