About Me

My photo
a Dynamic and Energetic guy.....

Sunday, October 5, 2008

Add a OOB webpart programatically in to a page

//
Add a webpart programatically in a easy manner
J

I tried to find out a method to add OOB webparts directly into a page.
Today I managed to do it!!!!!!!

This
is very useful if we are going to add Events, Forum(Discussion
board), Tasks list into our page with the OOB features.

using(SPLimitedWebPartManager wpm =web.GetLimitedWebPartManager(siteMapPage.Url,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))

{

//am creating a Task list

Guid WorkflowHistoryGuid = web.Lists.Add(ConfigurationInfo.Tasks, ConfigurationInfo.Tasks,SPListTemplateType.Tasks);


SPList TaskList = web.Lists[ConfigurationInfo.Tasks];

TaskList.OnQuickLaunch = true;
TaskList.Update();

//actual
work starts

ListViewWebPart webPart = new ListViewWebPart();
SPList
taskList = web.Lists[ConfigurationInfo.Tasks];
SPView
viewMyTasks = taskList.Views[“My Tasks”];
viewMyTasks.DefaultView = true;
viewMyTasks.Update();

taskList.Update();

webPart.ListName = taskList.ID.ToString("B").ToUpper();

webPart.ViewGuid = taskList.DefaultView.ID.ToString("B").ToUpper();
wpm.AddWebPart(webPart,"MiddleRightZone", 2);

}

No comments:

My Masters