【问题标题】:caml query not working properly in sharepoint onlinecaml 查询在 sharepoint online 中无法正常工作
【发布时间】:2018-07-09 13:00:52
【问题描述】:

我需要获取早于 7 天的列表项并将其删除。我尝试使用 caml 查询,它在 sharepoint 2010 中运行良好,但是当我尝试在 Sharepoint Online 中使用它时,它会获取所有列表项并将其删除,而不管条件如何。

    public static bool removeOldEntries(string listName, int offset)
    {
        bool successFlag = true;
        try
        {
            using (var context = new ClientContext(siteURL))
            {
                SecureString password = ToSecureString(pwd);
                context.Credentials = new SharePointOnlineCredentials(userName, password);
                Web web = context.Web;
                var list = context.Web.Lists.GetByTitle(listName);
                if (list != null)
                {
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = "<Where><Leq><FieldRef Name='Modified'/><Value Type='DateTime'><Today OffsetDays='-" + offset + "'/></Value></Leq></Where>";
                    ListItemCollection collListItem = list.GetItems(camlQuery);
                    context.Load(collListItem, items => items.Include(
                                         item => item["ID"]));
                    context.ExecuteQuery();
                    if (collListItem.Count > 0)
                    {
                        foreach (ListItem oListItem in collListItem)
                        {
                            ListItem itemToDelete = list.GetItemById(int.Parse(oListItem["ID"].ToString()));
                            itemToDelete.DeleteObject();
                            context.ExecuteQuery();
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            successFlag = false;
        }
        return successFlag;
    }

提前感谢您的帮助。

【问题讨论】:

  • 我已经添加了一个答案,如果有帮助请告诉我

标签: c# sharepoint sharepoint-online caml


【解决方案1】:

首先尝试在您的视图 xml 中添加标签

应该是这样的

 camlQuery.ViewXml = "<Query><Where><Leq><FieldRef Name='Modified'/><Value Type='DateTime'><Today OffsetDays='-" + offset + "'/></Value></Leq></Where></Query>";

如果没有帮助,请尝试添加视图标签

 camlQuery.ViewXml = "<View><Query><Where><Leq><FieldRef Name='Modified'/><Value Type='DateTime'><Today OffsetDays='-" + offset + "'/></Value></Leq></Where></Query></View>";

【讨论】:

    【解决方案2】:

    您可以使用 PNP.PowerShell 模块。

    $CreationDate = Get-Date "16.06.2021 20:04" -Format s 
    
    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></Eq></Where></Query></View>"
    

    欲了解更多信息,请查看参考: https://sposcripts.com/sharepoint/sharepointonline/filtering-for-sharepoint-items-with-caml-queries/#If_DateTime_should_exactly_match_a_specific_date

    【讨论】:

      猜你喜欢
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多