2

I am trying to reset the current displayed view for an Xsltlistviewwebpart, but it always always keep the first initialized view:

Notice: the view changed based on caml query that rely upon an int variable (itemid) came from Request Object.

 int itemid = Convert.ToInt16(Request["itemId"]);
 SPList list = web.Lists["listname"];
 SPLimitedWebPartManager wpm =     
 web.GetLimitedWebPartManager(this.Page.Request.Url.AbsolutePath, 
 PersonalizationScope.Shared);

     if (wpm.WebParts.Count == 0)
        {
            XsltListViewWebPart XsltListViewWebPart1 = new XsltListViewWebPart();

            StringBuilder sb = new StringBuilder();
                sb.Append("<Where><Eq><FieldRef Name='fieldname'/><Value  
            Type='Lookup'>" + itemid.ToString().Trim().ToString() + "</Value>
            </Eq></Where><OrderBy><FieldRef Name='ID'/></OrderBy>");
            SPQuery query = new SPQuery();
            query.Query = sb.ToString();

            SPView vv = list.DefaultView;
            vv.Query = query.Query.ToString();
            vv.Update();
            list.Update();

            XsltListViewWebPart1.ViewGuid = vv.ID.ToString("B").ToUpperInvariant();
            XsltListViewWebPart1.ViewId = Int32.Parse(vv.BaseViewID); 
            XsltListViewWebPart1.XmlDefinition = vv.GetViewXml().ToString();

            wpm.AddWebPart(XsltListViewWebPart1, "2_second", 0);
            wpm.SaveChanges(XsltListViewWebPart1);
            pageFile.Update();

            web.Update();

        }else{

           foreach (System.Web.UI.WebControls.WebParts.WebPart current in wpm.WebParts)
           {
              if (current is XsltListViewWebPart)
                 {
                     XsltListViewWebPart XsltListViewWebPart1 = current as 
                     XsltListViewWebPart;


                      StringBuilder sb = new StringBuilder();
                sb.Append("<Where><Eq><FieldRef Name='fieldname'/><Value  
            Type='Lookup'>" + itemid.ToString().Trim().ToString() + "</Value>
            </Eq></Where><OrderBy><FieldRef Name='ID'/></OrderBy>");
            SPQuery query = new SPQuery();
            query.Query = sb.ToString();

            SPView vv = list.DefaultView;
            vv.Query = query.Query.ToString();
            vv.Update();
            list.Update();

            XsltListViewWebPart1.ViewGuid = vv.ID.ToString("B").ToUpperInvariant();
            XsltListViewWebPart1.ViewId = Int32.Parse(vv.BaseViewID); 
            XsltListViewWebPart1.XmlDefinition = vv.GetViewXml().ToString();


            wpm.SaveChanges(XsltListViewWebPart1);
            pageFile.Update();

            web.Update();

           }

What am I doing wrong?

1
  • 1
    What is this question Title? Commented Aug 27, 2014 at 14:45

1 Answer 1

1

the answer is:

XsltListViewWebPart copies the view on creation and forgets about it.

You should just use it.

   SPView view = list.Views[new Guid(lvwp.ViewGuid)];

as your view to work with, and not anything you created.

Or remove and add web part again after you changed your view.

ViewGuid setter only seems to be working before AddWebPart.

Here's a réference resetting displayed view in the XsltListViewWebPart!

   //what i did is:
   else{
       ....
   //instead of use of 
    SPView vv = list.DefaultView;

   //i used
   SPView vv = list.Views[new Guid(XsltListViewWebPart1.ViewGuid)];
      ...
    }

everything still as it is

Your Answer

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

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