如何从Web部件中的页面获取Web部件中的自定义设置的引用?

时间:2015-01-06 20:57:03

标签: sharepoint sharepoint-2010 web-parts

我创建了一个webpart,然后我创建了第二个.cs页面,它将呈现自定义html。它基本上只是一个显示图像的简单页面。但是在第二个.cs页面中,我需要访问Web部件中的自定义设置。如果单击指向辅助页面的链接,如何在主Web部件中访问自定义工具窗格设置?

第二页是从VS 2010生成的,位于layouts文件夹中,如下所示:

    public partial class GetFile : LayoutsPageBase
    {

我想我必须从网络部分继承一些内容才能做到这一点,但真的不确定如何?

1 个答案:

答案 0 :(得分:1)

您可以通过阅读页面上的Web部件集及其属性,使用SPLimitedWebPartManager类访问Web部件属性,如下所示,

        var web = SPContext.Current.Web;
        var currentPageURL = "//URL of the page on which the Web part is present";
        var page = web.GetFile(currentPageURL);
        using (SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
        {
            try
            {
                var webPartList = from System.Web.UI.WebControls.WebParts.WebPart webPart in webPartManager.WebParts select webPart;

                foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in webPartList.ToList())
                {
                    if (webPart.Title.ToLower() == "//Name of the web part for which you want to read the properties")
                    {
                        PropertyInfo[] wptProperties = webPart.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

                        foreach (PropertyInfo property in wptProperties)
                        {
                            if (property.Name == "//Name of the web property which you want to get")
                            {
                                // Following code updates the web part property, you can also read it
                                property.SetValue(webPart, "value to be set", null);
                                webPartManager.SaveChanges(webPart);
                                break;
                            }
                        }
                    }
                }
                Response.Redirect(currentPageURL);
            }
            finally
            {
                webPartManager.Web.Dispose();
            }
        }