Modificar las propiedades de un WebPart sin usar el control de EditWebPart por defecto de SharePoint January 18, 2013
Posted by juanpablo1manrique in SharePoint, SharePoint Development.Tags: SharePoint
trackback
Hola amigos
En esta oportunidad he tenido el atrevimiento de modificar las propiedades de un webPart desde un boton sin necesidad de utilizar el control de propiedades que trae por defecto SharePoint,
Recordemos que un WebPart esta dividido en 2 clases importantes una para la definición de WebPart que hereda de Microsoft.SharePoint.UI.WebParts.WebPart de ahora en adelante WebPartMyClass y otra que es un User Control ascx; en el caso de los WebPart visuales de ahora en adelante alias UserControlWebPartMyClass,
Para lograr salvar las propiedades de esta manera es necesario construir un metodo publico en WebPartMyClass de la siguiente manera
public void SaveCustomProperties(string val1, string val2)
{
this.Property1 = val1;
this.Property2 = val2;
this.SaveProperties = true;
this.SaveControlState();
}
El código dice más que mil palabras, 🙂
Y desde el evento de boton del ascx (UserControlWebPartMyClass) llamar este metodo que acabamos de construir de la siguiente manera
protected void Button1_Click(object sender, EventArgs e)
{
((WebPartMyClass)this.Parent).SaveCustomProperties(“val1”, “val2”);
}
Aqui el código de las 2 clases
— Clase WebPartMyClass
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace xxxx
{
[ToolboxItemAttribute(false)]
public class Processes : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @”~/_CONTROLTEMPLATES/yyyyy/UserControlWebPartMyClass.ascx”;
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
((UserControlWebPartMyClass)control).Property1 = this.Property1;
((UserControlWebPartMyClass)control).Property2 = this.Property2;
Controls.Add(control);
}
public void SaveCustomProperties(string val1, string val2)
{
this.Property1 = val1;
this.Property2 = val2;
this.SaveProperties = true;
this.SaveControlState();
}
[WebBrowsable(true), WebDisplayName(“Property1”), WebDescription(“Property1”),
Personalizable(PersonalizationScope.Shared), Category(“Custom Property”),
System.ComponentModel.DefaultValue(“”)]
public string Property1{get;set;}
[WebBrowsable(true), WebDisplayName(“Property1”), WebDescription(“Property1”),
Personalizable(PersonalizationScope.Shared), Category(“Custom Property”),
System.ComponentModel.DefaultValue(“”)]
public string Property2{get;set;}
}
}
— UserControlWebPartMyClass —
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
namespace xxxx
{
public partial class ProcessesUserControl : WebPartControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
((WebPartMyClass)this.Parent).SaveCustomProperties(“val1”, “val2”);
}
}
}
Este post fue inspirado por esta duda
Comments»
No comments yet — be the first.