六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 165|回复: 0

SharePoint2010客户端模型获取中文字段的编码方式

[复制链接]

升级  22%

198

主题

198

主题

198

主题

进士

Rank: 4

积分
610
 楼主| 发表于 2012-12-12 00:09:04 | 显示全部楼层 |阅读模式
<div id="cnblogs_post_body">列表记录如下:

在SharePoint2010客户端模型如果要得到中文字段的
错误写法
  
//打开站点 ClientContext clientContext = new ClientContext("http://moss:8001"); //获取列表 List list = clientContext.Web.Lists.GetByTitle("统计表"); //查看内容 CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View/>"; //得到记录 ListItemCollection listItems = list.GetItems(camlQuery); //列表 clientContext.Load(list); //记录 clientContext.Load(listItems); //执行 clientContext.ExecuteQuery(); //得到记录 foreach (ListItem listItem in listItems) {      //            Console.WriteLine("Id: {0} Title: {1} 加班天数:{2}", listItem.Id, listItem["Title"], listItem["加班(天)"]);      Console.WriteLine();}这样写的结果,运行会出现错误,无法得到要的结果。 正确写法://打开站点ClientContext clientContext = new ClientContext("http://moss:8001");//获取列表List list = clientContext.Web.Lists.GetByTitle("统计表");//查看内容CamlQuery camlQuery = new CamlQuery();camlQuery.ViewXml = "<View/>";//得到记录ListItemCollection listItems = list.GetItems(camlQuery);//得到某个字段Field flds = list.Fields.GetByTitle("加班(天)");//列表clientContext.Load(list);//记录clientContext.Load(listItems);//字段clientContext.Load(flds);//执行clientContext.ExecuteQuery();//得到记录foreach (ListItem listItem in listItems){     //*****************汉字的字段需要通过编码后方可得到值flds.InternalName                     Console.WriteLine("Id: {0} Title: {1} 加班天数:{2}", listItem.Id, listItem["Title"], listItem[flds.InternalName]);     Console.WriteLine();}.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }效果图如下:
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表