public ActionResult Featured(int maxCount = 3) { using (var clientContext = HttpContext.GetUserClientContextForSPHost()) { ViewBag.SPHostUrl = HttpContext.Request.QueryString["SPHostUrl"]; var caml = new CamlQuery() { ViewXml = CAML.ViewQuery( CAML.Where( CAML.And( CAML.Geq( CAML.FieldValue(Event.FIELD_DATE, "Date", CAML.Today()) ), CAML.Eq( CAML.FieldValue(Event.FIELD_CATEGORY, "Text", "Featured") ) ) ), CAML.OrderBy(new OrderByField(Event.FIELD_DATE)), rowLimit: maxCount) }; var list = clientContext.Web.Lists.GetByTitle(ListDetails.EventsListName); clientContext.Load(list); clientContext.ExecuteQuery(); var items = list.GetItems(caml); clientContext.Load(items); clientContext.ExecuteQuery(); var result = items.Cast <ListItem>() .Select(i => new Event(i)).ToList(); return(View(result)); } }
public void Tag_ComplicatedQuery_DoesNotThrowXmlException() { Assert.DoesNotThrow(() => { var fieldRefId = CAML.FieldRef(BuiltInInternalFieldNames.ID); var orderByAsCaml = CAML.OrderBy( CAML.FieldRef(BuiltInInternalFieldNames.ID, CAML.SortType.Ascending) ); var viewFields = new List <string> { BuiltInInternalFieldNames.ID, BuiltInInternalFieldNames.Title }; var viewFieldsAsCaml = CAML.ViewFieldsByFieldNames(viewFields.ToArray()); const int maxRowLimit = 1000; var rowLimitAsCaml = CAML.RowLimit(maxRowLimit); const int startId = 1; const int endId = maxRowLimit; CAML.View( string.Concat( CAML.Query( string.Concat( CAML.Where( CAML.And( CAML.Geq( fieldRefId, CAML.Value(startId) ), CAML.Leq( fieldRefId, CAML.Value(endId) ) ) ), orderByAsCaml ) ), viewFieldsAsCaml, rowLimitAsCaml ) ); }); }
/// <summary> /// Process the internals of the CmdLet /// </summary> public override void ExecuteCmdlet() { base.ExecuteCmdlet(); // Load email notification list var listInSite = this.ListTitle.GetList(this.ClientContext.Web); // get site and query the list for pending requests var fieldNames = new List <string>() { "ID" }; if (ViewFields != null && ViewFields.Length > 0) { fieldNames.AddRange(ViewFields); } ; var fieldsXml = CAML.ViewFields(fieldNames.Select(s => CAML.FieldRef(s)).ToArray()); var camlWhereClause = string.Empty; var camlWhereConcat = false; if (!string.IsNullOrEmpty(this.OverrideCamlQuery)) { camlWhereConcat = true; camlWhereClause = this.OverrideCamlQuery; } if (StartsWithId > 0) { var camlWhereSubClause = CAML.Geq(CAML.FieldValue("ID", FieldType.Number.ToString("f"), StartsWithId.ToString())); if (camlWhereConcat) { // Wrap in an And Clause camlWhereClause = CAML.And(camlWhereClause, camlWhereSubClause); } else { camlWhereConcat = true; camlWhereClause = camlWhereSubClause; } } if (EndsWithId > 0) { var camlWhereSubClause = CAML.Leq(CAML.FieldValue("ID", FieldType.Number.ToString("f"), EndsWithId.ToString())); if (camlWhereConcat) { // Wrap in an And Clause camlWhereClause = CAML.And(camlWhereClause, camlWhereSubClause); } else { camlWhereConcat = true; camlWhereClause = camlWhereSubClause; } } if (string.IsNullOrEmpty(camlWhereClause)) { throw new Exception("Failed to construct a valid CAML Query."); } try { ListItemCollectionPosition ListItemCollectionPosition = null; var camlQuery = new CamlQuery() { ViewXml = CAML.ViewQuery(ViewScope.RecursiveAll, CAML.Where(camlWhereClause), string.Empty, fieldsXml, 50) }; var ids = new List <int>(); while (true) { camlQuery.ListItemCollectionPosition = ListItemCollectionPosition; var spListItems = listInSite.GetItems(camlQuery); this.ClientContext.Load(spListItems); this.ClientContext.ExecuteQueryRetry(); ListItemCollectionPosition = spListItems.ListItemCollectionPosition; foreach (var spListItem in spListItems) { var s = string.Empty; LogWarning("ListItem [{0}] will be deleted.", spListItem.Id); foreach (var fieldName in fieldNames) { s += string.Format("...[{0}]==[{1}]...", fieldName, spListItem[fieldName]); } LogVerbose("LISTITEM: {0}", s); ids.Add(spListItem.Id); } if (ListItemCollectionPosition == null) { break; } } // enumerate the ids to be deleted and process if -WhatIf (not passed) foreach (var id in ids) { if (this.ShouldProcess(string.Format("ListItem [{0}] now being deleted.", id))) { var spListItem = listInSite.GetItemById(id); spListItem.DeleteObject(); listInSite.Update(); this.ClientContext.ExecuteQueryRetry(); } } } catch (Exception ex) { LogError(ex, "Failed to query list {0} with message {1}", this.ListTitle, ex.Message); } }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); LogVerbose("Scanning CSOM callout"); var invalidLinks = new List <CalloutLinkModel>(); var fields = new string[] { "_dlc_DocId", "_dlc_DocIdUrl", "Modified", "Editor", "ServerRedirectedEmbedUri", "FileRef", "Title" }; var fieldsXml = CAML.ViewFields(fields.Select(s => CAML.FieldRef(s)).ToArray()); var onlineLibrary = List.GetList(this.ClientContext.Web); if (!EndId.HasValue) { onlineLibrary.EnsureProperties(ol => ol.ItemCount); EndId = onlineLibrary.ItemCount; } ListItemCollectionPosition itemCollectionPosition = null; CamlQuery camlQuery = new CamlQuery(); for (var idx = 1; idx <= EndId; idx += 1000) { camlQuery.ViewXml = string.Format(@"<View Scope='RecursiveAll'><Query> <OrderBy><FieldRef Name='ID' /></OrderBy> <Where> <And> <And> {0}{1} </And> <Or> {2}{3} </Or> </And> </Where> {4} <RowLimit Paged='TRUE'>30</RowLimit> </Query></View>", CAML.Geq(CAML.FieldValue("ID", FieldType.Integer.ToString("f"), idx.ToString())), CAML.Leq(CAML.FieldValue("ID", FieldType.Integer.ToString("f"), (idx + 1000).ToString())), CAML.Eq(CAML.FieldValue("FileDirRef", FieldType.Text.ToString("f"), Path)), CAML.Contains(CAML.FieldValue("_dlc_DocIdUrl", FieldType.URL.ToString("f"), PartialUrl)), fieldsXml ); while (true) { camlQuery.ListItemCollectionPosition = itemCollectionPosition; ListItemCollection listItems = onlineLibrary.GetItems(camlQuery); this.ClientContext.Load(listItems); this.ClientContext.ExecuteQuery(); itemCollectionPosition = listItems.ListItemCollectionPosition; if (listItems.Count() > 0) { foreach (var listItem in listItems) { var item = onlineLibrary.GetItemById(listItem.Id); this.ClientContext.Load(item); this.ClientContext.ExecuteQuery(); var docId = item.RetrieveListItemValue("_dlc_DocId"); var docIdUrl = item.RetrieveListItemValueAsHyperlink("_dlc_DocIdUrl"); var modified = item.RetrieveListItemValue("Modified").ToDateTime(); var editor = item.RetrieveListItemUserValue("Editor"); var redirectEmbeddedUrl = item.RetrieveListItemValue("ServerRedirectedEmbedUri"); var fileRef = item.RetrieveListItemValue("FileRef"); var title = item.RetrieveListItemValue("Title"); invalidLinks.Add(new CalloutLinkModel() { DocId = docId, DocIdUrl = (docIdUrl == null) ? string.Empty : docIdUrl.Url, Modified = modified, EditorEmail = (editor == null) ? string.Empty : editor.ToUserEmailValue(), EmbeddedUrl = redirectEmbeddedUrl, FileUrl = fileRef, Title = title, Id = listItem.Id }); } } if (itemCollectionPosition == null) { break; } } } WriteObject(invalidLinks); }