SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Working With Web Parts Through Code

This post will show you how to work with web parts through code. Often times there is a need to work with web parts through the API. You can import, export, add remove as well as change settings of web parts.

Importing and Adding Web Parts.

The first thing we need to get is out site and web object.

SPSite oSite = new SPSite("http://yoursiteurl");
SPWeb oWeb = oSite.OpenWeb();

The next thing we need to get is the page (file) that we want to import the web part on. For this example we will be working with the default.aspx page on a team site.

SPFile oFile = oWeb.GetFIle("default.aspx");

After we have a file object we can get the limited web part manager. The limited web part manager is what we use to do all of our web part operations.

Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager oWebPartManager = oFile.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

Now we need to create a StringReader class. The constructor we are using takes a string as a parameter. This is simply the xml file of the web part that we want to import. (The web part .dwp or .webpart file).

System.IO.StringReader oReader = new StringReader(<<web part xml file (.dwp or .webpart file string contents>>);

Now we need to create a XmlReader and pass in our string reader.

XmlTextReader oXmlReader = new XmlTextReader(oReader);

We need to create a string for our output message.

string strErrorMessage = "";

No back to our web part manager, we need to import the web part. We simply call the ImportWebPart method on our limited web part manager object. The first parameter is the xml reader we created before and the second parameter is the output message passed back to us as a out parameter. This method will return a WebPart object.

System.Web.UI.WebControls.WebParts.WebPart oWebPart = spWebPartManager.ImportWebPart(xmlReader, out strErrorMessage);

No that we have the web part, we can set specific settings that we want to control for our web part. For example, if we don't want the close x to show up on the top right of the web part we could set the AllowHide property of the web part.

oWebPart.AllowClose = false;

No that we have actually imported the web part we need to actually add it to the site. We do this by calling the AddWebPart method on the limited web part manager. The first parameter is the web part that we imported in the previous step. The second parameter is the  zone id (as a string) that you want to add the web part to and the third parameter is the zone order (index).

oWebPartManager.AddWebPart(oWebPart, "left", 1);

Full code listing.

// Get the site
SPSite oSite = new SPSite("http://yoursiteurl");

// Get the web we are going to import and add the web part to
SPWeb oWeb = oSite.OpenWeb();

// Get the file (page) that we want to add the web part to
SPFile oFile = oWeb.GetFIle("default.aspx");

// Get the limited web part manager
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager oWebPartManager = oFile.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

// Create the string reader for the web part .dwp or .webpart file.
System.IO.StringReader oReader = new StringReader(<<web part xml file (.dwp or .webpart file string contents>>);

// Create the xml text reader to read the string into
XmlTextReader oXmlReader = new XmlTextReader(oReader);

// Create the error message that will get passed back
string strErrorMessage = "";

// Import the web part
System.Web.UI.WebControls.WebParts.WebPart oWebPart = spWebPartManager.ImportWebPart(xmlReader, out strErrorMessage);

// Now actually add the web part to the page
oWebPartManager.AddWebPart(oWebPart, "left", 1);

NOTE: You can also remove and export web parts through the limited web part manager.

Continue reading if you want to see how to accomplish the same tasks in SWAT (SharePoint Work Acceleration Toolkit). You can find SWAT here http://www.idevfactory.com

Set Web Part Properties

In the Farm Tree window, rick click on the site that you want to set the web parts properties for and click Show Site Details. This will load all the details for that site.

In the Site Details section, select the Site Pages tab.

In the Site Pages tab, right click on the page you want to look at the web parts of and click Show Page Web Parts.

On the Page Web Parts window, right click on the web part that you want to set the settings on and click Web Part Properties.

On the Web Part Properties window, select the specific properties you wish to set and click Update.

NOTE: You can also close and open a web part from this screen as well.

NOTE: Under the Wizards menu of SWAT, there a 3 different wizards dealing with web parts. Add Web Parts, Replicate Web Parts and Delete Web Parts. These wizards allow you to work with many web parts on many different sites all in one action.

Enjoy!!!


Posted 10-03-2007 6:16 AM by ethan

Comments

Links (10/3/2007) « Steve Pietrek’s SharePoint Stuff wrote Links (10/3/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff
on 10-03-2007 7:52 PM

Pingback from  Links (10/3/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

Ben wrote re: Working With Web Parts Through Code
on 10-13-2008 5:04 AM

is this done in Visual studio, new web part?

... wrote re: Working With Web Parts Through Code
on 03-10-2009 2:32 PM

Dies ist ein gro�er Ort. Ich m�chte hier noch einmal.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.