public void ProcessListItem(IQueryable <TEntity> query, ContentIterator.ItemProcessor itemProcessor) { var caml = GetSPQuery(query); var iterator = new ContentIterator(); iterator.ProcessListItems(SPList, caml, itemProcessor, (e, i) => true); }
static void ExportTemplate(SPList list) { Log("Writing AutomatedTagging.csv..."); Dictionary <SPContentTypeId, List <Guid> > taxonomyFieldsByContentTypeId = new Dictionary <SPContentTypeId, List <Guid> >(); using (StreamWriter streamWriter = new StreamWriter("AutomatedTagging.csv")) { streamWriter.WriteLine("Filename,,Field,Tags,Field,Tags,..."); ContentIterator contentIterator = new ContentIterator(); contentIterator.ProcessListItems(list, delegate(SPListItem listItem) { List <Guid> fieldGuids; if (!taxonomyFieldsByContentTypeId.TryGetValue(listItem.ContentTypeId, out fieldGuids)) { fieldGuids = new List <Guid>(); foreach (SPField field in listItem.ContentType.Fields) { // Alternatively, the code could also support taxonomy fields that can take multiple values here. if (field.TypeAsString == "TaxonomyFieldType") { fieldGuids.Add(field.Id); } } taxonomyFieldsByContentTypeId.Add(listItem.ContentTypeId, fieldGuids); } if (fieldGuids.Count > 0) { streamWriter.Write(listItem.Url + ","); foreach (Guid fieldGuid in fieldGuids) { SPField field = listItem.Fields[fieldGuid]; streamWriter.Write("," + field.InternalName + ","); } streamWriter.WriteLine(); } }, delegate(SPListItem listItem, Exception e) { Debug.WriteLine("Error for item " + listItem.Url + ": " + e.Message); return(true); } ); } }
public static void ProcessItems(this SPList list, SPQuery query, Action <SPListItemCollection> itemAction, Func <SPListItemCollection, Exception, bool> itemActionOnError) { if (query.RowLimit == 0) { query.RowLimit = SPHelper.MaxRowLimit; } ContentIterator cItems = new ContentIterator(); //query.Query = query.Query + ContentIterator.ItemEnumerationOrderByNVPField; cItems.ProcessListItems(list, query, item => { if (itemAction != null) { itemAction(item); } }, (item, ex) => itemActionOnError != null && itemActionOnError(item, ex)); }
protected void btnIterator_Click(object sender, EventArgs e) { using (SPSite site = new SPSite(SPContext.Current.Web.Url)) ; using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists.TryGetList("LargeList"); if(list != null) { SPQuery camlQuery = new SPQuery(); camlQuery.ViewFields = @"<FieldRef Name =""Title""/><FieldRef Name=""Description""/>"; camlQuery.RowLimit = 8000; ContentIterator iterator = new ContentIterator(); iterator.ProcessListItems(list, camlQuery, ProcessItem, ProcessError); grdData.DataSource = table; grdData.DataBind(); } } }
/// <summary> /// Returns the total number of entities that correspond to the specified filter criteria. /// </summary> /// <param name="containerTitle"></param> /// <param name="path"></param> /// <param name="criteria"></param> /// <returns></returns> public virtual int CountEntities(string containerTitle, string path, EntityFilterCriteria criteria) { SPSite site; var web = GetDocumentStoreWeb(out site); SPList list; SPFolder folder; if (SPDocumentStoreHelper.TryGetFolderFromPath(web, containerTitle, out list, out folder, path) == false) { return(0); } ContentIterator.EnsureContentTypeIndexed(list); var documentStoreEntityContentTypeId = list.ContentTypes.BestMatch(new SPContentTypeId(Constants.DocumentStoreEntityContentTypeId)); var camlQuery = @"<Where> <Eq> <FieldRef Name=""ContentTypeId"" /> <Value Type=""ContentTypeId"">" + documentStoreEntityContentTypeId + @"</Value> </Eq> </Where>"; var viewFields = SPDocumentStoreHelper.GetDocumentStoreEntityViewFieldsXml(); var filteredListItems = new List <SPListItem>(); var itemsIterator = new ContentIterator(); itemsIterator.ProcessListItems(list, camlQuery + ContentIterator.ItemEnumerationOrderByPath + viewFields, ContentIterator.MaxItemsPerQueryWithViewFields, true, folder, spListItems => { var listItems = FilterListItemEntities(spListItems.OfType <SPListItem>(), criteria); // ReSharper disable AccessToModifiedClosure filteredListItems.AddRange(listItems); // ReSharper restore AccessToModifiedClosure }, null); return(filteredListItems.Count); }
private void MeasureUsage(SPList list) { SPQuery query = new SPQuery(); query.Query = @" <View Scope='RecursiveAll'> <ViewFields> <FieldRef Name='FileDirRef'/> <FieldRef Name='ContentTypeId'/> </ViewFields> </View> "; Console.WriteLine("Querying usage for list: " + list.RootFolder.Url); Debug.WriteLine("Querying usage for list: " + list.RootFolder.Url); ContentIterator contentIterator = new ContentIterator(); contentIterator.ProcessListItems(list, delegate(SPListItem listItem) { ReportRow row; if (this.rowsByContentTypeId.TryGetValue(listItem.ContentTypeId, out row)) { ++row.UsageCount; } else { Debug.WriteLine("Error for item " + listItem.Url + ": Content Type Not Found: " + listItem.ContentTypeId.ToString()); } }, delegate(SPListItem listItem, Exception e) { Debug.WriteLine("Error for item " + listItem.Url + ": " + e.Message); return(true); } ); }
/// <summary> /// Lists all folders contained in the specified container. /// </summary> /// <param name="containerTitle">The container title.</param> /// <param name="path">The path.</param> /// <returns></returns> public virtual IList <Folder> ListAllFolders(string containerTitle, string path) { SPSite site; var web = GetDocumentStoreWeb(out site); SPList list; SPFolder folder; if (SPDocumentStoreHelper.TryGetFolderFromPath(web, containerTitle, out list, out folder, path) == false) { return(null); } ContentIterator.EnsureContentTypeIndexed(list); var folderContentTypeId = list.ContentTypes.BestMatch(SPBuiltInContentTypeId.Folder); var camlQuery = @"<Where> <Eq> <FieldRef Name=""ContentTypeId"" /> <Value Type=""ContentTypeId"">" + folderContentTypeId + @"</Value> </Eq> </Where>"; var result = new List <Folder>(); var itemsIterator = new ContentIterator(); itemsIterator.ProcessListItems(list, camlQuery + ContentIterator.ItemEnumerationOrderByPath, ContentIterator.MaxItemsPerQuery, false, folder, spListItems => { // ReSharper disable ConvertToLambdaExpression result.AddRange(spListItems.OfType <SPListItem>() .Select( spListItem => SPDocumentStoreHelper.MapFolderFromSPFolder( spListItem.Folder))); // ReSharper restore ConvertToLambdaExpression }, null); return(result); }
static void ImportTemplate(SPList list) { TaxonomySession taxonomySession = new TaxonomySession(list.ParentWeb.Site, updateCache: true); // STEP 1: Load the TermSet objects for the SPField objects. Dictionary <string, TermSetLookup> termSetsByInternalName = new Dictionary <string, TermSetLookup>(StringComparer.OrdinalIgnoreCase); foreach (SPField field in list.Fields) { // Alternatively, the code could also support taxonomy fields that can take multiple values here. if (field.TypeAsString != "TaxonomyFieldType") { continue; } TaxonomyField taxonomyField = field as TaxonomyField; if (taxonomyField == null) { continue; } TermStore termStore = taxonomySession.TermStores[taxonomyField.SspId]; TermSet termSet = termStore.GetTermSet(taxonomyField.TermSetId); termSetsByInternalName.Add(field.InternalName, new TermSetLookup(termSet)); } // STEP 2: Load the Excel file. Dictionary <string, ExcelRow> excelRowsByUrl = new Dictionary <string, ExcelRow>(StringComparer.OrdinalIgnoreCase); // Parse the input file. Log("Reading AutomatedTagging.csv..."); using (StreamReader streamReader = new StreamReader("AutomatedTagging.csv")) { if (!streamReader.ReadLine().Contains("Filename")) { throw new InvalidOperationException("Invalid file format; header is missing"); } int lineNumber = 1; for (; ;) { string line = streamReader.ReadLine(); ++lineNumber; if (line == null) { break; } string[] csvValues = ParseCsvLine(line); if (csvValues == null) { throw new InvalidOperationException("[line " + lineNumber + "]: Syntax error"); } if (csvValues.Length < 1) { continue; } ExcelRow excelRow = new ExcelRow(csvValues[0], lineNumber); for (int i = 2; i + 1 < csvValues.Length;) { string key = csvValues[i++].Trim(); if (key == "") { break; } string value = csvValues[i++].Trim(); if (value == "") { break; } SPField field = list.Fields.GetFieldByInternalName(key); TermSetLookup termSetLookup = termSetsByInternalName[key]; Guid termId = termSetLookup.GetTermId(value); excelRow.Pairs.Add(new KeyValuePair <string, Guid>(field.InternalName, termId)); } excelRowsByUrl.Add(excelRow.ListItemUrl, excelRow); } } // STEP 3: Update the list items. ContentIterator contentIterator = new ContentIterator(); contentIterator.ProcessListItems(list, delegate(SPListItem listItem) { ExcelRow excelRow; if (!excelRowsByUrl.TryGetValue(listItem.Url, out excelRow)) { return; } excelRow.Processed = true; bool updated = false; foreach (KeyValuePair <string, Guid> pair in excelRow.Pairs) { TaxonomyField taxonomyField = (TaxonomyField)listItem.Fields.GetFieldByInternalName(pair.Key); TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField); taxonomyFieldValue.TermGuid = pair.Value.ToString(); TaxonomyFieldValue oldValue = listItem[taxonomyField.Id] as TaxonomyFieldValue; if (oldValue == null || oldValue.TermGuid != taxonomyFieldValue.TermGuid) { taxonomyField.SetFieldValue(listItem, taxonomyFieldValue); updated = true; } } if (updated) { Log("Updating item: " + listItem.Url); listItem.Update(); } }, delegate(SPListItem listItem, Exception e) { Log("Error processing item " + listItem.Url + ": " + e.Message); return(true); } ); // Were any items missed? Log(""); List <ExcelRow> missedRows = excelRowsByUrl.Values.Where(row => !row.Processed).ToList(); if (missedRows.Count > 0) { Log("Failed to match these rows"); foreach (ExcelRow row in missedRows) { Log(" " + row.ListItemUrl); } } }
/// <summary> /// Lists the entities. /// </summary> /// <param name="containerTitle">The container title.</param> /// <param name="path">The path.</param> /// <param name="criteria">The criteria.</param> /// <returns></returns> public virtual IList <Entity> ListEntities(string containerTitle, string path, EntityFilterCriteria criteria) { SPSite site; var web = GetDocumentStoreWeb(out site); SPList list; SPFolder folder; if (SPDocumentStoreHelper.TryGetFolderFromPath(web, containerTitle, out list, out folder, path) == false) { return(null); } ContentIterator.EnsureContentTypeIndexed(list); var documentStoreEntityContentTypeId = list.ContentTypes.BestMatch(new SPContentTypeId(Constants.DocumentStoreEntityContentTypeId)); var camlQuery = @"<Where> <Eq> <FieldRef Name=""ContentTypeId"" /> <Value Type=""ContentTypeId"">" + documentStoreEntityContentTypeId + @"</Value> </Eq> </Where>"; var viewFields = SPDocumentStoreHelper.GetDocumentStoreEntityViewFieldsXml(); var filteredListItems = new List <SPListItem>(); var itemsIterator = new ContentIterator(); itemsIterator.ProcessListItems(list, camlQuery + ContentIterator.ItemEnumerationOrderByPath + viewFields, ContentIterator.MaxItemsPerQueryWithViewFields, true, folder, spListItems => { var listItems = FilterListItemEntities(spListItems.OfType <SPListItem>(), criteria); // ReSharper disable AccessToModifiedClosure filteredListItems.AddRange(listItems); // ReSharper restore AccessToModifiedClosure }, null); var result = new List <Entity>(); if (criteria == null || criteria.IncludeData) { result.AddRange( filteredListItems .Select(li => { // ReSharper disable AccessToDisposedClosure var listItemFolder = li.Folder ?? web.GetFolder(SPUtility.GetUrlDirectory(li.Url)); // ReSharper restore AccessToDisposedClosure return(SPDocumentStoreHelper.MapEntityFromDocumentSet( DocumentSet.GetDocumentSet(listItemFolder), null)); }) .Where(entity => entity != null) ); ProcessEntityList(containerTitle, path, criteria, folder, result); } else { if (criteria.Skip.HasValue) { filteredListItems = filteredListItems.Skip((int)criteria.Skip.Value).ToList(); } if (criteria.Top.HasValue) { filteredListItems = filteredListItems.Take((int)criteria.Top.Value).ToList(); } result.AddRange( filteredListItems .Select( li => SPDocumentStoreHelper.MapEntityFromDocumentSet(DocumentSet.GetDocumentSet(li.Folder), null, null)) .Where(entity => entity != null) ); ProcessEntityList(containerTitle, path, criteria, folder, result); } return(result); }