SharePoint 2010 Create XsltListView WebPart Programatically

Posted: November 7, 2014 in SharePoint2010

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;





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s