I have a request from a client to filter the data in a list view webpart based off of some user controlled custom settings for our sharepoint applilcation. These user settings are stored in a sharepoint list, and each user has an entry in their list where one column is a User field pointing to their account and the other columns are their specific settings.
In order to achieve this I'm trying to use mostly built in Filter WebParts. I created a single custom webpart called "User Settings Filter" which lets me surface any user settings property and connect it to other webparts. I connect an instance of that webpart to the List Filter webpart in order to set that list filter's default value. Then I use the value of the list filter to filter the list view webpart.
The problem I'm having is that I can't seem to have both the default value piece and the filtering piece work at the same time. If my list filter is not wired up to filter the list, it receives the default value from my user settings filter just fine. However the moment I connect the list filter to a list, it stops having a default value. Below is the code for my custom filter. Am I doing anything wrong?
[ToolboxItemAttribute(false)]
public class UserSettingsFilter : WebPart, Microsoft.SharePoint.WebPartPages.ITransformableFilterValues, Microsoft.SharePoint.WebPartPages.IDefaultFilterValue
{
private const string _defaultListName = "User Settings";
private SPList _spList
{
get
{
var list = SPContext.Current.Web.Lists.TryGetList(ListName);
if (list== null)
throw new ArgumentException(string.Format("Cannot find a user settings list with the name {0}. Please check your webpart configuration and input the correct list name", ListName));
return list;
}
}
private string _listName;
[WebBrowsable(true),
Personalizable(PersonalizationScope.Shared),
WebDescription("User Settings List"),
Category("Filter Field"),
WebDisplayName("User Settings List"),
DefaultValue(_defaultListName)]
public string ListName
{
get
{
if (_listName == null)
_listName = _defaultListName;
return _listName;
}
set
{
_listName = value;
}
}
[WebBrowsable(true),
Personalizable(PersonalizationScope.User),
WebDescription("User Settings Property"),
Category("Filter Field"),
WebDisplayName("User Settings Property")]
public string SettingsProperty
{
get;
set;
}
protected override void CreateChildControls()
{
}
public bool AllowAllValue
{
get { return false; }
}
public bool AllowEmptyValue
{
get { return false; }
}
public bool AllowMultipleValues
{
get { return false; }
}
public string ParameterName
{
get
{
if ((this.SettingsProperty != null) && (this.SettingsProperty.Length > 0))
{
return this.SettingsProperty;
}
else
{
return "Unknown";
}
}
}
public ReadOnlyCollection<string> ParameterValues
{
get
{
SPUser user = SPContext.Current.Web.CurrentUser;
var settings = new UserSettingsProvider(_spList).GetUserSettingsListItem(user.ID);
var value = settings[ParameterName] as string;
if (settings.Fields[ParameterName].Type == SPFieldType.Lookup || settings.Fields[ParameterName].Type == SPFieldType.User)
{
if (value != null)
{
value = new SPFieldLookupValue(value as string).LookupValue;
}
}
string[] values = new string[] {value};
return new ReadOnlyCollection<string>(values);
}
}
public string DefaultValue
{
get
{
return GetValue();
}
}
private string GetValue()
{
SPUser user = SPContext.Current.Web.CurrentUser;
var settings = new UserSettingsProvider(_spList).GetUserSettingsListItem(user.ID);
if (settings == null)
return null;
var value = settings[ParameterName] as string;
if (settings.Fields[ParameterName].Type == SPFieldType.Lookup || settings.Fields[ParameterName].Type == SPFieldType.User)
{
if (value != null)
{
value = new SPFieldLookupValue(value as string).LookupValue;
}
}
return value;
}
[ConnectionProvider("UserSettings Filter", "ITransformableFilterValues", AllowsMultipleConnections = true)]
public Microsoft.SharePoint.WebPartPages.ITransformableFilterValues GetFilterConnectionInterface()
{
return this;
}
[ConnectionProvider("UserSettings Filter", "IDefaultFilterValue", AllowsMultipleConnections = true)]
public Microsoft.SharePoint.WebPartPages.IDefaultFilterValue GetDefaultValueConnectionInterface()
{
return this;
}
}