【问题标题】:Run CAML Query on DataTable SharePoint在 DataTable SharePoint 上运行 CAML 查询
【发布时间】:2015-09-11 18:11:12
【问题描述】:

我有一个包含一些行的 DataTable。有什么方法可以对其执行 CAML 查询。

或者,我如何将此 DataTable 转换为 SPList,以便 CAML 可以在 SPList 上执行。

private DataTable Process(DataTable d, string s)
    {
        DataTable dd = d;
        SPQuery query = new SPQuery();
        query.Query = s;
        query.ViewAttributes = "Scope = 'RecursiveAll'";
        DataTable dtSearch = new DataTable();
        SPList Dest = new SPList();


        dtSearch = d.GetItems(query).GetDataTable();
        return dtSearch;
    } 

在上面的代码d.GetItems 中不能完成。 ???

【问题讨论】:

    标签: sharepoint datatable caml


    【解决方案1】:

    我会尝试使用数据表支持的 api 搜索数据表。 例如下面的 msdn 文章解释了按主键搜索行和按列值搜索行。

    https://msdn.microsoft.com/en-us/library/y06xa2h1.aspx

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我在 technet 上试过这个。

      private SPList dtConvertSPList(DataTable dt)
          {
              SPList newList = null;
              using (SPSite site = new SPSite("http://sharepointvm"))
              {
                  using (SPWeb web = site.OpenWeb())
                  {
      
      
      
                          Guid newListGuid = web.Lists.Add("TempList", "This is my temp list, it will be removed when used", SPListTemplateType.GenericList);
                          newList = web.Lists[newListGuid];
      
                          if (newList != null)
                          {
                              /* Loop through the datatable and add columns to the list */
                              foreach (DataColumn dc in dt.Columns)
                              {
                                  newList.Fields.Add(dc.ColumnName, SPFieldType.Text, false);
                              }
      
                              /* Update the list to get the new fields */
                              newList.Update();
      
      
                              /* Populate the list from the datatable */
                              SPListItem newItem = null;
                              foreach (DataRow row in dt.Rows)
                              {
                                  /* Create a new item in the list */
                                  newItem = newList.Items.Add();
                                  foreach (DataColumn dc in dt.Columns)
                                  {
                                      newItem[dc.ColumnName] = row[dc].ToString();
                                      newItem.Update();
      
                                  }
      
      
                              }
                          }
                     }
              }
              return newList;
      
          }
      

      但我面临的问题是:外部for循环即

      foreach (DataRow row in dt.Rows)
                              {
                                  /* Create a new item in the list */
                                  newItem = newList.Items.Add();
                                  foreach (DataColumn dc in dt.Columns)
                                  {
                                      newItem[dc.ColumnName] = row[dc].ToString();
                                      newItem.Update();
      
                                  }
      
                              }
      

      这个外部循环意外地只执行了一次。

      *调试结果: 传递给此函数的 DataTable 有 3358 条记录。 newList 的所有列都创建得很好,但只有一行的列中的信息也不完整。

      需要改进:

      循环应该可以正常工作,我应该在哪里放置代码以在返回后删除这个 newList?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多