views:

3114

answers:

5

Does anybody know how to programmatically update the content of any of the standard SharePoint v3 Web Parts?

As an example, put a Link Summary Web Part on a page. Add some links to it. Now, how can I update this information using the WSS API? I have not found any direct way to do this, my only idea so far is to export the Web Part, (then delete it), modify the generated XML, and import it back. But surely, there must be an easier way?

A: 

What do you mean by "change the content"?

Muad'Dib
+4  A: 

You will probably need to call SPWeb.GetWebPartCollection and use the webpart collection to mess with the WebParts thusly

Nat
+5  A: 

You can use the SPLimitedWebPartManager class to manipulate Web parts on a Web part page. An instance of this class can be obtained from an SPFile object as follows:

using (SPSite site = new SPSite("<site url>"))  // e.g. http://server/sites/asite
using (SPWeb web = site.OpenWeb())
{
    SPFile file = web.GetFile("<page url>");   // e.g. /sites/asite/default.aspx
    SPLimitedWebPartManager lwpm = file.GetLimitedWebPartManager();
    SPLimitedWebPartCollection webParts = lwpm.WebParts;
    WebPart wp = webParts[<id, index or Guid>];

    // Add your code to update the Web Part

    lwpm.SaveChanges(wp);
}

You can also add or delete web parts with the SPLimitedWebPartManager.

Lars Fastrup
A: 

Maybe something like the approach here? http://www.sharepointblogs.com/peoplenet/archive/2007/04/02/dynamically-adding-a-content-editor-web-part.aspx

I'm trying to do something similar, so if you do figure out a way, please post it here. Thanks!

That article is about adding Web Parts. I wanted to modify content in an existing Web Part.
Magnus Johansson
A: 

To modify web part property values programmatically, refer following post:

http://www.etechplanet.com/post/2009/03/19/Modify-property-values-of-a-web-part-in-SharePoint-website-programmatically-using-C.aspx