接著上一篇:Query Options的一些用法(1): 展開使用者列資訊。
這次增加了兩條新的列表記錄。第二條沒有附件,第三條有2個附件,並且給第一條記錄增加了一個附件。
在View fields增加附件的引用。這樣可以顯示附件資訊。
static XmlNode ViewFields
{
get
{
XmlElement viewFields = _camlDoc.CreateElement("ViewFields");
System.Text.StringBuilder viewFieldsSB = new System.Text.StringBuilder();
#region View Fields
viewFieldsSB.Append(" <FieldRef Name=\"Title\" />");
viewFieldsSB.Append(" <FieldRef Name=\"Attachments\" />");
viewFieldsSB.Append(" <FieldRef Name=\"Author\" />");
viewFieldsSB.Append(" <FieldRef Name=\"Editor\" />");
#endregion
string sResult = viewFieldsSB.ToString();
viewFields.InnerXml = viewFieldsSB.ToString();
return viewFields;
}
}
運行一個不包含任何Query Options的查詢。
得到如下結果,包含附件的記錄顯示1,不包含顯示0,即使包含多個附件,也只顯示1,這個值更像表示當前的記錄是否包含附件。
<rs:data ItemCount="3" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="Test01" ows_Attachments="1" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_UniqueId="1;#{E1F2046C-16AC-4650-8EE3-73E12ED5E833}" ows_owshiddenversion="2" ows_FSObjType="1;#0" ows_Created="2012-11-30 01:37:17" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 02:43:10" ows_FileRef="1;#teams/smetechcom/Lists/QueryOptionsDemo/1_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test02" ows_Attachments="0" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="2;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="2" ows_UniqueId="2;#{DCDE833B-E69A-4C63-B9FE-0AE5938B3CDE}" ows_owshiddenversion="1" ows_FSObjType="2;#0" ows_Created="2012-11-30 02:48:12" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 02:48:12" ows_FileRef="2;#teams/smetechcom/Lists/QueryOptionsDemo/2_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test03" ows_Attachments="1" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="3;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="3" ows_UniqueId="3;#{74218240-4455-4FCA-AE83-5B966DF6F484}" ows_owshiddenversion="2" ows_FSObjType="3;#0" ows_Created="2012-11-30 03:04:00" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 03:04:45" ows_FileRef="3;#teams/smetechcom/Lists/QueryOptionsDemo/3_.000" xmlns:z="#RowsetSchema" />
</rs:data>
如果需要知道附件的URL呢?
一種方式是,使用Lists.GetAttachmentCollection方法,通過傳入列表名和列表記錄ID來查詢。
另外也就是可以指定Query Options:IncludeAttachmentUrls來進行查詢了。
修改Query Options包含附件。
static XmlNode QueryOptions
{
get
{
XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");
queryOptions.InnerXml = "<IncludeAttachmentUrls>True</IncludeAttachmentUrls>";
return queryOptions;
}
}
運行一下,可以發現,如果沒有附件,結果仍然是0。如果有附件,附件完整的URL就會顯示出來了。首尾都有一個;#,多個附件以分隔;#。
<rs:data ItemCount="3" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="Test01" ows_Attachments=";#http://ent191.sharepoint.hp.com/teams/smetechcom/Lists/QueryOptionsDemo/Attachments/1/test.xlsx;#" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_UniqueId="1;#{E1F2046C-16AC-4650-8EE3-73E12ED5E833}" ows_owshiddenversion="2" ows_FSObjType="1;#0" ows_Created="2012-11-30 01:37:17" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 02:43:10" ows_FileRef="1;#teams/smetechcom/Lists/QueryOptionsDemo/1_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test02" ows_Attachments="0" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="2;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="2" ows_UniqueId="2;#{DCDE833B-E69A-4C63-B9FE-0AE5938B3CDE}" ows_owshiddenversion="1" ows_FSObjType="2;#0" ows_Created="2012-11-30 02:48:12" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 02:48:12" ows_FileRef="2;#teams/smetechcom/Lists/QueryOptionsDemo/2_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test03" ows_Attachments=";#http://ent191.sharepoint.hp.com/teams/smetechcom/Lists/QueryOptionsDemo/Attachments/3/test.xlsx;#http://ent191.sharepoint.hp.com/teams/smetechcom/Lists/QueryOptionsDemo/Attachments/3/test1.xlsx;#" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="3;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="3" ows_UniqueId="3;#{74218240-4455-4FCA-AE83-5B966DF6F484}" ows_owshiddenversion="2" ows_FSObjType="3;#0" ows_Created="2012-11-30 03:04:00" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 03:04:45" ows_FileRef="3;#teams/smetechcom/Lists/QueryOptionsDemo/3_.000" xmlns:z="#RowsetSchema" />
</rs:data>