如何将Sharepoint文档库字段导入GridView

时间:2013-07-01 07:32:05

标签: winforms c#-4.0 sharepoint-2010

我在Windows应用程序中有网格视图。现在我想将共享文档字段值显示在网格视图中。任何人都可以有解决方案????

        IEnumerable<Sp.ListItem> list;
        Sp.ClientContext spcontext = new ClientContext("http://Sharepointsite");
        Sp.Web spsite = spcontext.Web;
        Sp.ListCollection lcollection = spsite.Lists;
        var productlist = spcontext.Web.Lists.GetByTitle("Shared Documents/Photo");
        Sp.CamlQuery cm = new CamlQuery();
        IQueryable<Sp.ListItem> mylist = productlist.GetItems(cm);
        list = spcontext.LoadQuery(mylist);
        spcontext.ExecuteQuery();
        var qry = (from prd in list
                   select new
                   {

                       Name = prd.FieldValues.Values.ElementAt(1).ToString(),
                       Custom = prd.FieldValues.Values.ElementAt(2).ToString(),

                   }).ToList();

        dataGridView1.DataSource = qry;

错误:在Sharepointsite中具有网址的网站上不存在“共享文档/照片”列表

2 个答案:

答案 0 :(得分:0)

假设Photo是文档库中的文件夹,则无法将其视为列表标题。

如果您将空Caml查询更改为

var productlist = spcontext.Web.Lists.GetByTitle("Shared Documents");

var folderName = "Photo";


Sp.CamlQuery cm = new Sp.CamlQuery();
cm.ViewXml = "<View Scope=\"RecursiveAll\"> " +
"<Query>" +
   "<Where>" +
   "<And>" +
     "<Eq>" +
       "<FieldRef Name=\"FSObjType\" />" +
       "<Value Type=\"Integer\">1</Value>" +
     "</Eq>" +
     "<Eq>" +
       "<FieldRef Name=\"Title\"/>" +
       "<Value Type=\"Text\">" + folderName + "</Value>" +
     "</Eq>" +
   "</And>" +
   "</Where>" +
 "</Query>" +
 "</View>";

它将获取文档库中具有该名称的所有文件夹。

现在,如果您需要FieldValue的List,可以将LinQ查询更改为以下内容:

 var qry = (from prd in list.ElementAt(0).FieldValues
           select new 
           {
               Key = prd.Key,
               Value = prd.Value
           }).ToList();

现在你有一个包含所有字段名称及其值的集合。什么都可以分类/过滤。

修改

如果您只想选择集合中的某些字段,则有机会在您的linq查询中添加where子句:

var qry = (from prd in list.ElementAt(0).FieldValues
           where prd.Key == "Title"
           select new 
           {
               Key = prd.Key,
               Value = prd.Value
           }).ToList();

答案 1 :(得分:-1)

错误本身暗示此行存在问题  var productlist = spcontext.Web.Lists.GetByTitle(“Shared Documents / Photo”);