As a general practice, we tend to check that a SharePoint list column/field isn't null before we try to set/get it (normally just a few fields), but what should we do in case we are working on an application where we have a lot of fields to check?  We can break the code into a separate class so that we can can create an object to make it easier to work with. Is it typical to do something like the following for every field? Is there a better way to do this?

//
// Microsoft provides programming examples for illustration only,
// without warranty either expressed or implied, including, but not
// limited to, the implied warranties of merchantability and/or
// fitness for a particular purpose.
//
// This sample assumes that you are familiar with the programming
// language being demonstrated and the tools used to create and debug
// procedures. Microsoft support professionals can help explain the
// functionality of a particular procedure, but they will not modify
// these examples to provide added functionality or construct
// procedures to meet your specific needs. If you have limited
// programming experience, you may want to contact a Microsoft
// Certified Partner or the Microsoft fee-based consulting line at
// (800) 936-5200.
//
// For more information about Microsoft Certified Partners, please
// visit the following Microsoft Web site:
//
 
// Allows AddItem operation using anonymous access
 
private static void AllowAnonAccess()
        {
            Console.WriteLine("Enabling Anonymous access....");
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(webAppUrl));
            webApp.ClientCallableSettings.AnonymousRestrictedTypes.Remove(typeof(Microsoft.SharePoint.SPList), "GetItems");
            webApp.ClientCallableSettings.AnonymousRestrictedTypes.Remove(typeof(Microsoft.SharePoint.SPList), "AddItem");
            webApp.Update();
            Console.WriteLine("Enabled Anonymous access!");
        }
 
// Revokes Add/Get Item operation using anonymous access
        private static void RemoveAnonAccess()
        {
            Console.WriteLine("Disabling Anonymous access....");
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(webAppUrl));
            webApp.ClientCallableSettings.AnonymousRestrictedTypes.Add(typeof(Microsoft.SharePoint.SPList), "GetItems");
            webApp.ClientCallableSettings.AnonymousRestrictedTypes.Add(typeof(Microsoft.SharePoint.SPList), "AddItem");
            webApp.Update();
            Console.WriteLine("Disabled Anonymous access!");
        }


So what are the other options you can perform the NULL value check for each of the fields? You can find several options of how you can perform that check below.

Use a ternary condition to get the value of a list item field (or set a default value if the field is null)

Sample code
item = list.Items[0];
var field1Value = item["fieldname1"] == null ? String.Empty : item["fieldname1"].ToString();
var field2Value = item["fieldname2"] == null ? false : Boolean.Parse(item["fieldname2"].ToString());

Write extension methods for SPListItem, so that you can call them right away

Sample Code
public static T GetValue<T>(this SPListItem item, string fieldName) where T : class
{
                 object o = item[fieldName];
                 if (o == null || !(o is T)) return null;
                 return (T)o;
}

Validation in Javascript to see if the field value is empty


 Sample code
function PreSaveAction() { var columnName = document.getElementById('<% =hdnMandatoryColumn.ClientID %>'); if (columnName != null && columnName.value != null) { var column = getTagFromIdentifierAndTitle("input", "", columnName.value) if (column.value == '') { alert(columnName.value + ' is a required field.'); return false; } else { return true; } } }
 
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);
    for (var i = 0; i < tags.length; i++) {
        var tempString = tags[i].id;
        if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
            return tags[i];
        }
    }
    return null;
}

From the above solutions you can pick any option which best fits in your requirement.

You can check the problem statement and opinions at this link.


See Also


An important place to find a huge amount of SharePoint related articles is the TechNet Wiki itself. The best entry point is SharePoint Resources on the TechNet Wiki