4

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;
    }

}
2
  • Interestingly this appears to be an environmental specific issue. I took the same setup and moved it to our QA environment and it works flawlessly. If anyone can explain what might be going on in my environment, that would be great, but it lease it's not going to impact the project anymore. Feb 2, 2012 at 17:16
  • Aren't these problems the best? Check you server versions (SP + CU installations). Also, if allowed and practical, take a backup of the content db from QA and install into your dev environment. Try to repro it again in your dev environment within the same site as you did in QA where it worked. Could be related to configuration of the list(s) and/or the content contained in the lists (or lack thereof).
    – Brian
    Mar 7, 2012 at 2:37

1 Answer 1

1

Edit the webpart file to set the default value using the property tag

<property name="" type="">[value]</property>

It probably isn't working because once it is altered you can't set the default property again in that webpart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.