SharePoint 列表项过滤器 (GetListItems)

2 投票
1 回答
3361 浏览
提问于 2025-04-16 02:34

我正在尝试通过WebService从SharePoint获取一组列表项。我想查询一小部分项来返回。我的SOAP数据包看起来排列得很正确,但似乎服务还是忽略了我的过滤条件(查询)。你们觉得这可能是什么原因呢?

<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
  <ns1:GetListItems>
     <ns1:listName>MyCalendar</ns1:listName>
     <query>
        <Query>
           <Where>
              <Eq>
                 <FieldRef Name="EventDate"/>
                 <Value Type="DateTime">[Now+2Minute(s)]</Value>
              </Eq>
           </Where>
        </Query>
     </query>
  </ns1:GetListItems>
</ns0:Body>
</SOAP-ENV:Envelope>

这是我用来生成这个SOAP的Python suds代码:

Query = Element('Query')
where = Element('Where')
eq = Element('Eq')
eq.append(Element('FieldRef').append(Attribute('Name', 'EventDate')))
vt = Element('Value').append(Attribute('Type', 'DateTime')).setText('[Now+2Minute(s)]')
eq.append(vt)
where.append(eq)
Query.append(where)

query = Element('query')
query.append(Query)

编辑:

这里是我最终成功的SOAP数据包和suds代码。我在过滤条件上有一些奇怪的要求,但我还是把它发出来,以便其他人可以从中学习。

<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
  <ns1:GetListItems>
     <ns1:listName>Economic Event Calendar</ns1:listName>
     <ns1:query>
        <Query>
           <Where>
              <And>
                 <Geq>
                    <FieldRef Name="EventDate"/>
                    <Value IncludeTimeValue="TRUE" Type="DateTime">2010-08-12T07:38:00</Value>
                 </Geq>
                 <Lt>
                    <FieldRef Name="EventDate"/>
                    <Value IncludeTimeValue="TRUE" Type="DateTime">2010-08-12T07:39:00</Value>
                 </Lt>
              </And>
           </Where>
        </Query>
     </ns1:query>
     <ns1:rowLimit>5</ns1:rowLimit>
     <viewFields>
        <FieldRef Name="Description"/>
        <FieldRef Name="EventDate"/>
     </viewFields>
  </ns1:GetListItems>
</ns0:Body>
</SOAP-ENV:Envelope>

这是让我成功的Python/suds代码:

#craft our XML
Query = Element('Query')
where = Element('Where')
And = Element('And')

geq = Element('Geq')
geq.append(Element('FieldRef').append(Attribute('Name', 'EventDate')))
vt = Element('Value').append(Attribute('IncludeTimeValue', 'TRUE'))
vt.append(Attribute('Type', 'DateTime')).setText(convert_dt('now +' + str(alertBefore) + ' minutes', '%Y-%m-%dT%H:%M:00' ))

lt = Element('Lt')
lt.append(Element('FieldRef').append(Attribute('Name', 'EventDate')))
vt2 = Element('Value').append(Attribute('IncludeTimeValue', 'TRUE'))
vt2.append(Attribute('Type', 'DateTime')).setText(convert_dt('now +' + str((alertBefore + 1))  + ' minutes', '%Y-%m-%dT%H:%M:00' ))

#viewFields fragment, only show the Description and EventDate for returned rows
viewFields = Element('viewFields')
viewFields.append(Element('FieldRef').append(Attribute('Name','Description')))
viewFields.append(Element('FieldRef').append(Attribute('Name','EventDate')))

#pack all the XML fragments
geq.append(vt)
lt.append(vt2)
where.append(And)
And.append(geq)
And.append(lt)
Query.append(where)
query = Element('ns1:query')
query.append(Query)

#issue the query
results = c_lists.service.GetListItems(SPCal, None, query, None, 5, viewFields, None)

1 个回答

1

试试在你的 Value 元素上使用 IncludeTimeValue 属性:

<Value Type="DateTime" IncludeTimeValue="TRUE">[Now+2Minute(s)]</Value>

根据 MSDN 的说明:

IncludeTimeValue:这是一个可选的布尔值。它的作用是让你在构建日期时间查询时,不仅考虑日期,还要考虑时间。如果你不设置这个属性,那么在涉及日期和时间的查询中,时间部分会被忽略。

我在我的 CAML 查询中没有使用很多日期时间过滤器,但你其他的 SOAP 数据包看起来是正确的。

撰写回答