Sample Code to Load data from SharePoint List Using CamlQuery
=========================================================
The different Query Options need to be handled a bit differently than with the SharePoint Server Object Model.
The row limit can also be specified within the ViewXml property:
query.ViewXml = "<View>"
+ "<Query>"
+ "<Where><Eq><FieldRef Name='Country' /><Value Type='Text'>Belgium</Value></Eq></Where>"
+ "<OrderBy><FieldRef Name='City'/></OrderBy>"
+ "</Query>"
+ "<RowLimit>5</RowLimit>"
+ "</View>";
-------------------------------------------------
Dates in UTC
+ "<Query>"
+ "<Where><Eq><FieldRef Name='Country' /><Value Type='Text'>Belgium</Value></Eq></Where>"
+ "<OrderBy><FieldRef Name='City'/></OrderBy>"
+ "</Query>"
+ "<RowLimit>5</RowLimit>"
+ "</View>";
-------------------------------------------------
Dates in UTC
You can choose to return dates in UTC (Coordinated Universal Time) by setting the DatesInUtc property of the CamlQuery instance:
query.DatesInUtc = true;
--------------------------------------------
Include attachment URLs
--------------------------------------------
Include attachment URLs
Using CAML you are able to know if list items have attachment by adding a w> element to the ViewFields element in the ViewXml property:
query.ViewXml = "<View>"
+ "<ViewFields>"
+ " <FieldRef Name='Title' /><FieldRef Name='City' /><FieldRef Name='Attachments' />"
+ "</ViewFields>"
+ "</View>";
SharePoint will return a boolean indicating whether the list item has attachments or not.
+ "<ViewFields>"
+ " <FieldRef Name='Title' /><FieldRef Name='City' /><FieldRef Name='Attachments' />"
+ "</ViewFields>"
+ "</View>";
SharePoint will return a boolean indicating whether the list item has attachments or not.
--------------------------------------------------------
For example, if you want to query all files and folders in your document library, no matter how deep they are nested, you have to add a Scope attribute to the View element, and set its value to RecursiveAll:
query.ViewXml = "<View Scope='RecursiveAll'></View>";
----------------------------------------------------------------
For example, if you want to query all files and folders in your document library, no matter how deep they are nested, you have to add a Scope attribute to the View element, and set its value to RecursiveAll:
query.ViewXml = "<View Scope='RecursiveAll'></View>";
----------------------------------------------------------------
--------------------------------------------------------------------------------
If you need to retrieving the attachments itself you will have to write some extra code that retrieves the files from the Attachment folder:
If you need to retrieving the attachments itself you will have to write some extra code that retrieves the files from the Attachment folder:
Folder folder = clientContext.Web.GetFolderByServerRelativeUrl(
spList.RootFolder.ServerRelativeUrl + "/Attachments/" + item.Id);
FileCollection files = attFolder.Files;
// If you only need the URLs
ctx.Load(files, fs => fs.Include(f => f.ServerRelativeUrl));
ctx.ExecuteQuery();
---------------------------
You can always add a Query element in the View element and specify a Where clause to add an extra filter to the query, or an OrderBy clause to sort the result.
spList.RootFolder.ServerRelativeUrl + "/Attachments/" + item.Id);
FileCollection files = attFolder.Files;
// If you only need the URLs
ctx.Load(files, fs => fs.Include(f => f.ServerRelativeUrl));
ctx.ExecuteQuery();
---------------------------
You can always add a Query element in the View element and specify a Where clause to add an extra filter to the query, or an OrderBy clause to sort the result.
If you want to query only the folders, you have to add an extra where clause:
query.ViewXml = "<View Scope='RecursiveAll'>"
+ "<Query>"
+ " <Where>"
+ " <Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value></Eq>"
+ " </Where>"
+ "</Query>"
+ "</View>";
---------------------------------------
If you want to query only the files, the extra where clause can be changed as follows:
+ "<Query>"
+ " <Where>"
+ " <Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value></Eq>"
+ " </Where>"
+ "</Query>"
+ "</View>";
---------------------------------------
If you want to query only the files, the extra where clause can be changed as follows:
query.ViewXml = "<View Scope='RecursiveAll'>"
+ "<Query>"
+ " <Where>"
+ " <Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>"
+ " </Where>"
+ "</Query>"
+ "</View>";
-----------------------------------------------------------
If you want to retrieve the content of a specific folder, i.e files and folders, you have to add the relative URL to that folder to the FolderServerRelativeUrl property of the query instance:
+ "<Query>"
+ " <Where>"
+ " <Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>"
+ " </Where>"
+ "</Query>"
+ "</View>";
-----------------------------------------------------------
If you want to retrieve the content of a specific folder, i.e files and folders, you have to add the relative URL to that folder to the FolderServerRelativeUrl property of the query instance:
query.FolderServerRelativeUrl = "/Shared Documents/Folder 1";
----------------------------------------------------------
If you only want to see the files of a specific sub folder, you have to set the Scope attribute of the ViewXml property to FilesOnly:
----------------------------------------------------------
If you only want to see the files of a specific sub folder, you have to set the Scope attribute of the ViewXml property to FilesOnly:
query.ViewXml = "<View Scope='FilesOnly' />";
query.FolderServerRelativeUrl = "/Shared Documents/Folder 1";
--------------------------------------------------------------------
Of course you can also query all files in a specific sub folder and its underlying sub folders. In that case you also have to specify the relative URL to the folder, but you also have to set the Scope attribute of the ViewXml property to Recursive:
query.FolderServerRelativeUrl = "/Shared Documents/Folder 1";
--------------------------------------------------------------------
Of course you can also query all files in a specific sub folder and its underlying sub folders. In that case you also have to specify the relative URL to the folder, but you also have to set the Scope attribute of the ViewXml property to Recursive:
query.ViewXml = "<View Scope='Recursive' />";
query.FolderServerRelativeUrl = "/Shared Documents/Folder 1";
--------------------------------------------------------------------
If you want to retrieve all files AND folders from a specific folder and its underlying sub folders, you have to set the Scope attribute of the ViewXml property to RecursiveAll:
query.FolderServerRelativeUrl = "/Shared Documents/Folder 1";
--------------------------------------------------------------------
If you want to retrieve all files AND folders from a specific folder and its underlying sub folders, you have to set the Scope attribute of the ViewXml property to RecursiveAll:
query.ViewXml = "<View Scope='RecursiveAll' />";
query.FolderServerRelativeUrl = "/Shared Documents/Folder 1";
query.FolderServerRelativeUrl = "/Shared Documents/Folder 1";