Question

I am challenging this requirement:

Client wants to show only list items in list view web part based on value from user profile and also preserve functionality of the list view web part like OOB sorting and filtering. I do some research and here is my finding:

  • i try to use same approach as listed here. However this approach has one very ugly drawback. It rewrites the view on each page refresh and leaves the query on the view based on last user user profile property - not acceptable.

Therefore I would like to generate like on-the-fly view for this web part and also apply the query on it. I would not like to edit the original view on list.

Any ideas please?

Was it helpful?

Solution

so, I resolved it with my colleague help, on create child controls I do following. Take schema xml from the view, overwrite query and pass it to list view. No new view is created and everything works.

var obtainedList = elevatedWeb.GetListFromWebPartPageUrl(FullUrl);

if (obtainedList == null)
{
   throw new Exception(string.Format(ListFromUrlNotObtained,FullUrl));
}

FullUrl = HttpUtility.UrlDecode(FullUrl);

var listViewWebPart = new ListViewWebPart();
listViewWebPart.WebId = elevatedWeb.ID;
listViewWebPart.ListId = obtainedList.ID;//.ToString("B").ToUpper();

var obtainedListView =obtainedList.Views.OfType<SPView>().FirstOrDefault<SPView>(lv => FullUrl.EndsWith(lv.Url));

if (obtainedListView == null)
{
   throw new Exception(string.Format(ViewFromUrlNotObtained,FullUrl));
}

var html = obtainedListView.HtmlSchemaXml;

var xml = XElement.Parse(obtainedListView.HtmlSchemaXml);
xml.Attribute("Url").Remove();
xml.Element(XName.Get("Query")).SetValue(WildcardProcessor.ProcessQuery(FilterQuery));
var updatedXml = xml.ToString();

listViewWebPart.ListViewXml = updatedXml;

Controls.Add(listViewWebPart);

OTHER TIPS

You can create view dynamically. I dont know about "Client wants to show only list items in list view web part based on value from user profile". But you can check for suppose view named "ClientView" into list if found delete it and create new view with same name for current user.

e.g.

SPList taskList = web.Lists[tasklistName];
                    string query = string.Empty;
                    if (taskList != null && taskList.ItemCount > 0)
                    {

                      query = "<OrderBy><FieldRef Name=\"Requirement_x0020_Details\" Ascending='TRUE'/></OrderBy><Where><Eq><FieldRef Name=\"Item_x0020_Type\"/><Value Type='Text'>Current</Value></Eq></Where>";

                    }
                    else
                    {
                        query = "<OrderBy><FieldRef Name=\"Requirement_x0020_Details\" Ascending='TRUE'/></OrderBy><Where><Eq><FieldRef Name=\"Task_x0020_Type\"/><Value Type='Text'>Current</Value></Eq></Where>";
                    }


                    StringCollection relevantColumns = CreateViewColumns();
                    SPView taskView = taskList.Views.Add("ClientView", relevantColumns, query, rowCount, true, false);

private StringCollection CreateViewColumns()
    {
        StringCollection viewFieldsCollection = new StringCollection();
        string tasksColumns = "Requirement_x0020_Details;DocIcon;LinkTitle;AssignedTo;Status;Priority;DueDate;PercentComplete";
        string[] columns = tasksColumns.Split(';');
        foreach (string column in columns)
        {
            viewFieldsCollection.Add(column);
        }
        return viewFieldsCollection;
    }

I'm not sure if this meets your requirements, but couldn't you do this Out of the box with the Current User Filter Web Part. It exposes User Profile data for the current user, then add a connection to your list and it will filter.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top