簡體   English   中英

創建頁面時向WebPartPage添加描述

[英]Adding description to WebPartPage when creating the page

我正在遵循此(http://msdn.microsoft.com/zh-cn/library/ms450826.aspx)方法來添加Webpartpage(samplewpp.aspx),並且它可以正常工作。 但是,我還需要添加一行描述。 怎么樣?

您需要在頁面上添加內容編輯器Web部件(CEWP),然后在其中添加描述。 CEWP允許您將text / html放在頁面上。

要以編程方式執行此操作,然后遵循Razi bin Rais的以下代碼 :-

AddAndFillCEWP("http://server","/" ,"/Pages/blank.aspx","this text is adding via    code","Header","CEWP WebPart");

private void AddAndFillCEWP(string siteUrl, string webName, string pageUrl, string textCEWP, string zoneId, string title)
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite spSiteTest = new SPSite(siteUrl))
        {
            using (SPWeb web = spSiteTest.OpenWeb(webName))
            {
                try
                {
                    web.AllowUnsafeUpdates = true;
                    SPFile file = web.GetFile(pageUrl);
                    if (null != file)
                    {
                        using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
                        {
                            if (null != mgr)
                            {
                                //create new webpart object            
                                ContentEditorWebPart contentEditor = new ContentEditorWebPart();

                                //set properties of new webpart object     
                                contentEditor.ZoneID = zoneId;
                                contentEditor.Title = title;
                                contentEditor.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
                                contentEditor.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleAndBorder;

                                //Add content to CEWP
                                XmlDocument xmlDoc = new XmlDocument();
                                XmlElement xmlElement = xmlDoc.CreateElement("Root");
                                xmlElement.InnerText = textCEWP;
                                contentEditor.Content = xmlElement;
                                contentEditor.Content.InnerText = xmlElement.InnerText;

                                //Add it to the zone
                                mgr.AddWebPart(contentEditor, contentEditor.ZoneID, 0);

                                web.Update();
                            }
                        }
                    }
                }
                finally
                {
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM