How to programmatically update content in a SharePoint Web Part?

风流意气都作罢 提交于 2019-12-05 23:24:00

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/485527/how-to-programmatically-update-content-in-a-sharepoint-web-part

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!