SharePoint 使用对象模型操作页面上的Web Part

在WSS的开发中有时需要通过程序操作页面上的Web Part,比较添加,删除Web Part.此时可以使用 SPLimitedWebPartManager这个类,代码如下:

        //添加Web Part

        string AbsolutePageUrl = "http://YourSite/Page.aspx";
        using (SPSite site = new SPSite(AbsolutePageUrl))
        {
            using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
            {
                SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
                System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                // if MyWebPart is already installed, leave! 
                foreach (var webpart in SpWebPartManger.WebParts)
                {
                    if (webpart is MyWebPart)
                        return;
                }
                // else add it in the bottom! 
                SpWebPartManger.AddWebPart(new MyWebPart(), "Main", 10);
                SpWebPartManger.Dispose();

            }
        }

 

        //删除Web Part
        string AbsolutePageUrl = "http://YourSite/Page.aspx";
        using (SPSite site = new SPSite(AbsolutePageUrl))
        {
            using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
            {
                SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
                System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                WebPart webPartToDelete = null;
                // let's locate the webpart first.... 
                foreach (WebPart webpart in SpWebPartMaanger.WebParts)
                {
                    if (webpart is MyWebPart)
                    {
                        webPartToDelete = webpart;
                        break;
                    }
                }

                if (webPartToDelete != null)
                    SpWebPartManger.DeleteWebPart(webPartToDelete);
                SpWebPartManger.Dispose();

            }
        }

可参考:Add or Remove WebParts into SharePoint 2007 on runtime using SPLimitedWebPartManager

           How to create Web Part instances on pages using Features - FeatureReceiver - SPLimitedWebPartManager

 

Content Query Web Part的RSS Feed的URL格如下:

http://servername/_layouts/feed.aspx?xsl=registered .xsl files in the Web.config file&web=webaddress&page=guid&wp=guid.

wp=guid中的这个guid是指Content Query Web Part的ID,但是它并非是WebPart.UniqueId而是要通过SPLimitedWebPartManager.GetStorageKey()这个方法获取

可参考: _layouts/feed.aspx querystring "wp" id - what is it?

转载于:https://www.cnblogs.com/ITHelper/archive/2010/11/02/1867605.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值