Pregunta

Estoy siguiendo este método (http://msdn.microsoft.com/en-us/library/ms450826.aspx) para añadir un webpartpage (samplewpp.aspx) y funciona. Sin embargo, tengo que añadir una descripción de línea también. ¿Cómo?

No hay solución correcta

Otros consejos

Se necesita agregar un elemento Web Content Editor (CEWP) a la página y luego agregar su descripción a esto. El CEWP le permite poner text / html en una página.

Para hacer esto mediante programación a continuación, siga algo así como el código de 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;
                }
            }
        }
    });
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top