SharePoint 2010: Cross Site List and XsltListViewWebPart

I been working on a project recently and one of the pages required a list view from another site from the same site collection. So this is what I had to do to make it work.

var site = SPContext.Current.Site;
var spList = site.AllWeb["yourwebsite"].Lists["listname"];
var xsltLvWp = new XsltListViewWebPart(){
    Title = "My Cross Site List View",
    WebId = spList.ParentWeb.ID,
    ListName = spList.ID.ToString("B").ToUpper(),
    ListId = spList.ID,
    ViewGuid = spList.Views["View Name"].ID.ToString("B").ToUpper()
}
Shares