Enhanced Lookup Field – Part 1

The SharePoint lookup field is far from perfect. Once configured, it’s not possible to change the list it gets it’s data from. This is a good thing while the list actually exists.

Now, if a user deleted the source list, the lookup field becomes next to useless and will need to be deleted and re-created.

Would it not be good to be able to re-configure it when this happens?

Or, how about when the source list or display field is deleted, items that have values are not deleted but instead display (in views and the item display form) the original value when they were created?

So, if you had a lookup field that retrieved its values from a list called Cities and in another list this column was added and had existing items. With the out of the box lookup field, if you delete the list Cities any fields that depends on this will stop showing the linked data. Wouldn’t it be good if the custom field detected this and showed the lookup values that were set when the item was last updated? When the list exists, the field would then show the current live item. So, if you changed the name of a city, the field would reflect this, delete the item and the original value would be shown.

Read on to find out how…
Continue reading

Posted in SharePoint | Tagged , | Leave a comment

Manually Set Form Field Context

When you’re developing custom code for SharePoint, sometimes you’ll have a situation where you’re dynamically adding form field controls to your control or WebPart and the form context will not be automatically picked up by the controls. If you’re using them outside a new, display or edit form for example.

It’s relatively simple to force the control(s) to the desired context but there’s something you should be aware of.

Take a look the following code:
Continue reading

Posted in SharePoint | Tagged , | Leave a comment

PeopleEditor control not available error

If you’re using a PeopleEditor control in your solution and enforce the filtering of selectable users by specifying a SharePoint group in the SharePointGroup property you may see the following error instead of the PeopleEditor control:

The control is not available because you do not have the correct permissions

This can happen if the context user does not have adequate permissions against the SharePoint group you specified as the filter group.

For instance, if you create a dummy group to contain the filtered set (where the users are also part of another group which grants them specific permissions) and do not grant any permissions to it when creating, it this error will occur for the majority of users, except the admin users.
Continue reading

Posted in Configuration, SharePoint | Tagged , , | Leave a comment

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.

Posted in SharePoint | Tagged | 2 Comments

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.
Continue reading

Posted in SharePoint | Tagged , , | Leave a comment