Sharepoint 使用UserProperty的二进制类型
出于某种原因,我需要将一些大字符串保存到用户配置文件中。因为字符串类型的属性限制为400个字符,所以我决定尝试使用长度为7500的二进制类型(PropertyDataType.binary)。我的IDEA是将我拥有的字符串转换成二进制并保存到属性 我使用以下代码创建属性:Sharepoint 使用UserProperty的二进制类型,sharepoint,binary,properties,user-profile,Sharepoint,Binary,Properties,User Profile,出于某种原因,我需要将一些大字符串保存到用户配置文件中。因为字符串类型的属性限制为400个字符,所以我决定尝试使用长度为7500的二进制类型(PropertyDataType.binary)。我的IDEA是将我拥有的字符串转换成二进制并保存到属性 我使用以下代码创建属性: context = ServerContext.GetContext(elevatedSite); profileManager = new UserProfileManager
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[]type的值时,我收到错误“无法将类型为'System.byte'的对象强制转换为类型为'System.String'。若我试图提供一个字符串值,我会收到“无效的二进制值:输入必须匹配二进制字节[]数据类型。”
那么我的问题是如何使用这个二进制类型
我拥有的代码:
SPUser user=elevatedWeb.CurrentUser;
ServerContext=ServerContext.GetContext(HttpContext.Current);
UserProfileManager=newuserprofilemanager(上下文);
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;
}
private静态字符串GenerateBigString()
{
StringBuilder sb=新的StringBuilder();
对于(inti=0;i<750;i++)sb.Append(“0123456789”);
使某人返回字符串();
}
私有静态字节[]StringToBinary(字符串源)
{
字节[]字节=新字节[7500];
thebytes=System.Text.Encoding.ASCII.GetBytes(源代码);
返回字节;
}
您是否尝试过使用较小的字符串?在第一次测试中使用max可能会隐藏其他行为。在调试器中检查生成的字符串时,它是否符合要求?(7500字节[])对于那些正在寻找答案的人。您必须改用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();