SharePoint2010客户端模型获取中文字段的编码方式
<div id="cnblogs_post_body">列表记录如下:http://images.cnblogs.com/cnblogs_com/love007/201205/201205301435469052.png
在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); 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; }效果图如下:
http://images.cnblogs.com/cnblogs_com/love007/201205/201205301435472890.png
页:
[1]