开发者

How to create a editable property on a SharePoint 2010 Sandboxed Visual WebPart

开发者 https://www.devze.com 2023-02-26 17:01 出处:网络
I am developing a sandboxed visual webpart using SharePoint 2010 in Visual Studio 2010 with the SharePoint powertools installed.The webpart deploys and works as expected, except that the properties a开

I am developing a sandboxed visual webpart using SharePoint 2010 in Visual Studio 2010 with the SharePoint powertools installed. The webpart deploys and works as expected, except that the properties a开发者_JAVA百科re not editable. I believe that the core problem is that WebPartStorageAttribute is not available in sandbox, but haven't been able to find guidance on how to create a sandboxed webpart with editable properties. Is this even possible?

[ToolboxItem(false)]
[XmlRoot(Namespace="MyNamespace")]
public partial class MyWebPart: System.Web.UI.WebControls.WebParts.WebPart
{
     const string  defaultStartTime = "00:30:00";
     private string _startTime = "00:30:00";

    [Browsable(true)]
    [WebBrowsable(true)]
    [Category("Timer")]
    [Description("Start time to use if the user has not set a different one.")]
    [XmlElement(ElementName="StartTime")]
    [DefaultValue(defaultStartTime)]
    [FriendlyName("Start Time")]       
    public string  StartTime
    {
        get
        {
            return _startTime;
        }
        set
        {
            _startTime = value;
        }
    }
...

Is there something missing in the above code? Is it possible to create an editable sandboxed webpart, and if so, how is it done?


I just went through that process and this is how I managed to have it run on under a sandbox solution and also a 365 site, this is the entire sample code:

Note: I'm using Visual WebPart (SandBoxed) within SharePoint Power Tools for Visual Studio

ToolPartTest.ascx:

<h3>Selected List</h3>
<dd><asp:Literal runat="server" ID="list" /></dd>

TooLPartTest.ascx.cs:

[WebBrowsable(true), Personalizable(true)]
public string ListName { get; set; }

protected override void OnInit(EventArgs e)
{
  base.OnInit(e);
  InitializeControl();
}

protected void Page_Load(object sender, EventArgs e)
{

}

protected override void OnPreRender(EventArgs e)
{
  base.OnPreRender(e);

  list.Text = ListName ?? "(None)";
}

public override EditorPartCollection CreateEditorParts()
{
  return new EditorPartCollection(base.CreateEditorParts(),
    new[]
    {
      new CustomEditorPart
      {
        ID = ID + "_editorPart"
      }
    }
  );
}

ToolPart.cs

public class CustomEditorPart : EditorPart
{
  private DropDownList _list;

  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);

    _list = new DropDownList();
    _list.AppendDataBoundItems = true;
    _list.Items.Add("");
    _list.DataTextField = "Title";
    _list.DataValueField = "Title";
    _list.DataSource = SPContext.Current.Site.RootWeb.Lists;
    _list.DataBind();

    Controls.Add(new LiteralControl("List: "));
    Controls.Add(_list);

  }

  public override bool ApplyChanges()
  {
    EnsureChildControls();

    if (!string.IsNullOrEmpty(_list.SelectedValue))
      ((ToolPartTest)WebPartToEdit).ListName = _list.SelectedValue;

    return true;
  }

  public override void SyncChanges()
  {
    EnsureChildControls();

    var webpart = ((ToolPartTest)WebPartToEdit);
    _list.SelectedValue = webpart.ListName;

  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消