Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
Version of SharePoint are you using? –  Anders Aune Feb 22 '13 at 7:48
    
oh, sorry, 2010 –  Ivan Feb 22 '13 at 8:05
add comment

3 Answers

up vote 1 down vote accepted

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);
share|improve this answer
add comment

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;
    }
share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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