The code to accomplish this is quite easy, basically there are two scenarios: you add a web part that is displaying data for a list, or you add any other web part (e.g. a custom web part or a 3rd party web part like the SmartPart). The code for the first scenario is:
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
// Get a reference to a web and a list
SPSite site = new SPSite("http://localhost:8000");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Contacts"];
// Instantiate the web part
ListViewWebPart wp = new ListViewWebPart();
wp.ZoneID = "Left";
wp.ListName = list.ID.ToString("B").ToUpper();
wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
// Get the web part collection
SPWebPartCollection coll =
web.GetWebPartCollection("default.aspx",
Storage.Shared);
// Add the web part
coll.Add(wp);
First you get a reference to the SPWeb in which you want to add the web part, and to the list you want to use (in this example the Contacts list). Next you create an instance of the ListViewWebPart class, in which you can set the ZoneID, the ListName and the ViewGuid. This is the tricky part, the ListName property should contain the ID of your list (a GUID), not the name of your list!! But the ListName property is of the type string, so you need to convert the List GUID to a string using .ToString(“B”).ToUpper(). The same goes for the ViewGuid. Finally you need to get a reference to the WebPartCollection for the page in which you want to add the web part (in this example the home page, being default.aspx). Now you can add the web part using the Add method.
For the second scenario, the code is as follows:
// Get a reference to a web and a listThe first section is the same, get a reference to your site and the WebPartCollection (you don’t need a reference to a list). Next you need to build a XML string containing the information about the web part you want to add. The contents of that string are the same as the DWP file that you need to create to use your custom web parts. A trick to figure out what needs to be in there (especially if you want to specify values for some properties) is to put the web part on a page (using the web interface), and choose “Export” from the dropdown menu of the web part. This will save the contents of the DWP in a file. Finally you can add the web part by calling the Add method of the WebPartCollection and using the dwp string as a parameter.
SPSite site = new SPSite("http://localhost:8000");
SPWeb web = site.OpenWeb();
// Get the web part collection
SPWebPartCollection coll =
web.GetWebPartCollection("default.aspx",
Storage.Shared);
string dwp = @"
http://www.w3.org/2001/XMLSchema" ";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns=""http://schemas.microsoft.com/WebPart/v2"">
SmartPart List 1.0.0.0 Left
SmartPart, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=dd064a5b12b5277a
SmartPart.UserControlWebpart
~\UserControls
coll.Add(dwp);
I have recently had to come up with a way to programmatically add a Web Part to a large number of existing publishing pages. It took me a little while to find the exact way to do it, so I thought I would post the source code here.
Attached is the C# code that will iterate over a bunch of pages in a bunch of sites, and then add a doclib Web Part on all those pages, in a specific Web Part Zone. The meat of the code is this:
SPSite mySiteCollection = new SPSite("http://sharepoint");
SPWeb w = mySiteCollection.AllWebs["/whatever"];
SPFile f = w.GetFile("Pages/Page.aspx");
SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);
SPList l = w.Lists["Documents"];
ListViewWebPart wp = new ListViewWebPart();
wp.ListName = l.ID.ToString("B").ToUpper();
wp.ViewGuid = l.DefaultView.ID.ToString("B").ToUpper();
wpm.AddWebPart(wp, "Zone 1", 1);
f.Publish("Added Web Part");
f.Approve("Web Part addition approved");
This bit of code uses an SPLimitedWebPartManager to add the ListViewWebPart to the "Zone 1" Web Part Zone. The page is then published and approved.
Kaynak:
http://blogs.msdn.com/tconte/archive/2007/01/18/programmatically-adding-web-parts-to-a-page.aspx
Hiç yorum yok:
Yorum Gönder