To use SharePoint's ListViewWebPart directly in your ASPX page, follow these steps:
1) Register Microsoft.SharePoint.WebPartPages namespace in ASPX page :
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
2) Add following ListViewWebPart's html code :
<WebPartPages:ListViewWebPart ID="oListViewWP" runat="server" __WebPartId="{GUID}"
WebPart="true"></WebPartPages:ListViewWebPart>
Note : Replace
GUID in above code either by a newly generated GUID or if you are using SharePoint designer, then SharePoint designer will automatically insert a new GUID in your HTML code when you try to save the ASPX page.
3) Add following server side code to populate data in ListViewWebPart :
SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["Announcements"];
oListViewWP.ListName = oList.ID.ToString("B").ToUpper();
oListViewWP.ViewGuid = oList.Views["MyView"].ID.ToString("B").ToUpper();
oListViewWP.GetDesignTimeHtml();
Note : Don't forget to write
oListViewWP.GetDesignTimeHtml();, otherwise ListViewWebPart will not display any data in it.
Here
MyView is just a dummy SPView name, you can use either default SPView or can create a new SPView with only desired columns and settings.
====================================================================
====================================================================
Now if you want to hide/remove the ToolBar from ListViewWebPart, then as per me following are the two ways :
1) Set
SuppressWebPartChrome property of WebPart
oListViewWP.SuppressWebPartChrome = true;
2) One of the problem with above code is that it hide the toolBar in ListViewWebPart as well as in the SPList/SPView from which it is displaying the data
( I face this problem, not sure about others :) ).
So, the other alternative is to add following CSS in our code:
<style type="text/css">
.ms-menutoolbar { display: none; }
</style>
Be careful while using the above CSS, because it hide all the ToolBars which uses the
.ms-menutoolbar CSS class.
Reference :MSDN Forum====================================================================
====================================================================
If you want to apply Filter on ListViewWebPart's column or in other words, display data in ListViewWebPart with only desired column value, then write following code :
string columnValue = "SP";
oListViewWP.FilterString = "FilterField1=Title&FilterValue1=" + columnValue;
The above code results in the display only those records whose column name
Title has value
SP.
====================================================================
====================================================================
Another major challenge that i faced while using ListView WebPart (or in any normal WebPart) is to change/modify the Empty message or no record message.
The exact message is :
There are no items to show in this view of the "ViewName" list. To create a new item, click "New" above.One of the way to change this message is to modify the
ViewEmpty XML node of
ListViewXml.
Following code snippet describes the way to change the empty message of ListView WebPart :
//oListViewWP is ListView WebPart instance
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(oListViewWP.ListViewXml);
XmlNode node = xmlDoc.SelectSingleNode("//ViewEmpty");
if (node != null)
{
StringBuilder strB = new StringBuilder();
strB.Append("<HTML><![CDATA[<SPAN Class='ms-vb' style='width:200px;'>]]></HTML>");
strB.Append("<HTML>There is no item to show.</HTML>");
strB.Append("<HTML><![CDATA[</SPAN>]]></HTML>");
node.InnerText = strB.ToString();
}
oListViewWP.ListViewXml = xmlDoc.OuterXml;
oListViewWP.GetDesignTimeHtml();
Note : It is required to set the
ListViewXml before
GetDesignTimeHtml() function call.
Otherwise, if you are using SharePoint Designer and using/populating ListView WebPart in design time. Then simply search following text :
There are no items to show in this view of the and replace it with your required/desired message.
Another way is to change/modify the text directly in
core.resx file that reside at following location :
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Resources\core.resx.
Note : If we change the core.resx file, it will be gets reflected in all sites that are using the core.resx resource file.