SharePoint 2010 Create XsltListView WebPart Programatically

In moss 2007 we use the ListView WebPart to display the List item but in Sharepoint 2010 we explore the XsltListView WebPart to display the List item.

using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))  

{  

    using (SPWeb web = site.OpenWeb())  

    {  

        SPList list = web.Lists["Tracker"];  

        XsltListViewWebPart XsltListViewWebPart1 = new XsltListViewWebPart();  

        XsltListViewWebPart1.ListId = list.ID;  

        SPView lstview = list.DefaultView;  

        XsltListViewWebPart1.Toolbar = "";  

        StringBuilder xml = new StringBuilder();  

        xml.Append("<View Name='{" + list.DefaultView.ID.ToString("B").ToString().ToUpperInvariant() + "}' TabularView='FALSE' MobileView='TRUE' Type='HTML'  Hidden='TRUE' DisplayName='' Url='" + Request.Url.ToString() + "' Level='1' BaseViewID='1' ContentTypeID='0x' ImageUrl='/_layouts/images/generic.png'>");  

        xml.Append("<Query><OrderBy><FieldRef Name='ID'/></OrderBy></Query>");  

        xml.Append("<ViewFields> <FieldRef Name='LinkTitle'/><FieldRef Name='Edit'/><FieldRef Name='Role'/></ViewFields>");  

        xml.Append("<RowLimit Paged='TRUE'>30</RowLimit>");  

        xml.Append("<Aggregations Value='Off'/>");  

        xml.Append("<Toolbar Type='Freeform'/></View>");  

        XsltListViewWebPart1.AllowEdit = true;  

        XsltListViewWebPart1.AllowClose = false;  

        XsltListViewWebPart1.AllowConnect = false;  

        XsltListViewWebPart1.AllowHide = false;  

        XsltListViewWebPart1.AllowMinimize = false;  

        XsltListViewWebPart1.AllowZoneChange = false;  

        XsltListViewWebPart1.ChromeType = PartChromeType.None;  

     

        XsltListViewWebPart1.XmlDefinition = xml.ToString();  

        XsltListViewWebPart1.AsyncRefresh = true;  

        XsltListViewWebPart1.ShowToolbarWithRibbon = true;  

        XsltListViewWebPart1.ExportMode = WebPartExportMode.All;        

        Panel1.Controls.Add(XsltListViewWebPart1);  

    }  

Ebook Download
View all
Learn
View all