Archive for August 15, 2012

GetDataTable Object Reference Error

Calling the GetDataTable method of a SPListItemCollection object that was retrieved using a join with projected fields will throw the following error:

Object reference not set to an instance of an object

For example:

SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name="Title"/><Value Type="Text">Search Criteria</Value></Eq></Where>";
query.Joins = "<Join Type="Left" ListAlias="Name of join list"><Eq><FieldRef Name="Title" RefType="Id" /><FieldRef List="Name of join list" Name="Id" /></Eq></Join>";
query.ProjectedFields = "<Field Name='Field name' Type='Lookup' List='Name of join list' ShowField='Field name'/>";
query.ViewFields = "<FieldRef Name="Field Name" /><FieldRef Name="Title" />";
 
// Next line will cause a "Object reference not set to an instance of an object" exception.
DataTable table = listItems.GetDataTable();

Simply access the Fields property prior to calling GetDataTable to resolve this issue:

SPFieldCollection fields = listItems.Fields;
DataTable table = listItems.GetDataTable();

Accessing the Fields property instantiates an object that is used by the GetDataTable method when there are projected fields in the join and the exception is resolved.

Create a local term set

SharePoint Short #12

Instead of creating term sets that are available farm wide, it’s also possible to create a term set that is only local to a particular site collection.

Create a site column as normal, selecting Managed Metadata type

Within the Term Set Settings section, select Customize your term set:

Then simply add terms to the top level node (same name as your column by default) and once your happy click OK to create your metadata column based on a local term set.

PeopleEditor in Bound Control

Having problems updating a dynamically created PeopleEditor control on postback when utilising it in a bound repeating control? The following solution might help you.

In the CreateChildControls method of your control create and add a HiddenField control at the same level as the PeopleEditor control you want to update:

peopleEditor = new PeopleEditor();
// Initialise your PeopleEditor
// ...
Controls.Add(peopleEditor);
 
_hiddenField = new HiddenField { ID = "_hiddenField" };
Controls.Add(_hiddenField);

and then in the ItemCommand event (for example) of your bound control, set the value of the HiddenField control to the value of the PeopleEditor:

_hiddenField.Value = peopleEditor.CommaSeparatedAccounts.Replace(",", "; ");

Here we replace the , with a ; as this is the separator used within the page DOM by the people editor.
Read more

Follow

Get every new post delivered to your Inbox

Join other followers: