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?