Getting Raw Field Values From SPListItem Or SPDataSourceViewResultItem

This method is helpful when you are working with the two different types, SPDataSourceViewResultItem providing custom type information:

[csharp]

 public object GetRawFieldValue(SPListItem item)
        {
            object fieldValue = null;
            if (item != null)
            {
                SPField fieldByInternalName = item.Fields.GetFieldByInternalName(InternalName);
                if (fieldByInternalName != null)
                {
                    fieldValue = item[fieldByInternalName.Title];
                }
            }
            return fieldValue;
        }

        public object GetRawFieldValue(T item)
        {
            object rawFieldValue = null;
            if (item is SPListItem)
            {
                SPListItem listItem = item as SPListItem;
                return GetRawFieldValue(listItem);
            }
            if (item is SPDataSourceViewResultItem)
            {
                SPDataSourceViewResultItem dataSourceViewResultItem = item as SPDataSourceViewResultItem;
                rawFieldValue = GetRawFieldValue(dataSourceViewResultItem);
            }
            return rawFieldValue;
        }

        public object GetRawFieldValue(SPDataSourceViewResultItem item)
        {
            object rawFieldValue = null;
            SPListItem spListItem = item.ResultItem as SPListItem;
            if (spListItem != null)
            {
                rawFieldValue = GetRawFieldValue(spListItem);
            }
            return rawFieldValue;
        }

[/csharp]

Share

Generating Columns For An SPGridView With LINQ (Extension Method)

More a small development note for myself since I generally end up using all the columns in a list when developing with SPGridView’s.

[csharp]
private static void GenerateColumns(this SPGridView gridView, SPList list )
{
gridView.AutoGenerateColumns = false;
foreach( var column in from string fieldname in list.DefaultView.ViewFields
select list.Fields.GetFieldByInternalName( fieldname )
into field
select new BoundField
{
DataField = field.StaticName,
HeaderText = field.Title
} )
{
gridView.Columns.Add( column );
}
}

[/csharp]

Share

Enumerating All SPWebs In SPFarm.Local Into Strongly Typed Collection

So when enumerating the SPWebs within a SPFarm to build a strongly typed SPWeb collection for whatever purpose your enumeration might look like this:

[csharp]

public static List WebsPreppedForIteration()
{
var collection = new List();
foreach (SPSite x in SPFarm.Local.Services.OfType().SelectMany
(svc => ((svc).WebApplications.Where
(webApp => !webApp.Properties.ContainsKey(“Microsoft.Office.Server.SharedResourceProvider”)).SelectMany
(webApp => webApp.Sites.Cast()))).Where
(x => !Equals(x.RootWeb.Title, “Central Administration”)))
{
collection.AddRange(x.RootWeb.Webs.Cast());
}
return collection;
}

[/csharp]

I saw this in a code review today. The part I am wondering about is the SPWebApplication property bag to query the key for WCAM as opposed to do a clunky string SPWeb.Title comparison. Putting the keys out to standard output hasn’t yielded anything particularly evident, and I’m getting frustrated with the under-the-hood, unnecessary foreach loop with a fancy shirt on (the second LINQ query against the Title property(,

Does anyone know the key for WCAM?

Share