获取Linq查询的字段值 - SharePoint

时间:2017-12-15 18:30:49

标签: c# linq sharepoint csom

我试图在日期范围之间限制我的结果。我使用CSOM连接到SharePoint。我认为SharePoint版本是SharePoint Online(我们有办公室365工作)。

基本上,我想限制下面查询返回的日期。我想要 Start_Date< =今天 End_Date> =今天。不仅如此,我还不太了解如何使用linq返回字段的值。

我补充说:

.Where(x => x.Id <= 50)

正常工作。这很容易,因为我可以找到具有Intellisense的Id属性,但我无法找到如何提取我感兴趣的字段,以及如何使用linq来限制我的结果一个额外的地方。

现在我在尝试按原样运行代码时出现以下错误:

The 'ToDateTime' member cannot be used in the expression.

相关代码:

    private ListItemCollection GetSharePointList(string XML, string tableName, ClientContext context, byte limit = 50)
    {
        List testTable = context.Web.Lists.GetByTitle(tableName);

        CamlQuery camlQuery = new CamlQuery
        {
            ViewXml = XML //The Passed in XML is: "<view></view>"
        };

        ListItemCollection GetSharePointList = testTable.GetItems(camlQuery);
        DateTime today = DateTime.Today;

        context.Load(GetSharePointList,
        items => items.Take(limit).Include(
                 item => item.Id,
                 item => item["DayOfMonth"],
                 item => item["Start_Date"],
                 item => item["End_Date"],
                 item => item["Task_FrequencyID_x003a_Frequency"],
                 item => item["TaskID_x003a_TaskName"],
                 item => item["TaskID_x003a_TaskAlias"],
                 item => item["TaskID_x003a_TaskDueTime"],
                 item => item["Daily_Frequency_x003a_DayofWeek"],
                 item => item["Weekly_Frequency_x003a_Week"],
                 item => item["End_Date"],
                 item => item["Monthly_Frequency"])
                        .Where(x => x.Id <= 50)
                        .Where(y => Convert.ToDateTime(y.FieldValues["Start_Date"]) <= today));

        context.ExecuteQuery();
        return GetSharePointList;
    }

1 个答案:

答案 0 :(得分:0)

要限制Start_Date&lt; = Today和End_Date&gt; = Today之间的结果,您需要使用CAML查询。这就是它在SharePoint中的工作方式。

因此,您需要修改代码,如下所示:

List testTable = context.Web.Lists.GetByTitle(tableName);

var viewQuery = string.Format(@"<View>
    <Query>
      <Where>
        <And>        
         <Leq>
            <FieldRef Name='Start_Date' />
            <Value Type='DateTime'><Today /></Value>
         </Leq>
         <Geq>
            <FieldRef Name='End_Date' />
            <Value Type='DateTime'><Today /></Value>
         </Geq>
      </And>
     </Where>
    </Query>
</View>");

CamlQuery camlQuery = new CamlQuery
{
    ViewXml = viewQuery;
};

ListItemCollection GetSharePointList = testTable.GetItems(camlQuery);

context.Load(GetSharePointList,
    items => items.Include(
    item => item.Id,
    item => item["DayOfMonth"],
    item => item["Start_Date"],
    item => item["End_Date"],
    item => item["Task_FrequencyID_x003a_Frequency"],
    item => item["TaskID_x003a_TaskName"],
    item => item["TaskID_x003a_TaskAlias"],
    item => item["TaskID_x003a_TaskDueTime"],
    item => item["Daily_Frequency_x003a_DayofWeek"],
    item => item["Weekly_Frequency_x003a_Week"],
    item => item["End_Date"],
    item => item["Monthly_Frequency"]));
context.ExecuteQuery();

foreach(ListItem spListItem in GetSharePointList){
    //do something with the data
}

参考 - Retrieve list items using CSOM C#