如何以编程方式更新 SharePoint Web 部件中的内容? [英] How to programmatically update content in a SharePoint Web Part?

查看:36
本文介绍了如何以编程方式更新 SharePoint Web 部件中的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何以编程方式更新任何标准 SharePoint v3 Web 部件的内容吗?

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

例如,在页面上放置一个链接摘要 Web 部件.添加一些链接.现在,如何使用 WSS API 更新此信息?我还没有找到任何直接的方法来做到这一点,到目前为止我唯一的想法是导出 Web 部件(然后删除它),修改生成的 XML,然后将其导入回来.但是,肯定有更简单的方法吗?

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?

推荐答案

您可以使用 SPLimitedWebPartManager 类来操作 Web 部件页面上的 Web 部件.可以从 SPFile 对象中获取此类的实例,如下所示:

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);
}

您还可以使用 SPLimitedWebPartManager 添加或删除 Web 部件.

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

这篇关于如何以编程方式更新 SharePoint Web 部件中的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆