Using the binary type UserProperty

For some reason, I need to save some large lines in user profiles. Since a property with a type string has a limit of up to 400 characters, I decided to try a binary type (PropertyDataType.Binary), which allows me to have a length of 7500. My idea is to convert the string that I have to binary and store it in the property.

I create a 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();

, byte [] , " " System.Byte " " System.String ".". , " : []". , ?

, :

SPUser = Web.CurrentUser; ServerContext = ServerContext.GetContext(HttpContext.Current); UserProfileManager profileManager = UserProfileManager (); UserProfile = 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;
    }
+3
2

? . , ? (7500 [])

0

, . 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();
0

Source: https://habr.com/ru/post/1729391/


All Articles