0

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?

  • Version of SharePoint are you using? – Anders Aune Feb 22 '13 at 7:48
  • oh, sorry, 2010 – Ivan Feb 22 '13 at 8:05
2

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);
0

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

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.

  • thanks for response. probably you think create custom ITransformableFilterValues web part. I tried it, but in the end it filters only on view. User can disable the filter. I would like to pass only the data that match user profile value and from them user can filter using web part functionality – Ivan Feb 22 '13 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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