tags:

views:

288

answers:

1

In the codebehind of a page.

How do i get hold of a webpart that exist on the page, then add properties to that webpart using c#.

The webpart exists withing a webpartzone.

Do i need to do anything with SPWebPartManager?

+1  A: 

Use SPWeb.GetLimitedWebPartManager. The following example demonstrates updating a property in a list view web part:

using (SPLimitedWebPartManager webPartManager =
    SPContext.Current.Web.GetLimitedWebPartManager("default.aspx",
        PersonalizationScope.Shared))
{
    try
    {
     foreach (WebPart webPart in webPartManager.WebParts)
     {
      if (webPart.Title == "Web Part To Update")
      {
       ListViewWebPart listViewWebPart = (ListViewWebPart)webPart;
       // TODO: Set property on web part
       webPartManager.SaveChanges(listViewWebPart);
       break;
      }
     }
    }
    finally
    {
     webPartManager.Web.Dispose();
    }
}

Instead of default.aspx you need to use the name of the current page relative to the SPWeb.

Alex Angas
thanks for this, how do i do the setting a property bit? eg in an exported .webpart file i have this property...how can i set that? <property name="AnchorLocation" type="string">/en</property>
raklos
no probs found it. thanks
raklos
Yes as you would've seen it is just set like any other property on a class.
Alex Angas