Wednesday, December 9, 2009

Use ListViewWebPart to display listdata from different than current sharepoint site.

using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace TestDashboard
{
[Guid("b732479d-6bcd-4dd9-97de-85f11bsdc5cb")]
public class TestDashboard : System.Web.UI.WebControls.WebParts.WebPart
{
public TestDashboard()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls();

ListViewWebPart wp = new ListViewWebPart();
SPSite objSpSite = null;
using (objSpSite = new SPSite("http://SiteColName/SiteName/"))
{
objSpSite = new SPSite("http://SiteColName/SiteName/", objSpSite.SystemAccount.UserToken);
using (SPWeb spweb = objSpSite.OpenWeb())
{
SPList splist = spweb.Lists["ListName"];

wp.ListName = splist.ID.ToString("B").ToUpper();
wp.ViewGuid = splist.Views["All Items"].ID.ToString("B").ToUpper();
wp.ViewType = ViewType.Html;
wp.WebId = spweb.ID;
wp.SuppressWebPartChrome = true;
wp.GetDesignTimeHtml();
this.Controls.Add(wp);
}
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
EnsureChildControls();

}
}
}

No comments: