2017-05-15 37 views
0

我访问这样的SharePoint列表:SharePoint列表=>数据表与标题名字

#region Accessing share point 
     Microsoft.SharePoint.Client.ClientContext cC = new Microsoft.SharePoint.Client.ClientContext(Link); 
     cC.Credentials = System.Net.CredentialCache.DefaultCredentials; 
     Microsoft.SharePoint.Client.List SPList = cC.Web.Lists.GetByTitle(ListName); 
     cC.Load(SPList.Fields); 
     Microsoft.SharePoint.Client.FieldCollection Fields = SPList.Fields; 
     #endregion 


循环中的字段,我有所有不同领域的名称信息。我是这样做的:

  int i = 0; 
     foreach(Microsoft.SharePoint.Client.Field FL in Fields) 
     { 
      WriteLine("Index: " + i++ + "; Internal name: " + FL.InternalName + "; Static name: " + FL.StaticName + "; Title: " + FL.Title); 
     } 


出口是这样的:

指数5;内部名称:Lieferant_x0020__x0028_Nummer_x0;静态名称:Lieferant_x0020__x0028_Nummer_x0;标题:Lieferant(Nummer)

正如您所看到的,sharepoint通过使用unicode为其他字符存储内部名称。只有在“FL.Title”中,名称才会存储,因为用户可以在SharePoint列表中看到它。
现在,我想把这些数据放在一个DataTable中并对其进行过滤。我能做到这一点是这样的:

 Microsoft.SharePoint.Client.CamlQuery cQuery = new Microsoft.SharePoint.Client.CamlQuery(); 
     cQuery = Microsoft.SharePoint.Client.CamlQuery.CreateAllItemsQuery(); 
     Microsoft.SharePoint.Client.ListItemCollection LIC = SPList.GetItems(cQuery); 
     cC.Load(LIC); 
     cC.ExecuteQuery(); 
     dt = new DataTable(); 

     #region Create columns 
     foreach (var Field in LIC[0].FieldValues) 
     { 
      dt.Columns.Add(Field.Key); 
     } 
     #endregion 

     #region Reading data 
     foreach (Microsoft.SharePoint.Client.ListItem It in LIC) 
     { 
      System.Data.DataRow row = dt.NewRow(); 

      foreach(var Field in It.FieldValues) 
      { 
       row[Field.Key] = Field.Value; 
      } 
      dt.Rows.Add(row); 
     } 
     #endregion 

的问题是,在ListItemCollection,只能用其他字符为Unicode的INTERNALNAME可用。我也无法编码它,因为SharePoint 2010只有32个字符的内部名称导致剪辑名称不再完整。

是否有任何可能性从0123列表中的列表中获取标题ListItem.FieldValuesKeyValuePair?实际上,KeyValuePairs都建立这样的:

<br> 
It.FieldValues.Key = //Column as Internal Name 
It.FieldValues.Value = //Value 

我需要:

It.FieldValues.Key = //Column as Title 
It.FieldValues.Value = //Value 

谢谢你在前进, 扬

回答

0

与以下这种方式,您将得到现场替换代码行集合,在那里您可以通过内部名称,静态名称和显示名称来获取字段。

Microsoft.SharePoint.Client.ListItemCollection LIC = SPList.GetItems(cQuery).Fields; 
+0

不,这会导致错误。 (cQuery)之后不允许使用任何方法。 – CJens

0

好的,我找到了解决加载SharePoint列表到DataTable具有field.Title用作列标题。

请找到代码:

 private System.Data.DataTable GetDataFromSharePointWithFilter(System.Uri Link, string ListName) 
    { 
     #region GetDataFromSharePoint 
     string connectionString = GenerateConnectionString(Link, ListName); 
     System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString); 
     conn.Open(); 

     string strSQL = "SELECT * FROM LIST"; 
     System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(strSQL, conn); 
     DataSet ds = new DataSet(); 

     ds.Locale = System.Globalization.CultureInfo.InstalledUICulture; 
     System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(cmd); 
     da.Fill(ds); 
     #endregion 

     #region Transfer data to DataTable 
     return ds.Tables[0]; 
     #endregion 
     } 

如果我循环中的所有列,他们的标题是正确的。但列表的内容没有正确存储。在Listcolumn上,像000,001,008或0028这样的值被存储在文本格式中。下面的代码读取例如008为45.在另一列中,例如Schneider,Markus和我的程序返回24输入名称。

有没有人有想法,为什么?

编辑:发现错误,你看不到。在连接字符串中,我有选项RetrieveIds =是;。这是我的代码错误地重复内容的原因。