Objective is to add the listviewer web part on the page programmatically. I have a document library for which i want to add the list viewer web part on any page.
Create a console application and paste the code
Code snippet
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite(”Site Url”);
SPWeb oSubWeb = site.OpenWeb();
oSubWeb.AllowUnsafeUpdates = true;
//Get the object of the page as SPFile on which we need to add the listviewer webpart
SPFile oFile = oSubWeb.GetFile(”default.aspx”);
//Create the object SPLimitedWebPart Manager
SPLimitedWebPartManager oWebPM = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
//Get the object of the list of which we are creatin the list viewer webpart
SPList oSubSiteList = oSubWeb.Lists["Shared Documents"];
//Create the object of ListViewer webpart
ListViewWebPart oListViewWP = new ListViewWebPart();
//Set the properties of the webpart
oListViewWP.ChromeType = PartChromeType.TitleOnly;
oListViewWP.Title = “Shared Documents”;
oListViewWP.ListName = oSubSiteList.ID.ToString(”B”).ToUpper();
//Define the zone in which webparts need to be added
oWebPM.AddWebPart(oListViewWP, “Zone 1″, 1);
}
}
Your job is done….
Enjoy!!!
If you find this post useful then please post your comments
No comments:
Post a Comment