caml 查询在在线共享点中无法正常工作

caml query not working properly in sharepoint online

我需要获取早于 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;
    }

在此先感谢您的帮助。

首先尝试在您的视图中添加标签 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>";

您可以使用 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