I am going through all the lists in site collection, where list contains content type MvContentType, and for all list items in this list (this list only contain this content type, so all items have this content type), trying to get some fields/metadata values for all list items.
I dont need to get whole SPListItem but only some metadata for this item.
Is there any way to optimise this code or make it more efficient/better which are marked with "??" in code or whole code in general ?
using (SPSite site = new SPSite(siteUrl))
{
SPWebCollection spWebs = site.AllWebs;
foreach (SPWeb web in spWebs)
{
try
{
SPListCollection lists = web.Lists; // ??
foreach (SPList list in lists)
{
SPContentType ctype = list.ContentTypes.Cast<SPContentType>().FirstOrDefault(ct => ct.Name.Contains("MyContentType"));
if (ctype != null)
{
int itemsCount = list.ItemCount - 1;
SPListItemCollection items = list.Items; // ??
for (int i = 0; i < itemsCount; i++)
{
SPListItem item = items[i]; // ??
String valueA = item["FieldA"]; // ??
String valueB = item["FieldB"]; // ??
}
}
}
}
catch (Exception)
{
// some exception handling
}
finally
{
web.Dispose();
}
}
}
Thanks.