How to programmatically update content in a SharePoint web part?

Does anyone know how to programmatically update the contents of any of the standard SharePoint v3 web parts?

As an example, we’ll place the Link Summary web part on the page. Add some links to it. Now, how can I update this information using the WSS API? I did not find a direct way to do this, my only idea is to export the web part (then delete it), modify the created XML and import it back. But of course, should there be an easier way?

+7
sharepoint web-parts
source share
2 answers

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

using (SPSite site = new SPSite("<site url>")) // eg http://server/sites/asite using (SPWeb web = site.OpenWeb()) { SPFile file = web.GetFile("<page url>"); // eg /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 remove web parts using SPLimitedWebPartManager.

+9
source share

You will probably need to call SPWeb.GetWebPartCollection and use a collection of web pages to mess using WebParts constant looks

+5
source share

All Articles